Class: OpenTelemetry::Baggage::Propagation::TextMapExtractor

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

Overview

Extracts baggage from carriers in the W3C Baggage format

Instance Method Summary collapse

Constructor Details

#initialize(default_getter = Context::Propagation.text_map_getter) ⇒ TextMapExtractor

Returns a new TextMapExtractor that extracts context using the specified getter

Parameters:

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

    The default getter used to read headers from a carrier during extract. Defaults to a Context::Propagation::TextMapGetter instance.



21
22
23
# File 'lib/opentelemetry/baggage/propagation/text_map_extractor.rb', line 21

def initialize(default_getter = Context::Propagation.text_map_getter)
  @default_getter = default_getter
end

Instance Method Details

#extract(carrier, context, getter = nil) ⇒ Context

Extract remote baggage from the supplied carrier. If extraction fails, the original context will be returned

Parameters:

  • carrier (Carrier)

    The carrier to get the header from

  • context (Context)

    The context to be updated with extracted baggage

  • getter (optional Getter) (defaults to: nil)

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

Returns:

  • (Context)

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/opentelemetry/baggage/propagation/text_map_extractor.rb', line 35

def extract(carrier, context, getter = nil)
  getter ||= @default_getter
  header = getter.get(carrier, BAGGAGE_KEY)

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

  baggage = entries.each_with_object({}) do |entry, memo|
    # The ignored variable below holds properties as per the W3C spec.
    # OTel is not using them currently, but they might be used for
    # metadata in the future
    kv, = entry.split(';', 2)
    k, v = kv.split('=').map!(&CGI.method(:unescape))
    memo[k] = v
  end

  context.set_value(ContextKeys.baggage_key, baggage)
rescue StandardError
  context
end