Class: OpenTelemetry::Propagator::OTTrace::TextMapPropagator

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/propagator/ottrace/text_map_propagator.rb

Overview

Propagates context using OTTrace header format

Instance Method Summary collapse

Instance Method Details

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

Extract OTTrace context from the supplied carrier and set the active span in the given context. The original context will be returned if OTTrace cannot be extracted from the carrier.

Parameters:

  • carrier (Carrier)

    The carrier to get the header from.

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

    The context to be updated with extracted context

  • 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)

    Updated context with active span derived from the header, or the original context if parsing fails.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opentelemetry/propagator/ottrace/text_map_propagator.rb', line 49

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  trace_id = optionally_pad_trace_id(getter.get(carrier, TRACE_ID_HEADER))
  span_id = getter.get(carrier, SPAN_ID_HEADER)
  sampled = getter.get(carrier, SAMPLED_HEADER)

  return context unless valid?(trace_id: trace_id, span_id: span_id)

  span_context = Trace::SpanContext.new(
    trace_id: Array(trace_id).pack('H*'),
    span_id: Array(span_id).pack('H*'),
    trace_flags: as_trace_flags(sampled),
    remote: true
  )

  span = OpenTelemetry::Trace.non_recording_span(span_context)
  Trace.context_with_span(span, parent_context: set_baggage(carrier: carrier, context: context, getter: getter))
end

#fieldsArray<String>

Returns the predefined propagation fields. 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.



86
87
88
# File 'lib/opentelemetry/propagator/ottrace/text_map_propagator.rb', line 86

def fields
  FIELDS
end

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

Parameters:

  • carrier (Object)

    to update with context.

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

    The active Context.

  • 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.



72
73
74
75
76
77
78
79
80
# File 'lib/opentelemetry/propagator/ottrace/text_map_propagator.rb', line 72

def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
  span_context = Trace.current_span(context).context
  return unless span_context.valid?

  inject_span_context(span_context: span_context, carrier: carrier, setter: setter)
  inject_baggage(context: context, carrier: carrier, setter: setter)

  nil
end