Class: OpenTelemetry::Baggage::Propagation::TextMapPropagator

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/baggage/propagation/text_map_propagator.rb

Overview

Propagates baggage using the W3C Baggage format

Instance Method Summary collapse

Instance Method Details

#extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter) ⇒ Context

Extract remote baggage from the supplied carrier. If extraction fails or there is no baggage to extract, then the original context will be returned

Parameters:

  • carrier (Carrier)

    The carrier to get the header from

  • context (optional Context) (defaults to: Context.current)

    Context to be updated with the baggage extracted from the carrier. Defaults to Context.current.

  • getter (optional Getter) (defaults to: Context::Propagation.text_map_getter)

    If the optional getter is provided, it will be used to read the header from the carrier, otherwise the default text map getter will be used.

Returns:

  • (Context)

    context updated with extracted baggage, or the original context if extraction fails

[View source]

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opentelemetry/baggage/propagation/text_map_propagator.rb', line 54

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  header = getter.get(carrier, BAGGAGE_KEY)
  return context if header.nil? || header.empty?

  entries = header.gsub(/\s/, '').split(',')

  OpenTelemetry::Baggage.build(context: context) do |builder|
    entries.each do |entry|
      # Note metadata is currently unused in OpenTelemetry, but is part
      # the W3C spec where it's referred to as properties. We preserve
      # the properties (as-is) so that they can be propagated elsewhere.
      kv, meta = entry.split(';', 2)
      k, v = kv.split('=').map!(&CGI.method(:unescape))
      builder.set_value(k, v, metadata: meta)
    end
  end
rescue StandardError => e
  OpenTelemetry.logger.debug "Error extracting W3C baggage: #{e.message}"
  context
end

#fieldsArray<String>

Returns the predefined propagation fields. If your carrier is reused, you should delete the fields returned by this method before calling inject.

Returns:

  • (Array<String>)

    a list of fields that will be used by this propagator.

[View source]

79
80
81
# File 'lib/opentelemetry/baggage/propagation/text_map_propagator.rb', line 79

def fields
  FIELDS
end

#inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter) ⇒ Object

Inject in-process baggage into the supplied carrier.

Parameters:

  • carrier (Carrier)

    The mutable carrier to inject baggage into

  • context (Context) (defaults to: Context.current)

    The context to read baggage from

  • setter (optional Setter) (defaults to: Context::Propagation.text_map_setter)

    If the optional setter is provided, it will be used to write context into the carrier, otherwise the default text map setter will be used.

[View source]

31
32
33
34
35
36
37
38
39
# File 'lib/opentelemetry/baggage/propagation/text_map_propagator.rb', line 31

def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
  baggage = OpenTelemetry::Baggage.raw_entries(context: context)

  return if baggage.nil? || baggage.empty?

  encoded_baggage = encode(baggage)
  setter.set(carrier, BAGGAGE_KEY, encoded_baggage) unless encoded_baggage&.empty?
  nil
end