Class: OpenTelemetry::Propagator::B3::Multi::TextMapInjector

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

Overview

Injects context into carriers using the b3 single header format

Instance Method Summary collapse

Constructor Details

#initialize(b3_trace_id_key: 'X-B3-TraceId', b3_span_id_key: 'X-B3-SpanId', b3_sampled_key: 'X-B3-Sampled', b3_flags_key: 'X-B3-Flags') ⇒ TextMapInjector

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

Parameters:

  • b3_trace_id_key (String) (defaults to: 'X-B3-TraceId')

    The b3 trace id key used in the carrier

  • b3_span_id_key (String) (defaults to: 'X-B3-SpanId')

    The b3 span id key used in the carrier

  • b3_sampled_key (String) (defaults to: 'X-B3-Sampled')

    The b3 sampled key used in the carrier

  • b3_flags_key (String) (defaults to: 'X-B3-Flags')

    The b3 flags key used in the carrier



25
26
27
28
29
30
31
32
33
# File 'lib/opentelemetry/propagator/b3/multi/text_map_injector.rb', line 25

def initialize(b3_trace_id_key: 'X-B3-TraceId',
               b3_span_id_key: 'X-B3-SpanId',
               b3_sampled_key: 'X-B3-Sampled',
               b3_flags_key: 'X-B3-Flags')
  @b3_trace_id_key = b3_trace_id_key
  @b3_span_id_key = b3_span_id_key
  @b3_sampled_key = b3_sampled_key
  @b3_flags_key = b3_flags_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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/opentelemetry/propagator/b3/multi/text_map_injector.rb', line 44

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

  setter ||= default_setter
  setter.call(carrier, @b3_trace_id_key, span_context.hex_trace_id)
  setter.call(carrier, @b3_span_id_key, span_context.hex_span_id)

  if B3.debug?(context)
    setter.call(carrier, @b3_flags_key, '1')
  elsif span_context.trace_flags.sampled?
    setter.call(carrier, @b3_sampled_key, '1')
  else
    setter.call(carrier, @b3_sampled_key, '0')
  end

  carrier
end