Class: OpenTelemetry::CorrelationContext::Propagation::TextExtractor

Inherits:
Object
  • Object
show all
Includes:
OpenTelemetry::Context::Propagation::DefaultGetter
Defined in:
lib/opentelemetry/correlation_context/propagation/text_extractor.rb

Overview

Extracts correlations from carriers in the W3C Correlation Context format

Instance Method Summary collapse

Methods included from OpenTelemetry::Context::Propagation::DefaultGetter

#default_getter

Constructor Details

#initialize(correlation_context_key: 'Correlation-Context') ⇒ TextExtractor

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

Parameters:

  • correlation_context_key (String) (defaults to: 'Correlation-Context')

    The correlation context header key used in the carrier



22
23
24
# File 'lib/opentelemetry/correlation_context/propagation/text_extractor.rb', line 22

def initialize(correlation_context_key: 'Correlation-Context')
  @correlation_context_key = correlation_context_key
end

Instance Method Details

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

Extract remote correlations 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 correlations

  • getter (optional Callable)

    An optional callable that takes a carrier and a key and returns the value associated with the key. If omitted the default getter will be used which expects the carrier to respond to [] and []=.

Yields:

  • (Carrier, String)

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

Returns:

  • (Context)

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



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

def extract(carrier, context, &getter)
  getter ||= default_getter
  header = getter.call(carrier, @correlation_context_key)

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

  correlations = 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.correlation_context_key, correlations)
rescue StandardError
  context
end