Skip to content

Commit a61739c

Browse files
authored
bugfix: AWS Lambda event source key incorrect for SNS in instrumenta… (#2612)
1 parent 41ca902 commit a61739c

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

CHANGELOG.md

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

88
## Unreleased
99

10+
- `opentelemetry-instrumentation-aws-lambda` Bugfix: AWS Lambda event source key incorrect for SNS in instrumentation library.
11+
([#2612](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2612))
12+
1013
### Added
1114

1215
- `opentelemetry-instrumentation-pyramid` Record exceptions raised when serving a request

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,11 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
306306
disable_aws_context_propagation,
307307
)
308308

309-
span_kind = None
310309
try:
311-
if lambda_event["Records"][0]["eventSource"] in {
310+
event_source = lambda_event["Records"][0].get(
311+
"eventSource"
312+
) or lambda_event["Records"][0].get("EventSource")
313+
if event_source in {
312314
"aws:sqs",
313315
"aws:s3",
314316
"aws:sns",

instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,43 @@ def test_lambda_handles_multiple_consumers(self):
349349

350350
mock_execute_lambda({"Records": [{"eventSource": "aws:sqs"}]})
351351
mock_execute_lambda({"Records": [{"eventSource": "aws:s3"}]})
352-
mock_execute_lambda({"Records": [{"eventSource": "aws:sns"}]})
352+
mock_execute_lambda({"Records": [{"EventSource": "aws:sns"}]})
353353
mock_execute_lambda({"Records": [{"eventSource": "aws:dynamodb"}]})
354354

355355
spans = self.memory_exporter.get_finished_spans()
356356

357357
assert spans
358+
assert len(spans) == 4
359+
360+
for span in spans:
361+
assert span.kind == SpanKind.CONSUMER
362+
363+
test_env_patch.stop()
364+
365+
def test_lambda_handles_invalid_event_source(self):
366+
test_env_patch = mock.patch.dict(
367+
"os.environ",
368+
{
369+
**os.environ,
370+
# NOT Active Tracing
371+
_X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED,
372+
# NOT using the X-Ray Propagator
373+
OTEL_PROPAGATORS: "tracecontext",
374+
},
375+
)
376+
test_env_patch.start()
377+
378+
AwsLambdaInstrumentor().instrument()
379+
380+
mock_execute_lambda({"Records": [{"eventSource": "invalid_source"}]})
381+
382+
spans = self.memory_exporter.get_finished_spans()
383+
384+
assert spans
385+
assert len(spans) == 1
386+
assert (
387+
spans[0].kind == SpanKind.SERVER
388+
) # Default to SERVER for unknown sources
358389

359390
test_env_patch.stop()
360391

0 commit comments

Comments
 (0)