Module: Foam::Otel::Stamping

Defined in:
lib/foam/otel/stamps.rb

Overview

Shared stamping logic for the span and log processors: default foam.instrumentation to app.custom, copy foam.rid from baggage, and attach dd.trace_id/dd.span_id verbatim when a Datadog trace is active (SPEC section 7.3). One implementation so spans and logs can never disagree. get/set abstract the two write shapes (span setter vs a plain attributes hash). Returns the number of attributes ADDED.

Class Method Summary collapse

Class Method Details

.baggage_rid(context) ⇒ Object



30
31
32
33
34
35
# File 'lib/foam/otel/stamps.rb', line 30

def baggage_rid(context)
  value = OpenTelemetry::Baggage.value(RID_KEY, context: context)
  value.is_a?(String) && !value.empty? ? value : nil
rescue StandardError
  nil
end

.candidates(context) ⇒ Object

The stamp attributes to apply, as [[key, value], ...]. The caller sets each only when absent. No per-call closures (this is the hottest path); Datadog correlation is resolved once here, not per attribute.



20
21
22
23
24
25
26
27
28
# File 'lib/foam/otel/stamps.rb', line 20

def candidates(context)
  pairs = [[INSTRUMENTATION_KEY, STAMPS::APP_CUSTOM]]
  rid = baggage_rid(context)
  pairs << [RID_KEY, rid] if rid
  Vendor::Datadog.correlation_attributes&.each { |key, value| pairs << [key, value] }
  pairs
rescue StandardError
  pairs || []
end