Class: OpenTelemetry::DistributedContext::Propagation::TextFormat
- Inherits:
-
Object
- Object
- OpenTelemetry::DistributedContext::Propagation::TextFormat
- 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
-
#fields ⇒ Object
readonly
Returns an array with the trace context header keys used by this formatter.
Instance Method Summary collapse
-
#extract(carrier, &getter) {|Carrier, String| ... } ⇒ SpanContext
Return a remote Trace::SpanContext extracted from the supplied carrier.
-
#initialize(traceparent_header_key:, tracestate_header_key:) ⇒ TextFormatter
constructor
Returns a new TextFormat that injects and extracts using the specified trace context header keys.
-
#inject(context, carrier, &setter) {|Carrier, String, String| ... } ⇒ Object
Set the span context on the supplied carrier.
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
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
#fields ⇒ Object (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.
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.
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 |