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

Inherits:
Object
  • Object
show all
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

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 TextMapGetter instance.



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

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

Instance Method Details

#extract(carrier, context, getter = nil) ⇒ 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 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)

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb', line 33

def extract(carrier, context, getter = nil)
  getter ||= @default_getter
  tp = TraceParent.from_string(getter.get(carrier, TRACEPARENT_KEY))
  tracestate = Tracestate.from_string(getter.get(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