Sha256: 3473431c46778be1830608bbaeaa116e07ef5f15c9cb0950a6f6bec4f5c81304

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

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

require 'cgi'

module OpenTelemetry
  module Baggage
    module Propagation
      # Injects baggage using the W3C Baggage format
      class TextMapInjector
        include Context::Propagation::DefaultSetter

        # Returns a new TextMapInjector that injects context using the specified
        # header key
        #
        # @param [String] baggage_key The baggage header
        #   key used in the carrier
        # @return [TextMapInjector]
        def initialize(baggage_key: 'baggage')
          @baggage_key = baggage_key
        end

        # Inject in-process baggage into the supplied carrier.
        #
        # @param [Carrier] carrier The carrier to inject baggage into
        # @param [Context] context The context to read baggage from
        # @param [optional Callable] getter An optional callable that takes a carrier and a key and
        #   returns the value associated with the key. If omitted the default getter will be used
        #   which expects the carrier to respond to [] and []=.
        # @yield [Carrier, String] if an optional getter is provided, inject will yield the carrier
        #   and the header key to the getter.
        # @return [Object] carrier with injected baggage
        def inject(carrier, context, &setter)
          return carrier unless (baggage = context[ContextKeys.baggage_key]) && !baggage.empty?

          setter ||= default_setter
          setter.call(carrier, @baggage_key, encode(baggage))

          carrier
        end

        private

        def encode(baggage)
          baggage.inject(+'') do |memo, (k, v)|
            memo << CGI.escape(k.to_s) << '=' << CGI.escape(v.to_s) << ','
          end.chop!
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opentelemetry-api-0.10.0 lib/opentelemetry/baggage/propagation/text_map_injector.rb
opentelemetry-api-0.9.0 lib/opentelemetry/baggage/propagation/text_map_injector.rb
opentelemetry-api-0.8.0 lib/opentelemetry/baggage/propagation/text_map_injector.rb
opentelemetry-api-0.7.0 lib/opentelemetry/baggage/propagation/text_map_injector.rb