Class: Datadog::Tracing::Distributed::B3Multi

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

Overview

B3 multi header-style trace propagation.

Constant Summary collapse

B3_TRACE_ID_KEY =
'x-b3-traceid'
B3_SPAN_ID_KEY =
'x-b3-spanid'
B3_SAMPLED_KEY =
'x-b3-sampled'

Instance Method Summary collapse

Constructor Details

#initialize(fetcher:, trace_id_key: B3_TRACE_ID_KEY, span_id_key: B3_SPAN_ID_KEY, sampled_key: B3_SAMPLED_KEY) ⇒ B3Multi

Returns a new instance of B3Multi.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datadog/tracing/distributed/b3_multi.rb', line 17

def initialize(
  fetcher:,
  trace_id_key: B3_TRACE_ID_KEY,
  span_id_key: B3_SPAN_ID_KEY,
  sampled_key: B3_SAMPLED_KEY
)
  @trace_id_key = trace_id_key
  @span_id_key = span_id_key
  @sampled_key = sampled_key
  @fetcher = fetcher
end

Instance Method Details

#extract(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datadog/tracing/distributed/b3_multi.rb', line 46

def extract(data)
  # DEV: B3 doesn't have "origin"
  fetcher = @fetcher.new(data)

  trace_id = Helpers.parse_hex_id(fetcher[@trace_id_key])

  # Return early if this propagation is not valid
  return if trace_id.nil? || trace_id <= 0 || trace_id > Tracing::Utils::TraceId::MAX

  span_id = Helpers.parse_hex_id(fetcher[@span_id_key])

  # Return early if this propagation is not valid
  return if span_id.nil? || span_id <= 0 || span_id >= Tracing::Utils::EXTERNAL_MAX_ID

  # We don't need to try and convert sampled since B3 supports 0/1 (AUTO_REJECT/AUTO_KEEP)
  sampling_priority = Helpers.parse_decimal_id(fetcher[@sampled_key])

  TraceDigest.new(
    trace_id: trace_id,
    span_id: span_id,
    trace_sampling_priority: sampling_priority
  )
end

#inject!(digest, data = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/tracing/distributed/b3_multi.rb', line 29

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

  # DEV: We need these to be hex encoded
  data[@trace_id_key] = format('%032x', digest.trace_id)
  data[@span_id_key] = format('%016x', digest.span_id)

  if digest.trace_sampling_priority
    sampling_priority = Helpers.clamp_sampling_priority(
      digest.trace_sampling_priority
    )
    data[@sampled_key] = sampling_priority.to_s
  end

  data
end