Class: OpenTelemetry::Propagator::Jaeger::TextMapExtractor

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

Overview

Extracts context from carriers

Constant Summary collapse

TRACE_SPAN_IDENTITY_REGEX =
/\A(?<trace_id>(?:[0-9a-f]){1,32}):(?<span_id>([0-9a-f]){1,16}):[0-9a-f]{1,16}:(?<sampling_flags>[0-9a-f]{1,2})\z/.freeze
ZERO_ID_REGEX =
/^0+$/.freeze

Instance Method Summary collapse

Constructor Details

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

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

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/jaeger/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 Jaeger context from the supplied carrier and set the active span in the given context. The original context will be return if Jaeger 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.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opentelemetry/propagator/jaeger/text_map_extractor.rb', line 48

def extract(carrier, context, getter = nil)
  getter ||= @default_getter
  header = getter.get(carrier, IDENTITY_KEY)
  return context unless (match = header.match(TRACE_SPAN_IDENTITY_REGEX))
  return context if match['trace_id'] =~ ZERO_ID_REGEX
  return context if match['span_id'] =~ ZERO_ID_REGEX

  sampling_flags = match['sampling_flags'].to_i
  span = build_span(match, sampling_flags)
  context = Jaeger.context_with_debug(context) if sampling_flags & DEBUG_FLAG_BIT != 0
  context = set_baggage(carrier, context, getter)
  Trace.context_with_span(span, parent_context: context)
end