Class: OpenTelemetry::Context::Propagation::CompositePropagator

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/context/propagation/composite_propagator.rb

Overview

A composite propagator composes a list of injectors and extractors into single interface exposing inject and extract methods. Injection and extraction will preserve the order of the injectors and extractors passed in during initialization.

Instance Method Summary collapse

Constructor Details

#initialize(injectors, extractors) ⇒ CompositePropagator

Returns a Propagator that extracts using the provided extractors and injectors.

Parameters:



20
21
22
23
# File 'lib/opentelemetry/context/propagation/composite_propagator.rb', line 20

def initialize(injectors, extractors)
  @injectors = injectors
  @extractors = extractors
end

Instance Method Details

#extract(carrier, context = Context.current, &getter) ⇒ Context

Runs extractors in order and returns a Context updated with the results of each extraction. If an extraction fails, a warning will be logged and remaining extractors will continue to be executed. Always returns a valid context.

Parameters:

  • carrier (Object)

    The carrier to extract context from

  • context (optional Context) (defaults to: Context.current)

    Context to be updated with the state extracted from the carrier. Defaults to Context.current

  • getter (optional Callable)

    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 []=.

Returns:

  • (Context)

    a new context updated with state extracted from the carrier



62
63
64
65
66
67
68
69
# File 'lib/opentelemetry/context/propagation/composite_propagator.rb', line 62

def extract(carrier, context = Context.current, &getter)
  @extractors.inject(context) do |ctx, extractor|
    extractor.extract(carrier, ctx, &getter)
  rescue => e # rubocop:disable Style/RescueStandardError
    OpenTelemetry.logger.warn "Error in CompositePropagator#extract #{e.message}"
    ctx
  end
end

#inject(carrier, context = Context.current, &setter) ⇒ Object

Runs injectors in order and returns a carrier. If an injection fails a warning will be logged and remaining injectors will be executed. Always returns a valid carrier.

Parameters:

  • carrier (Object)

    A carrier to inject context into context into

  • context (optional Context) (defaults to: Context.current)

    Context to be injected into carrier. Defaults to Context.current

  • setter (optional Callable)

    An optional callable that takes a carrier, 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 []=.

Returns:

  • (Object)

    carrier



39
40
41
42
43
44
45
46
# File 'lib/opentelemetry/context/propagation/composite_propagator.rb', line 39

def inject(carrier, context = Context.current, &setter)
  @injectors.inject(carrier) do |memo, injector|
    injector.inject(memo, context, &setter)
  rescue => e # rubocop:disable Style/RescueStandardError
    OpenTelemetry.logger.warn "Error in CompositePropagator#inject #{e.message}"
    carrier
  end
end