Module: Datadog::DistributedTracing::Headers::B3Single

Includes:
Ext::DistributedTracing
Defined in:
lib/ddtrace/distributed_tracing/headers/b3_single.rb

Overview

B3Single provides helpers to inject or extract headers for B3 single header style headers

Constant Summary

Constants included from Ext::DistributedTracing

Ext::DistributedTracing::B3_HEADER_SAMPLED, Ext::DistributedTracing::B3_HEADER_SINGLE, Ext::DistributedTracing::B3_HEADER_SPAN_ID, Ext::DistributedTracing::B3_HEADER_TRACE_ID, Ext::DistributedTracing::GRPC_METADATA_ORIGIN, Ext::DistributedTracing::GRPC_METADATA_PARENT_ID, Ext::DistributedTracing::GRPC_METADATA_SAMPLING_PRIORITY, Ext::DistributedTracing::GRPC_METADATA_TRACE_ID, Ext::DistributedTracing::HTTP_HEADER_ORIGIN, Ext::DistributedTracing::HTTP_HEADER_PARENT_ID, Ext::DistributedTracing::HTTP_HEADER_SAMPLING_PRIORITY, Ext::DistributedTracing::HTTP_HEADER_TRACE_ID, Ext::DistributedTracing::ORIGIN_KEY, Ext::DistributedTracing::PROPAGATION_EXTRACT_STYLE_ENV, Ext::DistributedTracing::PROPAGATION_INJECT_STYLE_ENV, Ext::DistributedTracing::PROPAGATION_STYLE_B3, Ext::DistributedTracing::PROPAGATION_STYLE_B3_SINGLE_HEADER, Ext::DistributedTracing::PROPAGATION_STYLE_DATADOG, Ext::DistributedTracing::SAMPLING_PRIORITY_KEY

Class Method Summary collapse

Class Method Details

.extract(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ddtrace/distributed_tracing/headers/b3_single.rb', line 31

def self.extract(env)
  # Header format:
  #   b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
  # https://github.com/apache/incubator-zipkin-b3-propagation/tree/7c6e9f14d6627832bd80baa87ac7dabee7be23cf#single-header
  # DEV: `{SamplingState}` and `{ParentSpanId`}` are optional

  headers = Headers.new(env)
  value = headers.header(B3_HEADER_SINGLE)
  return if value.nil?

  parts = value.split('-')
  trace_id = headers.value_to_id(parts[0], 16) unless parts.empty?
  span_id = headers.value_to_id(parts[1], 16) if parts.length > 1
  sampling_priority = headers.value_to_number(parts[2]) if parts.length > 2

  # Return early if this propagation is not valid
  return unless trace_id && span_id

  ::Datadog::Context.new(trace_id: trace_id,
                         span_id: span_id,
                         sampling_priority: sampling_priority)
end

.inject!(context, env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ddtrace/distributed_tracing/headers/b3_single.rb', line 12

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

  # Header format:
  #   b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
  # https://github.com/apache/incubator-zipkin-b3-propagation/tree/7c6e9f14d6627832bd80baa87ac7dabee7be23cf#single-header
  # DEV: `{SamplingState}` and `{ParentSpanId`}` are optional

  # DEV: We need these to be hex encoded
  header = "#{context.trace_id.to_s(16)}-#{context.span_id.to_s(16)}"

  unless context.sampling_priority.nil?
    sampling_priority = DistributedTracing::Headers::Helpers.clamp_sampling_priority(context.sampling_priority)
    header += "-#{sampling_priority}"
  end

  env[B3_HEADER_SINGLE] = header
end