Class: OpenTelemetry::Trace::Propagation::TraceContext::TextMapExtractor

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

Overview

Extracts context from carriers in the W3C Trace Context format

Instance Method Summary collapse

Methods included from Context::Propagation::DefaultGetter

#default_getter

Constructor Details

#initialize(traceparent_key: 'traceparent', tracestate_key: 'tracestate') ⇒ TextMapExtractor

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

Parameters:

  • traceparent_key (String) (defaults to: 'traceparent')

    The traceparent header key used in the carrier

  • tracestate_key (String) (defaults to: 'tracestate')

    The tracestate header key used in the carrier



20
21
22
23
24
# File 'lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb', line 20

def initialize(traceparent_key: 'traceparent',
               tracestate_key: 'tracestate')
  @traceparent_key = traceparent_key
  @tracestate_key = tracestate_key
end

Instance Method Details

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

Extract a remote SpanContext from the supplied carrier. Invalid headers will result in a new, valid, non-remote SpanContext.

Parameters:

  • carrier (Carrier)

    The carrier to get the header from.

  • context (Context)

    The context to be updated with extracted context

  • 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)

    Updated context with span context from the header, or the original context if parsing fails.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb', line 38

def extract(carrier, context, &getter)
  getter ||= default_getter
  tp = TraceParent.from_string(getter.call(carrier, @traceparent_key))
  tracestate = Tracestate.from_string(getter.call(carrier, @tracestate_key))

  span_context = Trace::SpanContext.new(trace_id: tp.trace_id,
                                        span_id: tp.span_id,
                                        trace_flags: tp.flags,
                                        tracestate: tracestate,
                                        remote: true)
  span = Trace::Span.new(span_context: span_context)
  OpenTelemetry::Trace.context_with_span(span)
rescue OpenTelemetry::Error
  context
end