Class: Datadog::OpenTelemetry::SDK::Propagator

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/opentelemetry/sdk/propagator.rb

Overview

Compatibility wrapper to allow Datadog propagators to fulfill the OpenTelemetry propagator API.

Instance Method Summary collapse

Constructor Details

#initialize(datadog_propagator) ⇒ Propagator

Returns a new instance of Propagator.



9
10
11
# File 'lib/datadog/opentelemetry/sdk/propagator.rb', line 9

def initialize(datadog_propagator)
  @datadog_propagator = datadog_propagator
end

Instance Method Details

#extract(carrier, context: ::OpenTelemetry::Context.current, getter: ::OpenTelemetry::Context::Propagation.text_map_getter) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/datadog/opentelemetry/sdk/propagator.rb', line 29

def extract(
  carrier, context: ::OpenTelemetry::Context.current,
  getter: ::OpenTelemetry::Context::Propagation.text_map_getter
)
  if getter != ::OpenTelemetry::Context::Propagation.text_map_getter &&
      getter != ::OpenTelemetry::Common::Propagation.rack_env_getter
    # PENDING: Not to report telemetry logs for now
    Datadog.logger.error(
      "Custom getter #{getter} is not supported. Please inform the `datadog` team at " \
    ' https://github.com/DataDog/dd-trace-rb of your use case so we can best support you. Using the default ' \
    'OpenTelemetry::Context::Propagation.text_map_getter as a fallback getter.'
    )
  end

  digest = @datadog_propagator.extract(carrier)
  return context unless digest

  # Converts the {Numeric} Datadog id object to OpenTelemetry's byte array format.
  # 128-bit unsigned, big-endian integer
  trace_id = [digest.trace_id >> 64, digest.trace_id & 0xFFFFFFFFFFFFFFFF].pack('Q>Q>')
  # 64-bit unsigned, big-endian integer
  span_id = [digest.span_id].pack('Q>')

  if digest.trace_state || digest.trace_flags
    trace_flags = ::OpenTelemetry::Trace::TraceFlags.from_byte(digest.trace_flags)
    tracestate = Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_tracestate, digest)
  else
    trace_flags = if Tracing::Sampling::PrioritySampler.sampled?(digest.trace_sampling_priority)
                    ::OpenTelemetry::Trace::TraceFlags::SAMPLED
                  else
                    ::OpenTelemetry::Trace::TraceFlags::DEFAULT
                  end
    tracestate = ::OpenTelemetry::Trace::Tracestate::DEFAULT
  end

  span_context = ::OpenTelemetry::Trace::SpanContext.new(
    trace_id: trace_id,
    span_id: span_id,
    trace_flags: trace_flags,
    tracestate: tracestate,
    remote: true
  )

  span = ::OpenTelemetry::Trace.non_recording_span(span_context)

  trace = Tracing.continue_trace!(digest)

  span.datadog_trace = trace

  ::OpenTelemetry::Trace.context_with_span(span, parent_context: context)
end

#fieldsObject

Returns fields set by this propagator. DEV: Doesn’t seem like it’s used in production Otel code paths.



83
84
85
# File 'lib/datadog/opentelemetry/sdk/propagator.rb', line 83

def fields
  []
end

#inject(carrier, context: ::OpenTelemetry::Context.current, setter: ::OpenTelemetry::Context::Propagation.text_map_setter) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datadog/opentelemetry/sdk/propagator.rb', line 13

def inject(
  carrier, context: ::OpenTelemetry::Context.current,
  setter: ::OpenTelemetry::Context::Propagation.text_map_setter
)
  unless setter == ::OpenTelemetry::Context::Propagation.text_map_setter
    # PENDING: Not to report telemetry logs for now
    Datadog.logger.error(
      'Custom setter is not supported. Please inform the `datadog` team at ' \
    ' https://github.com/DataDog/dd-trace-rb of your use case so we can best support you. Using the default ' \
    'OpenTelemetry::Context::Propagation.text_map_setter as a fallback setter.'
    )
  end

  @datadog_propagator.inject(context.trace.to_digest, carrier)
end