Sha256: b7b2384b563bfe52dd69c3bb5c5c0b572dfdb72756c3ca07417be53c5088c4d9

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

# typed: true

require_relative '../helpers'
require_relative 'parser'
require_relative '../headers/ext'
require_relative '../../trace_digest'

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

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

            # DEV: We need these to be hex encoded
            metadata[B3_HEADER_TRACE_ID] = digest.trace_id.to_s(16)
            metadata[B3_HEADER_SPAN_ID] = digest.span_id.to_s(16)

            if digest.trace_sampling_priority
              sampling_priority = Helpers.clamp_sampling_priority(
                digest.trace_sampling_priority
              )
              metadata[B3_HEADER_SAMPLED] = sampling_priority.to_s
            end

            metadata
          end

          def self.extract(metadata)
            # Extract values from gRPC metadata
            # DEV: B3 doesn't have "origin"
            metadata = Parser.new(metadata)
            trace_id = metadata.id(B3_HEADER_TRACE_ID, 16)
            span_id = metadata.id(B3_HEADER_SPAN_ID, 16)
            # We don't need to try and convert sampled since B3 supports 0/1 (AUTO_REJECT/AUTO_KEEP)
            sampling_priority = metadata.number(B3_HEADER_SAMPLED)

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ddtrace-1.6.1 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.6.0 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.5.2 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.5.1 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.5.0 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.4.2 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.4.1 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.4.0 lib/datadog/tracing/distributed/metadata/b3.rb
ddtrace-1.3.0 lib/datadog/tracing/distributed/metadata/b3.rb