Class: OpenTelemetry::Propagator::B3::Single::TextMapInjector

Inherits:
Object
  • Object
show all
Includes:
Context::Propagation::DefaultSetter
Defined in:
lib/opentelemetry/propagator/b3/single/text_map_injector.rb

Overview

Injects context into carriers using the b3 single header format

Instance Method Summary collapse

Constructor Details

#initialize(b3_key: 'b3') ⇒ TextMapInjector

Returns a new TextMapInjector that extracts b3 context using the specified header keys

Parameters:

  • b3_key (String) (defaults to: 'b3')

    The b3 header key used in the carrier



22
23
24
# File 'lib/opentelemetry/propagator/b3/single/text_map_injector.rb', line 22

def initialize(b3_key: 'b3')
  @b3_key = b3_key
end

Instance Method Details

#inject(carrier, context, &setter) {|Carrier, String, String| ... } ⇒ Object

Set the span context on the supplied carrier.

Parameters:

  • context (Context)

    The active Context.

  • setter (optional Callable)

    An optional callable that takes a carrier and a key and a value and assigns the key-value pair in the carrier. If omitted the default setter will be used which expects the carrier to respond to [] and []=.

Yields:

  • (Carrier, String, String)

    if an optional setter is provided, inject will yield carrier, header key, header value to the setter.

Returns:

  • (Object)

    the carrier with context injected



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/opentelemetry/propagator/b3/single/text_map_injector.rb', line 35

def inject(carrier, context, &setter)
  span_context = Trace.current_span(context).context
  return unless span_context.valid?

  sampling_state = if B3.debug?(context)
                     'd'
                   elsif span_context.trace_flags.sampled?
                     '1'
                   else
                     '0'
                   end

  b3_value = "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-#{sampling_state}"

  setter ||= default_setter
  setter.call(carrier, @b3_key, b3_value)
  carrier
end