From 84a29f2a1d8f358d03b7f6da284ca5f1e42b9eb6 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 9 Feb 2023 17:24:49 -0700 Subject: [PATCH 1/9] update awslambda to use _X_AMZN_TRACE_ID as a Span Link --- .../instrumentation/aws_lambda/__init__.py | 54 ++++--- .../test_aws_lambda_instrumentation_manual.py | 132 +++++++++--------- 2 files changed, 103 insertions(+), 83 deletions(-) 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 4404839c66..d09b00e529 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 @@ -71,7 +71,7 @@ def custom_event_context_extractor(lambda_event): import os import time from importlib import import_module -from typing import Any, Callable, Collection +from typing import Any, Callable, Collection, Optional, Sequence from urllib.parse import urlencode from wrapt import wrap_function_wrapper @@ -90,9 +90,11 @@ def custom_event_context_extractor(lambda_event): from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import ( + Link, Span, SpanKind, TracerProvider, + get_current_span, get_tracer, get_tracer_provider, ) @@ -149,7 +151,7 @@ def _determine_parent_context( """Determine the parent context for the current Lambda invocation. See more: - https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#determining-the-parent-of-a-span + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md Args: lambda_event: user-defined, so it could be anything, but this @@ -166,28 +168,45 @@ def _determine_parent_context( """ parent_context = None + if event_context_extractor: + parent_context = event_context_extractor(lambda_event) + else: + parent_context = _default_event_context_extractor(lambda_event) + + return parent_context + + +def _determine_links( + disable_aws_context_propagation: bool = False, +) -> Optional[Sequence[Link]]: + """Determine if a Link should be added to the Span based on the + environment variable `_X_AMZN_TRACE_ID`. + + + See more: + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md + + Args: + disable_aws_context_propagation: By default, this instrumentation + will try to read the context from the `_X_AMZN_TRACE_ID` environment + variable set by Lambda, set this to `True` to disable this behavior. + Returns: + A Link or None + """ + env_context = None + links = None + if not disable_aws_context_propagation: xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) if xray_env_var: - parent_context = AwsXRayPropagator().extract( + env_context = AwsXRayPropagator().extract( {TRACE_HEADER_KEY: xray_env_var} ) - if ( - parent_context - and get_current_span(parent_context) - .get_span_context() - .trace_flags.sampled - ): - return parent_context - - if event_context_extractor: - parent_context = event_context_extractor(lambda_event) - else: - parent_context = _default_event_context_extractor(lambda_event) + links = [Link(get_current_span(env_context).get_span_context())] - return parent_context + return links def _set_api_gateway_v1_proxy_attributes( @@ -294,6 +313,8 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches disable_aws_context_propagation, ) + links = _determine_links(disable_aws_context_propagation) + span_kind = None try: if lambda_event["Records"][0]["eventSource"] in { @@ -319,6 +340,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches name=orig_handler_name, context=parent_context, kind=span_kind, + links=links, ) as span: if span.is_recording(): lambda_context = args[1] 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 1df7499d31..710f604dbc 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 @@ -138,7 +138,9 @@ def test_active_tracing(self): self.assertEqual(len(spans), 1) span = spans[0] self.assertEqual(span.name, os.environ[_HANDLER]) - self.assertEqual(span.get_span_context().trace_id, MOCK_XRAY_TRACE_ID) + self.assertNotEqual( + span.get_span_context().trace_id, MOCK_XRAY_TRACE_ID + ) self.assertEqual(span.kind, SpanKind.SERVER) self.assertSpanHasAttributes( span, @@ -149,11 +151,7 @@ def test_active_tracing(self): ) parent_context = span.parent - self.assertEqual( - parent_context.trace_id, span.get_span_context().trace_id - ) - self.assertEqual(parent_context.span_id, MOCK_XRAY_PARENT_SPAN_ID) - self.assertTrue(parent_context.is_remote) + self.assertEqual(None, parent_context) test_env_patch.stop() @@ -165,11 +163,8 @@ class TestCase: context: Dict expected_traceid: int expected_parentid: int - xray_traceid: str expected_state_value: str = None expected_trace_state_len: int = 0 - disable_aws_context_propagation: bool = False - disable_aws_context_propagation_envvar: str = "" def custom_event_context_extractor(lambda_event): return get_global_textmap().extract(lambda_event["foo"]["headers"]) @@ -188,42 +183,9 @@ def custom_event_context_extractor(lambda_event): expected_parentid=MOCK_W3C_PARENT_SPAN_ID, expected_trace_state_len=3, expected_state_value=MOCK_W3C_TRACE_STATE_VALUE, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, - ), - TestCase( - name="custom_extractor_not_sampled_xray", - custom_extractor=custom_event_context_extractor, - context={ - "foo": { - "headers": { - TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED, - TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2", - } - } - }, - expected_traceid=MOCK_W3C_TRACE_ID, - expected_parentid=MOCK_W3C_PARENT_SPAN_ID, - expected_trace_state_len=3, - expected_state_value=MOCK_W3C_TRACE_STATE_VALUE, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, - ), - TestCase( - name="custom_extractor_sampled_xray", - custom_extractor=custom_event_context_extractor, - context={ - "foo": { - "headers": { - TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED, - TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2", - } - } - }, - expected_traceid=MOCK_XRAY_TRACE_ID, - expected_parentid=MOCK_XRAY_PARENT_SPAN_ID, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED, ), TestCase( - name="custom_extractor_sampled_xray_disable_aws_propagation", + name="custom_extractor", custom_extractor=custom_event_context_extractor, context={ "foo": { @@ -233,29 +195,10 @@ def custom_event_context_extractor(lambda_event): } } }, - disable_aws_context_propagation=True, - expected_traceid=MOCK_W3C_TRACE_ID, - expected_parentid=MOCK_W3C_PARENT_SPAN_ID, - expected_trace_state_len=3, - expected_state_value=MOCK_W3C_TRACE_STATE_VALUE, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED, - ), - TestCase( - name="no_custom_extractor_xray_disable_aws_propagation_via_env_var", - custom_extractor=None, - context={ - "headers": { - TraceContextTextMapPropagator._TRACEPARENT_HEADER_NAME: MOCK_W3C_TRACE_CONTEXT_SAMPLED, - TraceContextTextMapPropagator._TRACESTATE_HEADER_NAME: f"{MOCK_W3C_TRACE_STATE_KEY}={MOCK_W3C_TRACE_STATE_VALUE},foo=1,bar=2", - } - }, - disable_aws_context_propagation=False, - disable_aws_context_propagation_envvar="true", expected_traceid=MOCK_W3C_TRACE_ID, expected_parentid=MOCK_W3C_PARENT_SPAN_ID, expected_trace_state_len=3, expected_state_value=MOCK_W3C_TRACE_STATE_VALUE, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED, ), ] for test in tests: @@ -263,17 +206,13 @@ def custom_event_context_extractor(lambda_event): "os.environ", { **os.environ, - # NOT Active Tracing - _X_AMZN_TRACE_ID: test.xray_traceid, - OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION: test.disable_aws_context_propagation_envvar, # NOT using the X-Ray Propagator OTEL_PROPAGATORS: "tracecontext", }, ) test_env_patch.start() AwsLambdaInstrumentor().instrument( - event_context_extractor=test.custom_extractor, - disable_aws_context_propagation=test.disable_aws_context_propagation, + event_context_extractor=test.custom_extractor ) mock_execute_lambda(test.context) spans = self.memory_exporter.get_finished_spans() @@ -301,6 +240,65 @@ def custom_event_context_extractor(lambda_event): AwsLambdaInstrumentor().uninstrument() test_env_patch.stop() + def test_links_from_lambda_event(self): + @dataclass + class TestCase: + name: str + context: Dict + expected_link_trace_id: int + xray_traceid: str + disable_aws_context_propagation: bool = False + disable_aws_context_propagation_envvar: str = "" + + tests = [ + TestCase( + name="enabled_aws_context_propagation", + context={}, + expected_link_trace_id=MOCK_XRAY_TRACE_ID, + xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, + ), + TestCase( + name="disable_aws_context_propagation", + context={}, + expected_link_trace_id=None, + xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, + disable_aws_context_propagation=True, + ), + ] + for test in tests: + test_env_patch = mock.patch.dict( + "os.environ", + { + **os.environ, + # NOT Active Tracing + _X_AMZN_TRACE_ID: test.xray_traceid, + OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION: test.disable_aws_context_propagation_envvar, + # NOT using the X-Ray Propagator + OTEL_PROPAGATORS: "tracecontext", + }, + ) + test_env_patch.start() + AwsLambdaInstrumentor().instrument( + disable_aws_context_propagation=test.disable_aws_context_propagation, + ) + mock_execute_lambda(test.context) + spans = self.memory_exporter.get_finished_spans() + assert spans + self.assertEqual(len(spans), 1) + span = spans[0] + + if test.expected_link_trace_id == None: + self.assertEqual(0, len(span.links)) + else: + link = span.links[0] + self.assertEqual( + link.context.trace_id, test.expected_link_trace_id + ) + + self.memory_exporter.clear() + AwsLambdaInstrumentor().uninstrument() + test_env_patch.stop() + def test_lambda_no_error_with_invalid_flush_timeout(self): test_env_patch = mock.patch.dict( "os.environ", From 9e047e495e31132fe62b22767df1e3eab37a1fdd Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 10 Feb 2023 04:50:47 -0700 Subject: [PATCH 2/9] update changelog for aws-lambda change for Span link from env --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85bb0e5a65..4a86797eab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1592](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1592)) - `opentelemetry-instrumentation-django` Allow explicit `excluded_urls` configuration through `instrument()` ([#1618](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1618)) +- `opentelemetry-instrumentation-aws-lambda` Use env var `_X_AMZN_TRACE_ID` as a + Span Link instead of parent + ([#1657](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1657)) ### Fixed From a9680b35209e8261e222b7aa4cb9cf339763ea34 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 10 Feb 2023 14:34:30 -0700 Subject: [PATCH 3/9] awslambda: remove unneeded disable_aws_context_propagation option --- .../instrumentation/aws_lambda/__init__.py | 54 +++++-------------- .../test_aws_lambda_instrumentation_manual.py | 17 ++---- 2 files changed, 17 insertions(+), 54 deletions(-) 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 d09b00e529..54e06accfd 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 @@ -108,9 +108,6 @@ def custom_event_context_extractor(lambda_event): OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT = ( "OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT" ) -OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION = ( - "OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION" -) def _default_event_context_extractor(lambda_event: Any) -> Context: @@ -144,9 +141,7 @@ def _default_event_context_extractor(lambda_event: Any) -> Context: def _determine_parent_context( - lambda_event: Any, - event_context_extractor: Callable[[Any], Context], - disable_aws_context_propagation: bool = False, + lambda_event: Any, event_context_extractor: Callable[[Any], Context] ) -> Context: """Determine the parent context for the current Lambda invocation. @@ -160,9 +155,6 @@ def _determine_parent_context( Event as input and extracts an OTel Context from it. By default, the context is extracted from the HTTP headers of an API Gateway request. - disable_aws_context_propagation: By default, this instrumentation - will try to read the context from the `_X_AMZN_TRACE_ID` environment - variable set by Lambda, set this to `True` to disable this behavior. Returns: A Context with configuration found in the carrier. """ @@ -176,9 +168,7 @@ def _determine_parent_context( return parent_context -def _determine_links( - disable_aws_context_propagation: bool = False, -) -> Optional[Sequence[Link]]: +def _determine_links() -> Optional[Sequence[Link]]: """Determine if a Link should be added to the Span based on the environment variable `_X_AMZN_TRACE_ID`. @@ -186,25 +176,22 @@ def _determine_links( See more: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md - Args: - disable_aws_context_propagation: By default, this instrumentation - will try to read the context from the `_X_AMZN_TRACE_ID` environment - variable set by Lambda, set this to `True` to disable this behavior. Returns: A Link or None """ env_context = None links = None - if not disable_aws_context_propagation: - xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) + xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) - if xray_env_var: - env_context = AwsXRayPropagator().extract( - {TRACE_HEADER_KEY: xray_env_var} - ) + if xray_env_var: + env_context = AwsXRayPropagator().extract( + {TRACE_HEADER_KEY: xray_env_var} + ) - links = [Link(get_current_span(env_context).get_span_context())] + span_context = get_current_span(env_context).get_span_context() + if span_context.is_valid: + links = [Link(span_context)] return links @@ -295,7 +282,6 @@ def _instrument( flush_timeout, event_context_extractor: Callable[[Any], Context], tracer_provider: TracerProvider = None, - disable_aws_context_propagation: bool = False, meter_provider: MeterProvider = None, ): def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches @@ -308,12 +294,10 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches lambda_event = args[0] parent_context = _determine_parent_context( - lambda_event, - event_context_extractor, - disable_aws_context_propagation, + lambda_event, event_context_extractor ) - links = _determine_links(disable_aws_context_propagation) + links = _determine_links() span_kind = None try: @@ -434,9 +418,6 @@ def _instrument(self, **kwargs): Event as input and extracts an OTel Context from it. By default, the context is extracted from the HTTP headers of an API Gateway request. - ``disable_aws_context_propagation``: By default, this instrumentation - will try to read the context from the `_X_AMZN_TRACE_ID` environment - variable set by Lambda, set this to `True` to disable this behavior. """ lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER)) # pylint: disable=attribute-defined-outside-init @@ -458,16 +439,6 @@ def _instrument(self, **kwargs): flush_timeout_env, ) - disable_aws_context_propagation = kwargs.get( - "disable_aws_context_propagation", False - ) or os.getenv( - OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION, "False" - ).strip().lower() in ( - "true", - "1", - "t", - ) - _instrument( self._wrapped_module_name, self._wrapped_function_name, @@ -476,7 +447,6 @@ def _instrument(self, **kwargs): "event_context_extractor", _default_event_context_extractor ), tracer_provider=kwargs.get("tracer_provider"), - disable_aws_context_propagation=disable_aws_context_propagation, meter_provider=kwargs.get("meter_provider"), ) 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 710f604dbc..1db3378a29 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 @@ -27,7 +27,6 @@ _HANDLER, _X_AMZN_TRACE_ID, OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, - OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION, AwsLambdaInstrumentor, ) from opentelemetry.propagate import get_global_textmap @@ -247,22 +246,19 @@ class TestCase: context: Dict expected_link_trace_id: int xray_traceid: str - disable_aws_context_propagation: bool = False - disable_aws_context_propagation_envvar: str = "" tests = [ TestCase( - name="enabled_aws_context_propagation", + name="valid_xray_trace", context={}, expected_link_trace_id=MOCK_XRAY_TRACE_ID, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, + xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED, ), TestCase( - name="disable_aws_context_propagation", + name="invalid_xray_trace", context={}, expected_link_trace_id=None, - xray_traceid=MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED, - disable_aws_context_propagation=True, + xray_traceid=f"0", ), ] for test in tests: @@ -272,15 +268,12 @@ class TestCase: **os.environ, # NOT Active Tracing _X_AMZN_TRACE_ID: test.xray_traceid, - OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION: test.disable_aws_context_propagation_envvar, # NOT using the X-Ray Propagator OTEL_PROPAGATORS: "tracecontext", }, ) test_env_patch.start() - AwsLambdaInstrumentor().instrument( - disable_aws_context_propagation=test.disable_aws_context_propagation, - ) + AwsLambdaInstrumentor().instrument() mock_execute_lambda(test.context) spans = self.memory_exporter.get_finished_spans() assert spans From 72a38ff67eeecbbfcb100cd8bd6e05d03a22e611 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 17 Feb 2023 11:49:21 -0700 Subject: [PATCH 4/9] Update instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py Co-authored-by: Ron Yishai --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 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 54e06accfd..93cff37069 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 @@ -174,7 +174,7 @@ def _determine_links() -> Optional[Sequence[Link]]: See more: - https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#aws-x-ray-environment-span-link Returns: A Link or None From 4773c719d8f5c79f63f8701ca3df77bbe4f8c95c Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 17 Feb 2023 11:49:30 -0700 Subject: [PATCH 5/9] Update instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py Co-authored-by: Ron Yishai --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 1 - 1 file changed, 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 93cff37069..920c32c6c2 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 @@ -179,7 +179,6 @@ def _determine_links() -> Optional[Sequence[Link]]: Returns: A Link or None """ - env_context = None links = None xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) From 916376b6f9ccef9f52a5759171b6b929d813f3ee Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Wed, 22 Feb 2023 17:40:37 -0700 Subject: [PATCH 6/9] lambda: include xray source attribute on Link from _X_AMZN_TRACE_ID --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 2 +- .../tests/test_aws_lambda_instrumentation_manual.py | 5 +++++ 2 files changed, 6 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 920c32c6c2..8cd48ca786 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 @@ -190,7 +190,7 @@ def _determine_links() -> Optional[Sequence[Link]]: span_context = get_current_span(env_context).get_span_context() if span_context.is_valid: - links = [Link(span_context)] + links = [Link(span_context, {"source": "x-ray-env"})] return links 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 1db3378a29..cd9c50ec68 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 @@ -252,12 +252,14 @@ class TestCase: name="valid_xray_trace", context={}, expected_link_trace_id=MOCK_XRAY_TRACE_ID, + expected_link_attributes={"source": "x-ray-env"}, xray_traceid=MOCK_XRAY_TRACE_CONTEXT_SAMPLED, ), TestCase( name="invalid_xray_trace", context={}, expected_link_trace_id=None, + expected_link_attributes={}, xray_traceid=f"0", ), ] @@ -287,6 +289,9 @@ class TestCase: self.assertEqual( link.context.trace_id, test.expected_link_trace_id ) + self.assertEqual( + link.attributes, test.expected_link_attributes + ) self.memory_exporter.clear() AwsLambdaInstrumentor().uninstrument() From 301101d9d0a71b58bd70d7c67c70975c149a803b Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 24 Apr 2023 18:36:25 -0600 Subject: [PATCH 7/9] remove unneeded tracer get_current_span import --- .../src/opentelemetry/instrumentation/aws_lambda/__init__.py | 1 - 1 file changed, 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 8cd48ca786..12f90393d4 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 @@ -94,7 +94,6 @@ def custom_event_context_extractor(lambda_event): Span, SpanKind, TracerProvider, - get_current_span, get_tracer, get_tracer_provider, ) From db854c3144f6acafef6e2a15cb4f2e208c598c1d Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 17 Jul 2023 17:36:36 +0200 Subject: [PATCH 8/9] Fix lint --- .../tests/test_aws_lambda_instrumentation_manual.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 cd9c50ec68..5a8af225f9 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 @@ -260,7 +260,7 @@ class TestCase: context={}, expected_link_trace_id=None, expected_link_attributes={}, - xray_traceid=f"0", + xray_traceid="0", ), ] for test in tests: @@ -282,7 +282,7 @@ class TestCase: self.assertEqual(len(spans), 1) span = spans[0] - if test.expected_link_trace_id == None: + if test.expected_link_trace_id is None: self.assertEqual(0, len(span.links)) else: link = span.links[0] From aea4f71a851b5b0827397693f823931ce194e1d4 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 17 Jul 2023 17:55:02 +0200 Subject: [PATCH 9/9] Fix test case --- .../tests/test_aws_lambda_instrumentation_manual.py | 1 + tox.ini | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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 5a8af225f9..544022086c 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 @@ -245,6 +245,7 @@ class TestCase: name: str context: Dict expected_link_trace_id: int + expected_link_attributes: dict xray_traceid: str tests = [ diff --git a/tox.ini b/tox.ini index 18078536c1..8496b0943c 100644 --- a/tox.ini +++ b/tox.ini @@ -34,7 +34,7 @@ envlist = ; instrumentation-aiopg intentionally excluded from pypy3 ; opentelemetry-instrumentation-aws-lambda - py3{7,8,9}-test-instrumentation-aws-lambda + py3{7,8,9,10,11}-test-instrumentation-aws-lambda ; opentelemetry-instrumentation-botocore py3{7,8,9,10,11}-test-instrumentation-botocore