Skip to content

Commit 3708604

Browse files
emdnetoxrmx
andauthored
botocore: sns set destination name attribute to arn and redact phone number (#3249)
* set destination name only if not phone number Signed-off-by: emdneto <[email protected]> * redact phone_number Signed-off-by: emdneto <[email protected]> * add changelog Signed-off-by: emdneto <[email protected]> --------- Signed-off-by: emdneto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent d5dce5d commit 3708604

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

CHANGELOG.md

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

4040
- `opentelemetry-instrumentation-redis` Add missing entry in doc string for `def _instrument`
4141
([#3247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3247))
42+
- `opentelemetry-instrumentation-botocore` sns-extension: Change destination name attribute
43+
to match topic ARN and redact phone number from attributes
44+
([#3249](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3249))
4245
- `opentelemetry-instrumentation-asyncpg` Fix fallback for empty queries.
4346
([#3253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3253))
4447
- `opentelemetry-instrumentation-threading` Fix broken context typehints

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sns.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,35 @@ def span_kind(cls) -> SpanKind:
7474
def extract_attributes(
7575
cls, call_context: _AwsSdkCallContext, attributes: _AttributeMapT
7676
):
77-
destination_name, is_phone_number = cls._extract_destination_name(
77+
span_name, destination_name = cls._extract_destination_name(
7878
call_context
7979
)
80+
81+
call_context.span_name = f"{span_name} send"
82+
8083
attributes[SpanAttributes.MESSAGING_DESTINATION_KIND] = (
8184
MessagingDestinationKindValues.TOPIC.value
8285
)
8386
attributes[SpanAttributes.MESSAGING_DESTINATION] = destination_name
84-
85-
# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when opentelemetry-semantic-conventions 0.42b0 is released
86-
attributes["messaging.destination.name"] = cls._extract_input_arn(
87-
call_context
88-
)
89-
call_context.span_name = (
90-
f"{'phone_number' if is_phone_number else destination_name} send"
87+
attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = (
88+
destination_name
9189
)
9290

9391
@classmethod
9492
def _extract_destination_name(
9593
cls, call_context: _AwsSdkCallContext
96-
) -> Tuple[str, bool]:
94+
) -> Tuple[str, str]:
9795
arn = cls._extract_input_arn(call_context)
9896
if arn:
99-
return arn.rsplit(":", 1)[-1], False
97+
return arn.rsplit(":", 1)[-1], arn
10098

10199
if cls._phone_arg_name:
102100
phone_number = call_context.params.get(cls._phone_arg_name)
103101
if phone_number:
104-
return phone_number, True
102+
# phone number redacted because it's a PII
103+
return "phone_number", "phone_number:**"
105104

106-
return "unknown", False
105+
return "unknown", "unknown"
107106

108107
@classmethod
109108
def _extract_input_arn(

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_sns.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def setUp(self):
4141
)
4242
self.client = session.create_client("sns", region_name="us-west-2")
4343
self.topic_name = "my-topic"
44+
self.topic_arn = (
45+
f"arn:aws:sns:us-west-2:123456789012:{self.topic_name}"
46+
)
4447

4548
def tearDown(self):
4649
super().tearDown()
@@ -115,14 +118,12 @@ def _test_publish_to_arn(self, arg_name: str):
115118
span.attributes[SpanAttributes.MESSAGING_DESTINATION_KIND],
116119
)
117120
self.assertEqual(
118-
self.topic_name,
121+
self.topic_arn,
119122
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
120123
)
121124
self.assertEqual(
122125
target_arn,
123-
# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when
124-
# opentelemetry-semantic-conventions 0.42b0 is released
125-
span.attributes["messaging.destination.name"],
126+
span.attributes[SpanAttributes.MESSAGING_DESTINATION_NAME],
126127
)
127128

128129
@mock_aws
@@ -135,7 +136,8 @@ def test_publish_to_phone_number(self):
135136

136137
span = self.assert_span("phone_number send")
137138
self.assertEqual(
138-
phone_number, span.attributes[SpanAttributes.MESSAGING_DESTINATION]
139+
"phone_number:**",
140+
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
139141
)
140142

141143
@mock_aws
@@ -187,14 +189,12 @@ def test_publish_batch_to_topic(self):
187189
span.attributes[SpanAttributes.MESSAGING_DESTINATION_KIND],
188190
)
189191
self.assertEqual(
190-
self.topic_name,
192+
topic_arn,
191193
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
192194
)
193195
self.assertEqual(
194196
topic_arn,
195-
# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when
196-
# opentelemetry-semantic-conventions 0.42b0 is released
197-
span.attributes["messaging.destination.name"],
197+
span.attributes[SpanAttributes.MESSAGING_DESTINATION_NAME],
198198
)
199199

200200
self.assert_injected_span(message1_attrs, span)

0 commit comments

Comments
 (0)