Sha256: f8adb540afd12d0f67a8951642107ca60d7b8fb13899786ea68fb92ada49992e

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Common
    module Propagation
      # The RackEnvGetter class provides a common methods for reading
      # keys from a rack environment. It abstracts away the rack-normalization
      # process so that keys can be looked up without having to transform them
      # first. With this class you can get +traceparent+ instead of
      # +HTTP_TRACEPARENT+
      class RackEnvGetter
        # Converts key into a rack-normalized key and reads it from the carrier.
        # Useful for extract operations.
        def get(carrier, key)
          carrier[to_rack_key(key)] || carrier[key]
        end

        # Reads all keys from a carrier and converts them from the rack-normalized
        # form to the original. The resulting keys will be lowercase and
        # underscores will be replaced with dashes.
        def keys(carrier)
          carrier.keys.map(&method(:from_rack_key))
        end

        private

        def to_rack_key(key)
          ret = 'HTTP_' + key
          ret.tr!('-', '_')
          ret.upcase!
          ret
        end

        def from_rack_key(key)
          start = key.start_with?('HTTP_') ? 5 : 0
          ret = key[start..-1]
          ret.tr!('_', '-')
          ret.downcase!
          ret
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opentelemetry-common-0.19.6 lib/opentelemetry/common/propagation/rack_env_getter.rb
opentelemetry-common-0.19.5 lib/opentelemetry/common/propagation/rack_env_getter.rb
opentelemetry-common-0.19.3 lib/opentelemetry/common/propagation/rack_env_getter.rb