Class: Datadog::Tracing::Distributed::TraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/distributed/trace_context.rb

Overview

W3C Trace Context propagator implementation, version 00. The trace is propagated through two fields: traceparent and tracestate.

Defined Under Namespace

Modules: Refine

Constant Summary collapse

TRACEPARENT_KEY =
'traceparent'
TRACESTATE_KEY =
'tracestate'
SPEC_VERSION =
'00'

Instance Method Summary collapse

Constructor Details

#initialize(fetcher:, traceparent_key: TRACEPARENT_KEY, tracestate_key: TRACESTATE_KEY) ⇒ TraceContext

Returns a new instance of TraceContext.



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

def initialize(
  fetcher:,
  traceparent_key: TRACEPARENT_KEY,
  tracestate_key: TRACESTATE_KEY
)
  @fetcher = fetcher
  @traceparent_key = traceparent_key
  @tracestate_key = tracestate_key
end

Instance Method Details

#extract(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datadog/tracing/distributed/trace_context.rb', line 38

def extract(data)
  fetcher = @fetcher.new(data)

  trace_id, parent_id, sampled, trace_flags = extract_traceparent(fetcher[@traceparent_key])

  return unless trace_id # Could not parse traceparent

  tracestate, sampling_priority, origin, tags, unknown_fields = extract_tracestate(fetcher[@tracestate_key])

  sampling_priority = parse_priority_sampling(sampled, sampling_priority)

  TraceDigest.new(
    span_id: parent_id,
    trace_id: trace_id,
    trace_origin: origin,
    trace_sampling_priority: sampling_priority,
    trace_distributed_tags: tags,
    trace_flags: trace_flags,
    trace_state: tracestate,
    trace_state_unknown_fields: unknown_fields,
  )
end

#inject!(digest, data) ⇒ Object



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

def inject!(digest, data)
  return if digest.nil?

  if (traceparent = build_traceparent(digest))
    data[@traceparent_key] = traceparent

    if (tracestate = build_tracestate(digest))
      data[@tracestate_key] = tracestate
    end
  end

  data
end