Sha256: 0d2db5109c162e77257c0384b68f937b36b2dc29a361b9beb83f22d19f1e0a1a

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

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

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

    def initialize(env)
      @env = env
    end

    def valid?
      # Synthetics sends us `X-Datadog-Parent-Id: 0` which normally we would want
      # to filter out, but is ok in this context since there is no parent from Synthetics
      return true if origin == 'synthetics' && trace_id

      # Sampling priority and origin are optional.
      # DEV: We want to explicitly return true/false here
      trace_id && parent_id ? true : false
    end

    def trace_id
      id HTTP_HEADER_TRACE_ID
    end

    def parent_id
      id HTTP_HEADER_PARENT_ID
    end

    def sampling_priority
      hdr = header(HTTP_HEADER_SAMPLING_PRIORITY)

      # It's important to make a difference between no header,
      # and a header defined to zero.
      return if hdr.nil?

      # Convert header to an integer
      value = hdr.to_i

      # Ensure the parsed number is the same as the original string value
      # e.g. We want to make sure to throw away `'nan'.to_i == 0`
      # DEV: Ruby `.to_i` will return `0` if a number could not be parsed
      return unless value.to_s == hdr.to_s

      value
    end

    def origin
      hdr = header(HTTP_HEADER_ORIGIN)
      # Only return the value if it is not an empty string
      hdr if hdr != ''
    end

    private

    def header(name)
      rack_header = "http-#{name}".upcase!.tr('-', '_')

      @env[rack_header]
    end

    def id(header)
      value = header(header).to_i
      # Zero or greater than max allowed value of 2**64
      return if value.zero? || value > Span::EXTERNAL_MAX_ID
      value < 0 ? value + 0x1_0000_0000_0000_0000 : value
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ddtrace-0.23.3 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.23.2 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.23.1 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.23.0 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.22.0 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.21.2 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.21.1 lib/ddtrace/propagation/distributed_headers.rb
ddtrace-0.21.0 lib/ddtrace/propagation/distributed_headers.rb