Module: Datadog::OpenTracer::TextMapPropagator

Extended by:
Ext::DistributedTracing, Propagator
Includes:
Ext::DistributedTracing
Defined in:
lib/ddtrace/opentracer/text_map_propagator.rb

Overview

OpenTracing propagator for Datadog::OpenTracer::Tracer

Constant Summary collapse

BAGGAGE_PREFIX =
'ot-baggage-'.freeze

Constants included from Ext::DistributedTracing

Ext::DistributedTracing::B3_HEADER_SAMPLED, Ext::DistributedTracing::B3_HEADER_SINGLE, Ext::DistributedTracing::B3_HEADER_SPAN_ID, Ext::DistributedTracing::B3_HEADER_TRACE_ID, Ext::DistributedTracing::GRPC_METADATA_ORIGIN, Ext::DistributedTracing::GRPC_METADATA_PARENT_ID, Ext::DistributedTracing::GRPC_METADATA_SAMPLING_PRIORITY, Ext::DistributedTracing::GRPC_METADATA_TRACE_ID, Ext::DistributedTracing::HTTP_HEADER_ORIGIN, Ext::DistributedTracing::HTTP_HEADER_PARENT_ID, Ext::DistributedTracing::HTTP_HEADER_SAMPLING_PRIORITY, Ext::DistributedTracing::HTTP_HEADER_TRACE_ID, Ext::DistributedTracing::ORIGIN_KEY, Ext::DistributedTracing::PROPAGATION_EXTRACT_STYLE_ENV_OLD, Ext::DistributedTracing::PROPAGATION_INJECT_STYLE_ENV_OLD, Ext::DistributedTracing::PROPAGATION_STYLE_B3, Ext::DistributedTracing::PROPAGATION_STYLE_B3_SINGLE_HEADER, Ext::DistributedTracing::PROPAGATION_STYLE_DATADOG, Ext::DistributedTracing::PROPAGATION_STYLE_EXTRACT_ENV, Ext::DistributedTracing::PROPAGATION_STYLE_INJECT_ENV, Ext::DistributedTracing::SAMPLING_PRIORITY_KEY

Class Method Summary collapse

Methods included from Propagator

extract, inject

Class Method Details

.extract(carrier) ⇒ SpanContext?

Extract a SpanContext in TextMap format from the given carrier.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ddtrace/opentracer/text_map_propagator.rb', line 40

def extract(carrier)
  # First extract & build a Datadog context
  headers = DistributedHeaders.new(carrier)

  datadog_context = if headers.valid?
                      Datadog::Context.new(
                        trace_id: headers.trace_id,
                        span_id: headers.parent_id,
                        sampling_priority: headers.sampling_priority,
                        origin: headers.origin
                      )
                    else
                      Datadog::Context.new
                    end

  # Then extract any other baggage
  baggage = {}
  carrier.each do |key, value|
    baggage[item_to_baggage(key)] = value if baggage_item?(key)
  end

  SpanContextFactory.build(datadog_context: datadog_context, baggage: baggage)
end

.inject(span_context, carrier) ⇒ Object

Inject a SpanContext into the given carrier



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ddtrace/opentracer/text_map_propagator.rb', line 19

def inject(span_context, carrier)
  # Inject Datadog trace properties
  span_context.datadog_context.tap do |datadog_context|
    carrier[HTTP_HEADER_TRACE_ID] = datadog_context.trace_id
    carrier[HTTP_HEADER_PARENT_ID] = datadog_context.span_id
    carrier[HTTP_HEADER_SAMPLING_PRIORITY] = datadog_context.sampling_priority
    carrier[HTTP_HEADER_ORIGIN] = datadog_context.origin
  end

  # Inject baggage
  span_context.baggage.each do |key, value|
    carrier[BAGGAGE_PREFIX + key] = value
  end

  nil
end