Class: Datadog::Tracing::Distributed::B3Single

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

Overview

B3 single header-style trace propagation.

DEV: Format: DEV: b3: TraceId-SpanId-SamplingState-ParentSpanId DEV: github.com/apache/incubator-zipkin-b3-propagation/tree/7c6e9f14d6627832bd80baa87ac7dabee7be23cf#single-header DEV: ‘SamplingState` and `ParentSpanId` are optional

Constant Summary collapse

B3_SINGLE_HEADER_KEY =
'b3'

Instance Method Summary collapse

Constructor Details

#initialize(fetcher:, key: B3_SINGLE_HEADER_KEY) ⇒ B3Single

Returns a new instance of B3Single.



20
21
22
23
# File 'lib/datadog/tracing/distributed/b3_single.rb', line 20

def initialize(fetcher:, key: B3_SINGLE_HEADER_KEY)
  @key = key
  @fetcher = fetcher
end

Instance Method Details

#extract(env) ⇒ Object



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

def extract(env)
  fetcher = @fetcher.new(env)
  value = fetcher[@key]

  return unless value

  parts = value.split('-')
  trace_id = Helpers.parse_hex_id(parts[0]) unless parts.empty?
  # 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(parts[1]) if parts.length > 1
  # Return early if this propagation is not valid
  return if span_id.nil? || span_id <= 0 || span_id >= Tracing::Utils::EXTERNAL_MAX_ID

  sampling_priority = Helpers.parse_decimal_id(parts[2]) if parts.length > 2

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

#inject!(digest, env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/datadog/tracing/distributed/b3_single.rb', line 25

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

  span_id = digest.span_id || 0 # Fall back to zero (invalid) if not present

  # DEV: We need these to be hex encoded
  value = "#{format('%032x', digest.trace_id)}-#{format('%016x', span_id)}"

  if digest.trace_sampling_priority
    sampling_priority = Helpers.clamp_sampling_priority(
      digest.trace_sampling_priority
    )
    value += "-#{sampling_priority}"
  end

  env[@key] = value
  env
end