Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: AWS Lambda event source key incorrect for SNS in instrumenta… #2612

Merged
merged 8 commits into from
Jun 20, 2024
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

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

### Added

- `opentelemetry-sdk-extension-aws` Add AwsXrayLambdaPropagator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
disable_aws_context_propagation,
)

span_kind = None
try:
if lambda_event["Records"][0]["eventSource"] in {
event_source = lambda_event["Records"][0].get("eventSource") or lambda_event["Records"][0].get("EventSource")
if event_source in {
"aws:sqs",
"aws:s3",
"aws:sns",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,41 @@ def test_lambda_handles_multiple_consumers(self):

mock_execute_lambda({"Records": [{"eventSource": "aws:sqs"}]})
mock_execute_lambda({"Records": [{"eventSource": "aws:s3"}]})
mock_execute_lambda({"Records": [{"eventSource": "aws:sns"}]})
mock_execute_lambda({"Records": [{"EventSource": "aws:sns"}]})
mock_execute_lambda({"Records": [{"eventSource": "aws:dynamodb"}]})

spans = self.memory_exporter.get_finished_spans()

assert spans
assert len(spans) == 4

for span in spans:
assert span.kind == SpanKind.CONSUMER

test_env_patch.stop()

def test_lambda_handles_invalid_event_source(self):
test_env_patch = mock.patch.dict(
"os.environ",
{
**os.environ,
# NOT Active Tracing
_X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED,
# NOT using the X-Ray Propagator
OTEL_PROPAGATORS: "tracecontext",
},
)
test_env_patch.start()

AwsLambdaInstrumentor().instrument()

mock_execute_lambda({"Records": [{"eventSource": "invalid_source"}]})

spans = self.memory_exporter.get_finished_spans()

assert spans
assert len(spans) == 1
assert spans[0].kind == SpanKind.SERVER # Default to SERVER for unknown sources

test_env_patch.stop()

Expand Down
Loading