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

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

Overview

Datadog provides helpers to inject or extract headers for Datadog 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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ddtrace/distributed_tracing/headers/datadog.rb', line 20

def self.extract(env)
  # Extract values from headers
  headers = Headers.new(env)
  trace_id = headers.id(HTTP_HEADER_TRACE_ID)
  parent_id = headers.id(HTTP_HEADER_PARENT_ID)
  origin = headers.header(HTTP_HEADER_ORIGIN)
  sampling_priority = headers.number(HTTP_HEADER_SAMPLING_PRIORITY)

  # Return early if this propagation is not valid
  # DEV: To be valid we need to have a trace id and a parent id or when it is a synthetics trace, just the trace id
  # DEV: `DistributedHeaders#id` will not return 0
  return unless (trace_id && parent_id) || (origin && trace_id)

  # Return new context
  ::Datadog::Context.new(trace_id: trace_id,
                         span_id: parent_id,
                         origin: origin,
                         sampling_priority: sampling_priority)
end

.inject!(context, env) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/ddtrace/distributed_tracing/headers/datadog.rb', line 11

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

  env[HTTP_HEADER_TRACE_ID] = context.trace_id.to_s
  env[HTTP_HEADER_PARENT_ID] = context.span_id.to_s
  env[HTTP_HEADER_SAMPLING_PRIORITY] = context.sampling_priority.to_s unless context.sampling_priority.nil?
  env[HTTP_HEADER_ORIGIN] = context.origin.to_s unless context.origin.nil?
end