Class: OpenTelemetry::Exporters::Datadog::Propagator
- Inherits:
-
Object
- Object
- OpenTelemetry::Exporters::Datadog::Propagator
- 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
-
#extract(carrier, context, &getter) {|Carrier, String| ... } ⇒ Context
Extract a remote Trace::SpanContext from the supplied carrier.
-
#initialize ⇒ Propagator
constructor
Returns a new Propagator.
-
#inject(carrier, context, &setter) ⇒ Object
Set the span context on the supplied carrier.
Constructor Details
#initialize ⇒ Propagator
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_configure ⇒ Object
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.
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.}") context end |
#inject(carrier, context, &setter) ⇒ Object
Set the span context on the supplied carrier.
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 |