Sha256: 3caa5b0cfd98687fd7c911a1744ddca68be78efef1bcd9bcd6f5c572cda26bea

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

require 'ddtrace/span'
require 'ddtrace/ext/distributed'

module Datadog
  module OpenTracer
    # DistributedHeaders provides easy access and validation to headers
    class DistributedHeaders
      include Datadog::Ext::DistributedTracing

      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::Span::EXTERNAL_MAX_ID

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.51.0 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.50.0 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.49.0 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.48.0 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.47.0 lib/ddtrace/opentracer/distributed_headers.rb
ddtrace-0.46.0 lib/ddtrace/opentracer/distributed_headers.rb