Class: Datadog::Tracing::OTelThreadContext

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/otel_thread_context.rb,
ext/libdatadog_api/otel_thread_context.c

Overview

Publishes a per-thread OpenTelemetry context record into a thread-local slot, so an out-of-process reader (e.g. the eBPF profiler) can discover it.

See the "OTel Thread Context" OTEP: https://github.com/open-telemetry/opentelemetry-specification/blob/main/oteps/profiles/4947-thread-ctx.md

Native functions are implemented in ext/libdatadog_api/otel_thread_context.c

Defined Under Namespace

Modules: Testing

Constant Summary collapse

UNKNOWN_LOCAL_ROOT_SPAN_ID =
0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ OTelThreadContext

Returns a new instance of OTelThreadContext.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/datadog/tracing/otel_thread_context.rb', line 24

def initialize
  @span_before_start = proc do |event_span_op, event_trace_op|
    set(
      trace_id: event_trace_op.id,
      span_id: event_span_op.id,
      local_root_span_id: event_trace_op.send(:root_span).id,
    )
  end

  @span_finished = proc do |_event_span_op, event_trace_op|
    next if event_trace_op.finished?

    update_from_trace_op(event_trace_op)
  end
end

Class Method Details

.build(tracing_settings) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/datadog/tracing/otel_thread_context.rb', line 15

def self.build(tracing_settings)
  return unless tracing_settings.otel_thread_context_enabled
  return unless Datadog::Core::LIBDATADOG_API_FAILURE.nil?

  instance = new
  instance._native_enable
  instance
end

Instance Method Details

#_native_clear ⇒ Object



32
# File 'ext/libdatadog_api/otel_thread_context.c', line 32

static VALUE native_clear(VALUE _self);

#_native_enable ⇒ Object



33
# File 'ext/libdatadog_api/otel_thread_context.c', line 33

static VALUE native_enable(VALUE _self);

#_native_set ⇒ Object



31
# File 'ext/libdatadog_api/otel_thread_context.c', line 31

static VALUE native_set(VALUE _self, VALUE trace_id, VALUE span_id, VALUE local_root_span_id);

#after_fork ⇒ Object



56
57
58
# File 'lib/datadog/tracing/otel_thread_context.rb', line 56

def after_fork
  clear
end

#clear ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/datadog/tracing/otel_thread_context.rb', line 60

def clear
  _native_clear
rescue => e
  Datadog.logger.debug do
    "Error clearing OTel thread context: #{e.class}: #{e.message}"
  end

  false
end

#set(trace_id:, span_id:, local_root_span_id:) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/datadog/tracing/otel_thread_context.rb', line 47

def set(trace_id:, span_id:, local_root_span_id:)
  # OTel requires trace-id and span-id to be either both set or both unset.
  # Zeroes can be used to indicate that no trace and span are active.
  # https://github.com/open-telemetry/opentelemetry-specification/blob/v1.61.0/oteps/profiles/4947-thread-ctx.md?plain=1#L116C144-L116C189
  return clear if trace_id.zero? || span_id.zero?

  _native_set(trace_id, span_id, local_root_span_id)
end

#subscribe_to_tracer_events!(events) ⇒ Object



42
43
44
45
# File 'lib/datadog/tracing/otel_thread_context.rb', line 42

def subscribe_to_tracer_events!(events)
  events.span_before_start.subscribe(&@span_before_start)
  events.span_finished.subscribe(&@span_finished)
end

#update_from_trace_op(trace_op) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datadog/tracing/otel_thread_context.rb', line 70

def update_from_trace_op(trace_op)
  active_span = trace_op.active_span

  if active_span
    # Internally created active span
    set(
      trace_id: trace_op.id,
      span_id: active_span.id,
      local_root_span_id: trace_op.send(:root_span).id,
    )
  elsif trace_op.parent_span_id && trace_op.parent_span_id != 0
    # Active distributed trace context, before first local trace is created
    set(
      trace_id: trace_op.id,
      span_id: trace_op.parent_span_id,
      local_root_span_id: UNKNOWN_LOCAL_ROOT_SPAN_ID,
    )
  else
    clear
  end
rescue => e
  Datadog.logger.debug do
    "Error updating OTel thread context: #{e.class}: #{e.message}"
  end
end