fix(lambda): align Python context propagation with JS implementation#727
Conversation
The custom_event_context_extractor was always using the X-Ray env var as parent context because _X_AMZN_TRACE_ID is always set and valid when active tracing is enabled, causing upstream W3C trace context (traceparent) from event headers to be ignored and breaking trace continuity. Align with the JS implementation: inject the X-Ray env var into the event headers and delegate to the global composite propagator, which respects propagator priority ordering (xray, then tracecontext).
|
|
||
| return lambda_trace_context | ||
| extracted_context = get_global_textmap().extract(headers) | ||
| if get_current_span(extracted_context).get_span_context(): |
There was a problem hiding this comment.
Bug: Dead code - condition is always truthy
get_current_span(extracted_context).get_span_context() always returns a SpanContext object (even INVALID_SPAN_CONTEXT). Since SpanContext does not define bool, any instance is truthy in Python. Line 76 (return Context()) is unreachable dead code.
Fix: use .is_valid:
if get_current_span(extracted_context).get_span_context().is_valid:
return extracted_context
return Context()
Or just return extracted_context if the fallback is unnecessary.
|
|
||
| return get_global_textmap().extract(headers) | ||
| if xray_env_var: | ||
| headers = {k: v for k, v in headers.items() if k.lower() != TRACE_HEADER_KEY.lower()} |
There was a problem hiding this comment.
Minor: TRACE_HEADER_KEY.lower() is recomputed on every iteration of the dict comprehension. Consider pre-computing into a local variable.
| assert spans | ||
| def test_parent_context_from_lambda_event(self): | ||
| original_propagator = get_global_textmap() | ||
| set_global_textmap(TraceContextTextMapPropagator()) |
There was a problem hiding this comment.
Minor (test gap): This test uses only TraceContextTextMapPropagator (no X-Ray support), so the injected X-Ray header is always ignored - making it functionally identical to test_xray_ignored_when_propagator_does_not_include_xray. Consider adding a test with a composite propagator and a passthrough X-Ray env var.
Closes #663
Summary
custom_event_context_extractorinotel_wrapper.pyalways used the X-Ray env var (_X_AMZN_TRACE_ID) as parent context because it is always set and valid when active tracing is enabled, causing upstream W3C trace context (traceparent) from event headers to be ignored and breaking trace continuity.get_global_textmap().extract(). This respects the propagator priority ordering configured inotel-instrument(baggage,xray,tracecontext), wheretracecontext(W3C) takes precedence overxraywhen both are present.test_xray_ignored_when_propagator_does_not_include_xray— X-Ray active tracing on, but onlytracecontextpropagator configured; W3C headers should be used.test_w3c_takes_precedence_over_xray_when_both_present— both X-Ray and W3C headers present with composite propagator; W3C should win.Test plan
pytest -v)otel_wrapper.pyat 90% (uncovered lines are error-handling edge cases)black,isort,flake8all clean