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::Propagation.text_map_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 Getter) (defaults to: Context::Propagation.text_map_getter)

    If the optional getter is provided, it will be used to read the header from the carrier, otherwise the default getter will be used.

Returns:

  • (Context)

    a new context updated with state extracted from the carrier



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

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_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: Context::Propagation.text_map_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 Setter) (defaults to: Context::Propagation.text_map_setter)

    If the optional setter is provided, it will be used to write context into the carrier, otherwise the default setter will be used.

Returns:

  • (Object)

    carrier



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

def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_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