Skip to content

Commit b7d99dc

Browse files
seungleeleewantsui
andauthored
feat(aws): add partition tag (#14577)
<!-- * New contributors are highly encouraged to read our [CONTRIBUTING](/CONTRIBUTING.md) documentation. * Commit and PR titles should be prefixed with the general area of the pull request's change. --> ### What does this PR do? Adds a `aws.partition` tag onto AWS traces based on the region. The logic to derive this partition is [identical](https://github.com/DataDog/dd-trace-go/blob/main/contrib/aws/aws-sdk-go-v2/aws/aws.go#L206-L215) to what we do within other tracing libraries. ### Motivation Part of a larger effort to better link cloud resources within Datadog. We often need the AWS partition during downstream enrichment - it is almost always a required part of constructing an ARN, but it's currently not captured in the library [Jira Ticket](https://datadoghq.atlassian.net/browse/CLP-1021) and more [context](https://datadoghq.atlassian.net/wiki/x/toGyLwE) around this effort <!-- * What inspired you to submit this pull request? * Link any related GitHub issues or PRs here. * If this resolves a GitHub issue, include "Fixes #XXXX" to link the issue and auto-close it on merge. --> ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: wantsui <[email protected]>
1 parent 1e45596 commit b7d99dc

File tree

8 files changed

+37
-0
lines changed

8 files changed

+37
-0
lines changed

ddtrace/_trace/utils_botocore/span_tags.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def set_botocore_patched_api_call_span_tags(span: Span, instance, args, params,
9292
if region_name is not None:
9393
span.set_tag_str("aws.region", region_name)
9494
span.set_tag_str("region", region_name)
95+
span.set_tag_str("aws.partition", aws.get_aws_partition(region_name))
9596

9697
# Derive peer hostname only in serverless environments to avoid
9798
# unnecessary tag noise in traditional hosts/containers.

ddtrace/contrib/internal/aiobotocore/patch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ async def _wrapped_api_call(original_func, instance, args, kwargs):
160160
"aws.region": region_name,
161161
"region": region_name,
162162
}
163+
164+
if region_name:
165+
meta["aws.partition"] = aws.get_aws_partition(region_name)
166+
163167
span.set_tags(meta)
164168

165169
result = await original_func(*args, **kwargs)

ddtrace/contrib/internal/boto/patch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def patched_query_request(original_func, instance, args, kwargs):
124124
if region_name:
125125
meta[aws.REGION] = region_name
126126
meta[aws.AWSREGION] = region_name
127+
meta[aws.PARTITION] = aws.get_aws_partition(region_name)
127128

128129
if in_aws_lambda():
129130
# Derive the peer hostname now that we have both service and region.
@@ -192,6 +193,7 @@ def patched_auth_request(original_func, instance, args, kwargs):
192193
if region_name:
193194
meta[aws.REGION] = region_name
194195
meta[aws.AWSREGION] = region_name
196+
meta[aws.PARTITION] = aws.get_aws_partition(region_name)
195197

196198
if in_aws_lambda():
197199
# Derive the peer hostname

ddtrace/ext/aws.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ def _add_api_param_span_tags(span, endpoint_name, params):
8282
span.set_tag_str("statemachinearn", state_machine_arn)
8383

8484

85+
def get_aws_partition(region_name):
86+
# type: (str) -> str
87+
"""Determine AWS partition from region name."""
88+
if region_name.startswith("cn-"):
89+
return "aws-cn"
90+
elif region_name.startswith("us-gov-"):
91+
return "aws-us-gov"
92+
93+
return "aws"
94+
95+
8596
AWSREGION = "aws.region"
8697
REGION = "region"
98+
PARTITION = "aws.partition"
8799
AGENT = "aws.agent"
88100
OPERATION = "aws.operation"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
AWS: adds ``aws.partition`` tag onto AWS traces based on the region for the boto, botocore, and aiobotocore integrations.
5+

tests/contrib/aiobotocore/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async def test_traced_client(tracer):
3838
assert span.get_tag("aws.agent") == "aiobotocore"
3939
assert span.get_tag("aws.region") == "us-west-2"
4040
assert span.get_tag("region") == "us-west-2"
41+
assert span.get_tag("aws.partition") == "aws"
4142
assert span.get_tag("aws.operation") == "DescribeInstances"
4243
assert_span_http_status_code(span, 200)
4344
assert span.get_metric("retry_attempts") == 0
@@ -203,6 +204,7 @@ async def test_sqs_client(tracer):
203204
assert_is_measured(span)
204205
assert span.get_tag("aws.region") == "us-west-2"
205206
assert span.get_tag("region") == "us-west-2"
207+
assert span.get_tag("aws.partition") == "aws"
206208
assert span.get_tag("aws.operation") == "ListQueues"
207209
assert_span_http_status_code(span, 200)
208210
assert span.service == "aws.sqs"
@@ -225,6 +227,7 @@ async def test_kinesis_client(tracer):
225227
assert_is_measured(span)
226228
assert span.get_tag("aws.region") == "us-west-2"
227229
assert span.get_tag("region") == "us-west-2"
230+
assert span.get_tag("aws.partition") == "aws"
228231
assert span.get_tag("aws.operation") == "ListStreams"
229232
assert_span_http_status_code(span, 200)
230233
assert span.service == "aws.kinesis"

tests/contrib/boto/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_ec2_client(self):
5252
self.assertEqual(span.get_tag(http.METHOD), "POST")
5353
self.assertEqual(span.get_tag("aws.region"), "us-west-2")
5454
self.assertEqual(span.get_tag("region"), "us-west-2")
55+
self.assertEqual(span.get_tag("aws.partition"), "aws")
5556
self.assertEqual(span.get_tag("component"), "boto")
5657
self.assertEqual(span.get_tag("span.kind"), "client")
5758

@@ -67,6 +68,7 @@ def test_ec2_client(self):
6768
self.assertEqual(span.get_tag(http.METHOD), "POST")
6869
self.assertEqual(span.get_tag("aws.region"), "us-west-2")
6970
self.assertEqual(span.get_tag("region"), "us-west-2")
71+
self.assertEqual(span.get_tag("aws.partition"), "aws")
7072
self.assertEqual(span.get_tag("component"), "boto")
7173
self.assertEqual(span.get_tag("span.kind"), "client")
7274
self.assertEqual(span.service, "test-boto-tracing.ec2")
@@ -495,6 +497,7 @@ def test_lambda_client(self):
495497
self.assertEqual(span.get_tag(http.METHOD), "GET")
496498
self.assertEqual(span.get_tag("aws.region"), "us-east-2")
497499
self.assertEqual(span.get_tag("region"), "us-east-2")
500+
self.assertEqual(span.get_tag("aws.partition"), "aws")
498501
self.assertEqual(span.get_tag("aws.operation"), "list_functions")
499502
self.assertEqual(span.get_tag("component"), "boto")
500503
self.assertEqual(span.get_tag("span.kind"), "client")
@@ -612,6 +615,7 @@ def test_sts_client(self):
612615
assert_is_measured(span)
613616
self.assertEqual(span.get_tag("aws.region"), "us-west-2")
614617
self.assertEqual(span.get_tag("region"), "us-west-2")
618+
self.assertEqual(span.get_tag("aws.partition"), "aws")
615619
self.assertEqual(span.get_tag("aws.operation"), "GetFederationToken")
616620
self.assertEqual(span.get_tag("component"), "boto")
617621
self.assertEqual(span.get_tag("span.kind"), "client")
@@ -750,6 +754,7 @@ def test_elasticache_client(self):
750754
span = spans[0]
751755
self.assertEqual(span.get_tag("aws.region"), "us-west-2")
752756
self.assertEqual(span.get_tag("region"), "us-west-2")
757+
self.assertEqual(span.get_tag("aws.partition"), "aws")
753758
self.assertEqual(span.get_tag("component"), "boto")
754759
self.assertEqual(span.get_tag("span.kind"), "client")
755760
self.assertEqual(span.service, "test-boto-tracing.elasticache")
@@ -783,6 +788,7 @@ def test_ec2_client_ot(self):
783788
self.assertEqual(dd_span.get_tag(http.METHOD), "POST")
784789
self.assertEqual(dd_span.get_tag("aws.region"), "us-west-2")
785790
self.assertEqual(dd_span.get_tag("region"), "us-west-2")
791+
self.assertEqual(dd_span.get_tag("aws.partition"), "aws")
786792

787793
with ot_tracer.start_active_span("ot_span"):
788794
ec2.run_instances(21)
@@ -800,6 +806,7 @@ def test_ec2_client_ot(self):
800806
self.assertEqual(dd_span.get_tag(http.METHOD), "POST")
801807
self.assertEqual(dd_span.get_tag("aws.region"), "us-west-2")
802808
self.assertEqual(dd_span.get_tag("region"), "us-west-2")
809+
self.assertEqual(dd_span.get_tag("aws.partition"), "aws")
803810
self.assertEqual(dd_span.get_tag("component"), "boto")
804811
self.assertEqual(dd_span.get_tag("span.kind"), "client")
805812
self.assertEqual(dd_span.service, "test-boto-tracing.ec2")

tests/contrib/botocore/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def test_traced_client(self):
164164
assert span.get_tag("aws.agent") == "botocore"
165165
assert span.get_tag("aws.region") == "us-west-2"
166166
assert span.get_tag("region") == "us-west-2"
167+
assert span.get_tag("aws.partition") == "aws"
167168
assert span.get_tag("aws.operation") == "DescribeInstances"
168169
assert span.get_tag("aws.requestid") == "fdcdcab1-ae5c-489e-9c33-4637c5dda355"
169170
assert span.get_tag("component") == "botocore"
@@ -718,6 +719,7 @@ def _test_sqs_client(self):
718719
assert len(spans) == 1
719720
assert span.get_tag("aws.region") == "us-east-1"
720721
assert span.get_tag("region") == "us-east-1"
722+
assert span.get_tag("aws.partition") == "aws"
721723
assert span.get_tag("aws.operation") == "CreateQueue"
722724
assert span.get_tag("component") == "botocore"
723725
assert_is_measured(span)
@@ -771,6 +773,7 @@ def test_sqs_send_message_distributed_tracing_off(self):
771773
assert len(spans) == 1
772774
assert span.get_tag("aws.region") == "us-east-1"
773775
assert span.get_tag("region") == "us-east-1"
776+
assert span.get_tag("aws.partition") == "aws"
774777
assert span.get_tag("aws.operation") == "SendMessage"
775778
assert span.get_tag("params.MessageBody") is None
776779
assert span.get_tag("component") == "botocore"

0 commit comments

Comments
 (0)