Skip to content

Commit 7f8fe69

Browse files
Alex Botensrikanthccv
Alex Boten
andauthored
Adds an option to configure disable_aws_context_propagation by environment variable (#1507)
* Adds an option to configure `disable_aws_context_propagation` by environment variable The variable `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION` can be used to disable aws context propagation. This is similar to the proposed changes in the JS implementation: open-telemetry/opentelemetry-js-contrib#1227 Signed-off-by: Alex Boten <[email protected]> * update changelog * Apply suggestions from code review Co-authored-by: Srikanth Chekuri <[email protected]> Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent a9f3413 commit 7f8fe69

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by
11+
environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION`
12+
([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507))
13+
1014
## Version 1.15.0/0.36b0 (2022-12-10)
1115

1216
- Add uninstrument test for sqlalchemy

Diff for: instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def custom_event_context_extractor(lambda_event):
101101
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT = (
102102
"OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT"
103103
)
104+
OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION = (
105+
"OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION"
106+
)
104107

105108

106109
def _default_event_context_extractor(lambda_event: Any) -> Context:
@@ -408,6 +411,16 @@ def _instrument(self, **kwargs):
408411
flush_timeout_env,
409412
)
410413

414+
disable_aws_context_propagation = kwargs.get(
415+
"disable_aws_context_propagation", False
416+
) or os.getenv(
417+
OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION, "False"
418+
).strip().lower() in (
419+
"true",
420+
"1",
421+
"t",
422+
)
423+
411424
_instrument(
412425
self._wrapped_module_name,
413426
self._wrapped_function_name,
@@ -416,9 +429,7 @@ def _instrument(self, **kwargs):
416429
"event_context_extractor", _default_event_context_extractor
417430
),
418431
tracer_provider=kwargs.get("tracer_provider"),
419-
disable_aws_context_propagation=kwargs.get(
420-
"disable_aws_context_propagation", False
421-
),
432+
disable_aws_context_propagation=disable_aws_context_propagation,
422433
)
423434

424435
def _uninstrument(self, **kwargs):

Diff for: instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
_HANDLER,
2828
_X_AMZN_TRACE_ID,
2929
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT,
30+
OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION,
3031
AwsLambdaInstrumentor,
3132
)
3233
from opentelemetry.propagate import get_global_textmap
@@ -168,6 +169,7 @@ class TestCase:
168169
expected_state_value: str = None
169170
expected_trace_state_len: int = 0
170171
disable_aws_context_propagation: bool = False
172+
disable_aws_context_propagation_envvar: str = ""
171173

172174
def custom_event_context_extractor(lambda_event):
173175
return get_global_textmap().extract(lambda_event["foo"]["headers"])
@@ -238,6 +240,23 @@ def custom_event_context_extractor(lambda_event):
238240
expected_state_value=MOCK_W3C_TRACE_STATE_VALUE,
239241
xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED,
240242
),
243+
TestCase(
244+
name="no_custom_extractor_xray_disable_aws_propagation_via_env_var",
245+
custom_extractor=None,
246+
context={
247+
"headers": {
248+
TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED,
249+
TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2",
250+
}
251+
},
252+
disable_aws_context_propagation=False,
253+
disable_aws_context_propagation_envvar="true",
254+
expected_traceid=MOCK_W3C_TRACE_ID,
255+
expected_parentid=MOCK_W3C_PARENT_SPAN_ID,
256+
expected_trace_state_len=3,
257+
expected_state_value=MOCK_W3C_TRACE_STATE_VALUE,
258+
xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED,
259+
),
241260
]
242261
for test in tests:
243262
test_env_patch = mock.patch.dict(
@@ -246,6 +265,7 @@ def custom_event_context_extractor(lambda_event):
246265
**os.environ,
247266
# NOT Active Tracing
248267
_X_AMZN_TRACE_ID: test.xray_traceid,
268+
OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION: test.disable_aws_context_propagation_envvar,
249269
# NOT using the X-Ray Propagator
250270
OTEL_PROPAGATORS: "tracecontext",
251271
},

0 commit comments

Comments
 (0)