Class: OpenTelemetry::Propagator::B3::Single::TextMapExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/propagator/b3/single/text_map_extractor.rb

Overview

Extracts context from carriers in the b3 single header format

Constant Summary collapse

B3_CONTEXT_REGEX =
/\A(?<trace_id>(?:[0-9a-f]{16}){1,2})-(?<span_id>[0-9a-f]{16})(?:-(?<sampling_state>[01d](?![0-9a-f])))?(?:-(?<parent_span_id>[0-9a-f]{16}))?\z/.freeze
SAMPLED_VALUES =
%w[1 d].freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new TextMapExtractor that extracts b3 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.



32
33
34
# File 'lib/opentelemetry/propagator/b3/single/text_map_extractor.rb', line 32

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

Instance Method Details

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

Extract b3 context from the supplied carrier and set the active span in the given context. The original context will be returned if b3 cannot be extracted from the carrier.

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 active span derived from the header, or the original context if parsing fails.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opentelemetry/propagator/b3/single/text_map_extractor.rb', line 47

def extract(carrier, context, getter = nil)
  getter ||= @default_getter
  header = getter.get(carrier, B3_CONTEXT_KEY)
  return context unless (match = header.match(B3_CONTEXT_REGEX))

  span_context = Trace::SpanContext.new(
    trace_id: B3.to_trace_id(match['trace_id']),
    span_id: B3.to_span_id(match['span_id']),
    trace_flags: to_trace_flags(match['sampling_state']),
    remote: true
  )

  span = Trace::Span.new(span_context: span_context)
  context = B3.context_with_debug(context) if match['sampling_state'] == 'd'
  Trace.context_with_span(span, parent_context: context)
rescue OpenTelemetry::Error
  context
end