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

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

Overview

Extracts baggage from carriers in the W3C Baggage format

Instance Method Summary collapse

Methods included from Context::Propagation::DefaultGetter

#default_getter

Constructor Details

#initialize(baggage_key: 'baggage') ⇒ TextMapExtractor

Returns a new TextMapExtractor that extracts context using the specified header key



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

def initialize(baggage_key: 'baggage')
  @baggage_key = baggage_key
end

Instance Method Details

#extract(carrier, context, &getter) {|Carrier, String| ... } ⇒ Context

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

Yields:

  • (Carrier, String)

    if an optional getter is provided, extract will yield the carrier and the header key to the getter.



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

def extract(carrier, context, &getter)
  getter ||= default_getter
  header = getter.call(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