Class: OpenTelemetry::Propagator::XRay::TextMapPropagator

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

Overview

Propagates context in carriers in the xray single header 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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/propagator/xray/text_map_propagator.rb', line 39

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  header = getter.get(carrier, XRAY_CONTEXT_KEY)
  match = parse_header(header)
  return context unless match

  span_context = Trace::SpanContext.new(
    trace_id: to_trace_id(match['trace_id']),
    span_id: to_span_id(match['span_id']),
    trace_flags: to_trace_flags(match['sampling_state']),
    tracestate: to_trace_state(match['trace_state']),
    remote: true
  )

  span = Trace::Span.new(span_context: span_context)
  context = XRay.context_with_debug(context) if match['sampling_state'] == 'd'
  Trace.context_with_span(span, parent_context: context)
rescue OpenTelemetry::Error
  context
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.



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

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

  sampling_state = if XRay.debug?(context)
                     'd'
                   elsif span_context.trace_flags.sampled?
                     '1'
                   else
                     '0'
                   end

  ot_trace_id = span_context.hex_trace_id
  xray_trace_id = "1-#{ot_trace_id[0..7]}-#{ot_trace_id[8..ot_trace_id.length]}"
  parent_id = span_context.hex_span_id

  xray_value = "Root=#{xray_trace_id};Parent=#{parent_id};Sampled=#{sampling_state}"

  setter.set(carrier, XRAY_CONTEXT_KEY, xray_value)
  nil
end