Class: OpenTelemetry::Exporters::Datadog::Propagator

Inherits:
Object
  • Object
show all
Includes:
Context::Propagation::DefaultGetter, Context::Propagation::DefaultSetter
Defined in:
lib/opentelemetry/exporters/datadog/propagator.rb

Overview

Injects context into carriers using the W3C Trace Context format

Constant Summary collapse

TRACE_ID_KEY =
'x-datadog-trace-id'
PARENT_ID_KEY =
'x-datadog-parent-id'
SAMPLING_PRIORITY_KEY =
'x-datadog-sampling-priority'
ORIGIN_KEY =
'x-datadog-origin'
DD_ORIGIN =
'_dd_origin'
ORIGIN_REGEX =
/#{DD_ORIGIN}\=(.*?)($|,)/.freeze
DEFAULT_INJECTORS =
[
  OpenTelemetry::Trace::Propagation::TraceContext.text_map_injector,
  OpenTelemetry::Baggage::Propagation.text_map_injector
].freeze
DEFAULT_EXTRACTORS =
[
  OpenTelemetry::Trace::Propagation::TraceContext.rack_extractor,
  OpenTelemetry::Baggage::Propagation.rack_extractor
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePropagator

Returns a new Propagator



37
38
39
40
# File 'lib/opentelemetry/exporters/datadog/propagator.rb', line 37

def initialize
  # pass
  @truncation_helper = ::Datadog::DistributedTracing::Headers::Headers.new({})
end

Class Method Details

.auto_configureObject



102
103
104
105
106
107
108
109
110
# File 'lib/opentelemetry/exporters/datadog/propagator.rb', line 102

def self.auto_configure
  default_propagator = new
  updated_injectors = DEFAULT_INJECTORS + [default_propagator]
  updated_extractors = DEFAULT_EXTRACTORS + [default_propagator]
  OpenTelemetry.propagation.http = OpenTelemetry::Context::Propagation::CompositePropagator.new(
    updated_injectors,
    updated_extractors
  )
end

Instance Method Details

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

Extract a remote Trace::SpanContext from the supplied carrier. Invalid headers will result in a new, valid, non-remote Trace::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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/opentelemetry/exporters/datadog/propagator.rb', line 76

def extract(carrier, context, &getter)
  getter ||= default_getter
  trace_id = getter.call(carrier, TRACE_ID_KEY) || getter.call(carrier, rack_helper(TRACE_ID_KEY))
  span_id = getter.call(carrier, PARENT_ID_KEY) || getter.call(carrier, rack_helper(PARENT_ID_KEY))
  sampled = getter.call(carrier, SAMPLING_PRIORITY_KEY) || getter.call(carrier, rack_helper(SAMPLING_PRIORITY_KEY))
  origin = getter.call(carrier, ORIGIN_KEY) || getter.call(carrier, rack_helper(ORIGIN_KEY))

  is_sampled = sampled.to_i.positive? ? 1 : 0

  tracestate = origin ? "#{DD_ORIGIN}=#{origin}" : nil

  return context if trace_id.nil? || span_id.nil?

  span_context = Trace::SpanContext.new(trace_id: Array(trace_id.to_i.to_s(16)).pack('H*'),
                                        span_id: Array(span_id.to_i.to_s(16)).pack('H*'),
                                        trace_flags: OpenTelemetry::Trace::TraceFlags.from_byte(is_sampled),
                                        tracestate: tracestate,
                                        remote: true)

  span = Trace::Span.new(span_context: span_context)
  Trace.context_with_span(span, parent_context: context)
rescue StandardError => e
  OpenTelemetry.logger.debug("error extracting datadog propagation, #{e.message}")
  context
end

#inject(carrier, context, &setter) ⇒ Object

Set the span context on the supplied carrier.

Parameters:

  • context (Context)

    The active Context.

  • setter (optional Callable)

    An optional callable that takes a carrier and a key and a value and assigns the key-value pair in the carrier. If omitted the default setter will be used which expects the carrier to respond to [] and []=.

Returns:

  • (Object)

    the carrier with context injected



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/opentelemetry/exporters/datadog/propagator.rb', line 49

def inject(carrier, context, &setter)
  return carrier unless (span_context = span_context_from(context))

  sampled = span_context.trace_flags&.sampled? ? 1 : 0

  origin = get_origin_string(span_context.tracestate)
  setter ||= DEFAULT_SETTER
  setter.call(carrier, PARENT_ID_KEY, @truncation_helper.value_to_id(span_context.span_id.unpack1('H*'), 16).to_s)
  setter.call(carrier, TRACE_ID_KEY, @truncation_helper.value_to_id(span_context.trace_id.unpack1('H*'), 16).to_s)
  setter.call(carrier, SAMPLING_PRIORITY_KEY, sampled.to_s)
  setter.call(carrier, ORIGIN_KEY, origin) if origin

  carrier
end