Sha256: a6a81461b44f275b312d30c7088d9a8aa4c66c8f83f45b0ec65af3f31a9288ea

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# typed: true

require 'datadog/core/configuration'
require 'datadog/tracing/sampling/ext'

module Datadog
  module Tracing
    module Distributed
      # Helpers module provides common helper functions for distributed tracing headers
      module Helpers
        # Base provides common methods for distributed header helper classes
        def self.clamp_sampling_priority(sampling_priority)
          # B3 doesn't have our -1 (USER_REJECT) and 2 (USER_KEEP) priorities so convert to acceptable 0/1
          if sampling_priority < 0
            sampling_priority = Sampling::Ext::Priority::AUTO_REJECT
          elsif sampling_priority > 1
            sampling_priority = Sampling::Ext::Priority::AUTO_KEEP
          end

          sampling_priority
        end

        def self.truncate_base16_number(value)
          # Lowercase if we want to parse base16 e.g. 3E8 => 3e8
          # DEV: Ruby will parse `3E8` just fine, but to test
          #      `num.to_s(base) == value` we need to lowercase
          value = value.downcase

          # Truncate to trailing 16 characters if length is greater than 16
          # https://github.com/apache/incubator-zipkin/blob/21fe362899fef5c593370466bc5707d3837070c2/zipkin/src/main/java/zipkin2/storage/StorageComponent.java#L49-L53
          # DEV: This ensures we truncate B3 128-bit trace and span ids to 64-bit
          value = value[value.length - 16, 16] if value.length > 16

          # Remove any leading zeros
          # DEV: When we call `num.to_s(16)` later Ruby will not add leading zeros
          #      for us so we want to make sure the comparision will work as expected
          # DEV: regex, remove all leading zeros up until we find the last 0 in the string
          #      or we find the first non-zero, this allows `'0000' -> '0'` and `'00001' -> '1'`
          value.sub(/^0*(?=(0$)|[^0])/, '')
        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/helpers.rb
ddtrace-1.0.0 lib/datadog/tracing/distributed/helpers.rb
ddtrace-1.0.0.beta2 lib/datadog/tracing/distributed/helpers.rb