Sha256: 8adb8931879bb442692fd1ea6d9d9e7945436b2faf91b3066efc22be343d0422

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# typed: true

require 'datadog/tracing/span'
require 'datadog/tracing/distributed/headers/ext'

module Datadog
  module OpenTracer
    # DistributedHeaders provides easy access and validation to headers
    # @public_api
    class DistributedHeaders
      include Tracing::Distributed::Headers::Ext

      def initialize(carrier)
        @carrier = carrier
      end

      def valid?
        # Sampling priority is optional.
        !trace_id.nil? && !parent_id.nil?
      end

      def trace_id
        id HTTP_HEADER_TRACE_ID
      end

      def parent_id
        id HTTP_HEADER_PARENT_ID
      end

      def sampling_priority
        hdr = @carrier[HTTP_HEADER_SAMPLING_PRIORITY]
        # It's important to make a difference between no header,
        # and a header defined to zero.
        return unless hdr

        value = hdr.to_i
        return if value < 0

        value
      end

      def origin
        hdr = @carrier[HTTP_HEADER_ORIGIN]
        # Only return the value if it is not an empty string
        hdr if hdr != ''
      end

      private

      def id(header)
        value = @carrier[header].to_i
        return if value.zero? || value >= Datadog::Tracing::Span::EXTERNAL_MAX_ID

        value < 0 ? value + 0x1_0000_0000_0000_0000 : value
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ddtrace-1.2.0 lib/datadog/opentracer/distributed_headers.rb
ddtrace-1.1.0 lib/datadog/opentracer/distributed_headers.rb
ddtrace-1.0.0 lib/datadog/opentracer/distributed_headers.rb
ddtrace-1.0.0.beta2 lib/datadog/opentracer/distributed_headers.rb