Class: OpenTelemetry::Propagator::Jaeger::TextMapPropagator

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

Overview

Propagates trace context using the Jaeger format

Instance Method Summary collapse

Instance Method Details

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

Extract trace context from the supplied carrier. If extraction fails, the original context will be returned

Parameters:

  • carrier (Carrier)

    The carrier to get the header from

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

    Context to be updated with the trace context 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 text map getter will be used.

Returns:

  • (Context)

    context updated with extracted baggage, or the original context if extraction fails



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opentelemetry/propagator/jaeger/text_map_propagator.rb', line 46

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  header = getter.get(carrier, IDENTITY_KEY)
  return context unless header
  return context unless (match = header.match(TRACE_SPAN_IDENTITY_REGEX))
  return context if match['trace_id'] =~ ZERO_ID_REGEX
  return context if match['span_id'] =~ ZERO_ID_REGEX

  sampling_flags = match['sampling_flags'].to_i
  span = build_span(match, sampling_flags)
  context = Jaeger.context_with_debug(context) if sampling_flags & DEBUG_FLAG_BIT != 0
  context = context_with_extracted_baggage(carrier, context, getter)
  Trace.context_with_span(span, parent_context: context)
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.



88
89
90
# File 'lib/opentelemetry/propagator/jaeger/text_map_propagator.rb', line 88

def fields
  FIELDS
end

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

Inject trace context into the supplied carrier.

Parameters:

  • carrier (Carrier)

    The mutable carrier to inject trace context into

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

    The context to read trace context from

  • 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 text map setter will be used.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/opentelemetry/propagator/jaeger/text_map_propagator.rb', line 67

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

  flags = to_jaeger_flags(context, span_context)
  trace_span_identity_value = [
    span_context.hex_trace_id, span_context.hex_span_id, '0', flags
  ].join(':')
  setter.set(carrier, IDENTITY_KEY, trace_span_identity_value)
  OpenTelemetry::Baggage.values(context: context).each do |key, value|
    baggage_key = 'uberctx-' + key
    encoded_value = CGI.escape(value)
    setter.set(carrier, baggage_key, encoded_value)
  end
  carrier
end