Class: OpenTelemetry::Propagator::XRay::LambdaTextMapPropagator

Inherits:
TextMapPropagator show all
Defined in:
lib/opentelemetry/propagator/xray/lambda_text_map_propagator.rb

Overview

Implementation of AWS X-Ray Trace Header propagation with special handling for Lambda's _X_AMZN_TRACE_ID environment variable

Constant Summary collapse

AWS_TRACE_HEADER_ENV_KEY =
'_X_AMZN_TRACE_ID'

Instance Method Summary collapse

Methods inherited from TextMapPropagator

#inject

Instance Method Details

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

Extract trace context from the supplied carrier or from Lambda environment variable 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



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

def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
  # Check if the original input context already has a valid span
  span_context = Trace.current_span(context).context
  # If original context is valid, just return it - do not extract from carrier
  return context if span_context.valid?

  # First try to extract from the carrier using the standard X-Ray propagator
  xray_context = super

  # Check if we successfully extracted a context from the carrier
  span_context = Trace.current_span(xray_context).context
  return xray_context if span_context.valid?

  # If not, check for the Lambda environment variable
  trace_header = ENV.fetch(AWS_TRACE_HEADER_ENV_KEY, nil)
  return xray_context unless trace_header

  # Create a carrier with the trace header and extract from it
  env_carrier = { XRAY_CONTEXT_KEY => trace_header }
  super(env_carrier, context: xray_context, getter: getter)
rescue OpenTelemetry::Error
  context
end