Class: OpenTelemetry::Context::Propagation::CompositeTextMapPropagator

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

Overview

A composite text map propagator either composes a list of injectors and a list of extractors, or wraps a list of propagators, into a single interface exposing inject and extract methods. Injection and extraction will preserve the order of the injectors and extractors (or propagators) passed in during initialization.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(injectors: nil, extractors: nil, propagators: nil) ⇒ CompositeTextMapPropagator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CompositeTextMapPropagator.



44
45
46
47
48
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 44

def initialize(injectors: nil, extractors: nil, propagators: nil)
  @injectors = injectors
  @extractors = extractors
  @propagators = propagators
end

Class Method Details

.compose(injectors:, extractors:) ⇒ Object

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

Parameters:

  • injectors (Array<#inject, #fields>)

    An array of text map injectors

  • extractors (Array<#extract>)

    An array of text map extractors

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 24

def compose(injectors:, extractors:)
  raise ArgumentError, 'injectors and extractors must both be non-nil arrays' unless injectors.is_a?(Array) && extractors.is_a?(Array)

  new(injectors: injectors, extractors: extractors)
end

.compose_propagators(propagators) ⇒ Object

Returns a Propagator that extracts using the provided propagators.

Parameters:

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 34

def compose_propagators(propagators)
  raise ArgumentError, 'propagators must be a non-nil array' unless propagators.is_a?(Array)
  return NoopTextMapPropagator.new if propagators.empty?
  return propagators.first if propagators.size == 1

  new(propagators: propagators)
end

Instance Method Details

#extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter) ⇒ Context

Runs extractors or propagators 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



83
84
85
86
87
88
89
90
91
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 83

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  extractors = @extractors || @propagators
  extractors.inject(context) do |ctx, extractor|
    extractor.extract(carrier, context: ctx, getter: getter)
  rescue StandardError => e
    OpenTelemetry.logger.warn "Error in CompositePropagator#extract #{e.message}"
    ctx
  end
end

#fieldsArray<String>

Returns the union of the propagation fields returned by the composed injectors or propagators. If your carrier is reused, you should delete the fields returned by this method before calling inject.

Returns:

  • (Array<String>)

    a list of fields that will be used by this propagator.



98
99
100
101
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 98

def fields
  injectors = @injectors || @propagators
  injectors.flat_map(&:fields).uniq
end

#inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter) ⇒ Object

Runs injectors or propagators in order. If an injection fails a warning will be logged and remaining injectors will be executed.

Parameters:

  • carrier (Object)

    A mutable carrier to inject 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.



59
60
61
62
63
64
65
66
67
# File 'lib/opentelemetry/context/propagation/composite_text_map_propagator.rb', line 59

def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
  injectors = @injectors || @propagators
  injectors.each do |injector|
    injector.inject(carrier, context: context, setter: setter)
  rescue StandardError => e
    OpenTelemetry.logger.warn "Error in CompositePropagator#inject #{e.message}"
  end
  nil
end