Sha256: 57680a552b2843e553129a54a9e270afafaf2c1a2f79b5fcd65e9eae35023f00

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

# typed: true

require 'datadog/tracing/distributed/parser'
require 'datadog/tracing/distributed/headers/ext'
require 'datadog/tracing/trace_digest'

module Datadog
  module Tracing
    module Distributed
      module Headers
        # Datadog provides helpers to inject or extract headers for Datadog style headers
        module Datadog
          include Ext

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

            env[HTTP_HEADER_TRACE_ID] = digest.trace_id.to_s
            env[HTTP_HEADER_PARENT_ID] = digest.span_id.to_s
            env[HTTP_HEADER_SAMPLING_PRIORITY] = digest.trace_sampling_priority.to_s if digest.trace_sampling_priority
            env[HTTP_HEADER_ORIGIN] = digest.trace_origin.to_s unless digest.trace_origin.nil?

            env
          end

          def self.extract(env)
            # Extract values from headers
            headers = Parser.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: `Parser#id` will not return 0
            return unless (trace_id && parent_id) || (origin && trace_id)

            # Return new trace headers
            TraceDigest.new(
              span_id: parent_id,
              trace_id: trace_id,
              trace_origin: origin,
              trace_sampling_priority: sampling_priority
            )
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ddtrace-1.1.0 lib/datadog/tracing/distributed/headers/datadog.rb
ddtrace-1.0.0 lib/datadog/tracing/distributed/headers/datadog.rb
ddtrace-1.0.0.beta2 lib/datadog/tracing/distributed/headers/datadog.rb