From b739af273c768ce7e26cd5aa4494dd3f95066d6d Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Fri, 7 Feb 2025 08:45:46 -0300 Subject: [PATCH 1/3] set destination name only if not phone number Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- .../instrumentation/botocore/extensions/sns.py | 8 ++++---- tox.ini | 5 ----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py index 9c3df3a2bc..227ee9dc22 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py @@ -81,10 +81,10 @@ def extract_attributes( ) attributes[SpanAttributes.MESSAGING_DESTINATION] = destination_name - # TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when opentelemetry-semantic-conventions 0.42b0 is released - attributes["messaging.destination.name"] = cls._extract_input_arn( - call_context - ) + if not is_phone_number: + attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = ( + cls._extract_input_arn(call_context) + ) call_context.span_name = ( f"{'phone_number' if is_phone_number else destination_name} send" ) diff --git a/tox.ini b/tox.ini index b45b6b4d4c..c12cf0a681 100644 --- a/tox.ini +++ b/tox.ini @@ -421,11 +421,6 @@ test_deps = opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils -pass_env = - AWS_ACCESS_KEY_ID - AWS_SECRET_ACCESS_KEY - AWS_SESSION_TOKEN - AWS_DEFAULT_REGION deps = lint: -r dev-requirements.txt From 26f20634bfb06be741e4e75121d39e94df69ca7f Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:35:34 -0300 Subject: [PATCH 2/3] redact phone_number Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- .../botocore/extensions/sns.py | 23 +++++++++---------- .../tests/test_botocore_sns.py | 18 +++++++-------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py index 227ee9dc22..db0811e227 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py @@ -73,36 +73,35 @@ def span_kind(cls) -> SpanKind: def extract_attributes( cls, call_context: _AwsSdkCallContext, attributes: _AttributeMapT ): - destination_name, is_phone_number = cls._extract_destination_name( + span_name, destination_name = cls._extract_destination_name( call_context ) + + call_context.span_name = f"{span_name} send" + attributes[SpanAttributes.MESSAGING_DESTINATION_KIND] = ( MessagingDestinationKindValues.TOPIC.value ) attributes[SpanAttributes.MESSAGING_DESTINATION] = destination_name - - if not is_phone_number: - attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = ( - cls._extract_input_arn(call_context) - ) - call_context.span_name = ( - f"{'phone_number' if is_phone_number else destination_name} send" + attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = ( + destination_name ) @classmethod def _extract_destination_name( cls, call_context: _AwsSdkCallContext - ) -> Tuple[str, bool]: + ) -> Tuple[str, str]: arn = cls._extract_input_arn(call_context) if arn: - return arn.rsplit(":", 1)[-1], False + return arn.rsplit(":", 1)[-1], arn if cls._phone_arg_name: phone_number = call_context.params.get(cls._phone_arg_name) if phone_number: - return phone_number, True + # phone number redacted because it's a PII + return "phone_number", "phone_number:**" - return "unknown", False + return "unknown", "unknown" @classmethod def _extract_input_arn( diff --git a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_sns.py b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_sns.py index 5d6b94f145..38bdfc28a3 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_sns.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_sns.py @@ -41,6 +41,9 @@ def setUp(self): ) self.client = session.create_client("sns", region_name="us-west-2") self.topic_name = "my-topic" + self.topic_arn = ( + f"arn:aws:sns:us-west-2:123456789012:{self.topic_name}" + ) def tearDown(self): super().tearDown() @@ -115,14 +118,12 @@ def _test_publish_to_arn(self, arg_name: str): span.attributes[SpanAttributes.MESSAGING_DESTINATION_KIND], ) self.assertEqual( - self.topic_name, + self.topic_arn, span.attributes[SpanAttributes.MESSAGING_DESTINATION], ) self.assertEqual( target_arn, - # TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when - # opentelemetry-semantic-conventions 0.42b0 is released - span.attributes["messaging.destination.name"], + span.attributes[SpanAttributes.MESSAGING_DESTINATION_NAME], ) @mock_aws @@ -135,7 +136,8 @@ def test_publish_to_phone_number(self): span = self.assert_span("phone_number send") self.assertEqual( - phone_number, span.attributes[SpanAttributes.MESSAGING_DESTINATION] + "phone_number:**", + span.attributes[SpanAttributes.MESSAGING_DESTINATION], ) @mock_aws @@ -187,14 +189,12 @@ def test_publish_batch_to_topic(self): span.attributes[SpanAttributes.MESSAGING_DESTINATION_KIND], ) self.assertEqual( - self.topic_name, + topic_arn, span.attributes[SpanAttributes.MESSAGING_DESTINATION], ) self.assertEqual( topic_arn, - # TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when - # opentelemetry-semantic-conventions 0.42b0 is released - span.attributes["messaging.destination.name"], + span.attributes[SpanAttributes.MESSAGING_DESTINATION_NAME], ) self.assert_injected_span(message1_attrs, span) From 242425933f692fc2f8b19fd8bef17cce1e57a28f Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:52:45 -0300 Subject: [PATCH 3/3] add changelog Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d0ce4370a..7e83d1d3ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Fixed + - `opentelemetry-instrumentation-redis` Add missing entry in doc string for `def _instrument` ([#3247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3247)) +- `opentelemetry-instrumentation-botocore` sns-extension: Change destination name attribute + to match topic ARN and redact phone number from attributes + ([#3249](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3249)) ## Version 1.30.0/0.51b0 (2025-02-03)