Class: OpenTelemetry::Trace::Propagation::TraceContext::TextMapInjector

Inherits:
Object
  • Object
show all
Includes:
Context::Propagation::DefaultSetter
Defined in:
lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb

Overview

Injects context into carriers using the W3C Trace Context format

Instance Method Summary collapse

Methods included from Context::Propagation::DefaultSetter

#default_setter

Constructor Details

#initialize(traceparent_key: 'traceparent', tracestate_key: 'tracestate') ⇒ TextMapInjector

Returns a new TextMapInjector that injects context using the specified header keys

Parameters:

  • traceparent_key (String) (defaults to: 'traceparent')

    The traceparent header key used in the carrier

  • tracestate_key (String) (defaults to: 'tracestate')

    The tracestate header key used in the carrier



20
21
22
23
24
# File 'lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb', line 20

def initialize(traceparent_key: 'traceparent',
               tracestate_key: 'tracestate')
  @traceparent_key = traceparent_key
  @tracestate_key = tracestate_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
# File 'lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb', line 35

def inject(carrier, context, &setter)
  return carrier unless (span_context = span_context_from(context))

  setter ||= DEFAULT_SETTER
  setter.call(carrier, @traceparent_key, TraceParent.from_span_context(span_context).to_s)
  setter.call(carrier, @tracestate_key, span_context.tracestate.to_s) unless span_context.tracestate.empty?

  carrier
end