From 684084bf3ccbf9498bfc2589962f965da6089fd0 Mon Sep 17 00:00:00 2001 From: brett-bim Date: Thu, 17 Feb 2022 18:12:05 -0600 Subject: [PATCH 1/3] adding additional event sources --- .../opentelemetry/instrumentation/aws_lambda/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index 951f27dd44..e78cedbbb8 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -189,8 +189,14 @@ def _instrumented_lambda_handler_call( # See more: # https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html + # https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html + # https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html + # https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html + span_kind = SpanKind.SERVER + valid_consumers = ["aws:sqs", "aws:s3", "aws:sns", "aws:dynamodb"] + try: - if lambda_event["Records"][0]["eventSource"] == "aws:sqs": + if lambda_event["Records"][0]["eventSource"] in valid_consumers: span_kind = SpanKind.CONSUMER except (IndexError, KeyError, TypeError): span_kind = SpanKind.SERVER From aa25924fa7717eb94bccc4752729fd61c1e17460 Mon Sep 17 00:00:00 2001 From: brett-bim Date: Fri, 18 Feb 2022 12:30:30 -0600 Subject: [PATCH 2/3] updating CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c755d76f0..1a876e1e96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added + +- `opentelemetry-instrumentation-aws-lambda` Added additional consumers. Fixes issue 902 + ([#926](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/926)) + - `opentelemetry-instrumentation-dbapi` add experimental sql commenter capability ([#908](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/908)) From 0c0e8b2297393b6130d037063cd48fa14068a153 Mon Sep 17 00:00:00 2001 From: brett-bim Date: Tue, 1 Mar 2022 14:45:19 -0600 Subject: [PATCH 3/3] Cleaned up intent * Added test to prove change * Reduced some boilerplate --- CHANGELOG.md | 3 +-- .../instrumentation/aws_lambda/__init__.py | 20 +++++++------- .../test_aws_lambda_instrumentation_manual.py | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a876e1e96..60e85e9801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `opentelemetry-instrumentation-aws-lambda` Added additional consumers. Fixes issue 902 - ([#926](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/926)) +- `opentelemetry-instrumentation-aws-lambda` `SpanKind.SERVER` by default, add more cases for `SpanKind.CONSUMER` services. ([#926](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/926)) - `opentelemetry-instrumentation-dbapi` add experimental sql commenter capability ([#908](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/908)) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index e78cedbbb8..ad83418fbb 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -187,17 +187,19 @@ def _instrumented_lambda_handler_call( lambda_event, event_context_extractor ) - # See more: - # https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html - # https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html - # https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html - # https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html - span_kind = SpanKind.SERVER - valid_consumers = ["aws:sqs", "aws:s3", "aws:sns", "aws:dynamodb"] - + span_kind = None try: - if lambda_event["Records"][0]["eventSource"] in valid_consumers: + if lambda_event["Records"][0]["eventSource"] in set( + ["aws:sqs", "aws:s3", "aws:sns", "aws:dynamodb"] + ): + # See more: + # https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html + # https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html + # https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html + # https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html span_kind = SpanKind.CONSUMER + else: + span_kind = SpanKind.SERVER except (IndexError, KeyError, TypeError): span_kind = SpanKind.SERVER diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py index c2d7d71689..e463a09b47 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py @@ -274,3 +274,29 @@ def test_lambda_no_error_with_invalid_flush_timeout(self): self.assertEqual(len(spans), 1) test_env_patch.stop() + + def test_lambda_handles_multiple_consumers(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": "aws:sqs"}]}) + mock_execute_lambda({"Records": [{"eventSource": "aws:s3"}]}) + mock_execute_lambda({"Records": [{"eventSource": "aws:sns"}]}) + mock_execute_lambda({"Records": [{"eventSource": "aws:dynamodb"}]}) + + spans = self.memory_exporter.get_finished_spans() + + assert spans + + test_env_patch.stop()