Class: OpenTelemetry::DistributedContext::Propagation::TextFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/distributed_context/propagation/text_format.rb

Overview

TextFormat is a formatter that injects and extracts a value as text into carriers that travel in-band across process boundaries. Encoding is expected to conform to the HTTP Header Field semantics. Values are often encoded as RPC/HTTP request headers.

The carrier of propagated data on both the client (injector) and server (extractor) side is usually an http request. Propagation is usually implemented via library-specific request interceptors, where the client-side injects values and the server-side extracts them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(traceparent_header_key:, tracestate_header_key:) ⇒ TextFormatter

Returns a new TextFormat that injects and extracts using the specified trace context header keys

Parameters:

  • traceparent_header_key (String)

    The traceparent header key used in the carrier

  • tracestate_header_key (String)

    The tracestate header key used in the carrier



31
32
33
34
35
# File 'lib/opentelemetry/distributed_context/propagation/text_format.rb', line 31

def initialize(traceparent_header_key:, tracestate_header_key:)
  @traceparent_header_key = traceparent_header_key
  @tracestate_header_key = tracestate_header_key
  @fields = [traceparent_header_key, tracestate_header_key].freeze
end

Instance Attribute Details

#fieldsObject (readonly)

Returns an array with the trace context header keys used by this formatter



23
24
25
# File 'lib/opentelemetry/distributed_context/propagation/text_format.rb', line 23

def fields
  @fields
end

Instance Method Details

#extract(carrier, &getter) {|Carrier, String| ... } ⇒ SpanContext

Return a remote Trace::SpanContext extracted from the supplied carrier. Expects the the supplied carrier to have keys in rack normalized format (HTTP_#UPPERCASE_KEY). Invalid headers will result in a new, valid, non-remote Trace::SpanContext.

Parameters:

  • carrier (Carrier)

    The carrier to get the header from.

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

Yields:

  • (Carrier, String)

    if an optional getter is provided, extract will yield the carrier and the header key to the getter.

Returns:

  • (SpanContext)

    the span context from the header, or a new one if parsing fails.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opentelemetry/distributed_context/propagation/text_format.rb', line 48

def extract(carrier, &getter)
  getter ||= DEFAULT_GETTER
  header = getter.call(carrier, @traceparent_header_key)
  tp = TraceParent.from_string(header)

  tracestate = getter.call(carrier, @tracestate_header_key)

  Trace::SpanContext.new(trace_id: tp.trace_id, span_id: tp.span_id, trace_flags: tp.flags, tracestate: tracestate, remote: true)
rescue OpenTelemetry::Error
  Trace::SpanContext.new
end

#inject(context, carrier, &setter) {|Carrier, String, String| ... } ⇒ Object

Set the span context on the supplied carrier.

Parameters:

  • context (SpanContext)

    The active Trace::SpanContext.

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



68
69
70
71
72
# File 'lib/opentelemetry/distributed_context/propagation/text_format.rb', line 68

def inject(context, carrier, &setter)
  setter ||= DEFAULT_SETTER
  setter.call(carrier, @traceparent_header_key, TraceParent.from_context(context).to_s)
  setter.call(carrier, @tracestate_header_key, context.tracestate) unless context.tracestate.nil?
end