Skip to content

Commit 531ba2b

Browse files
mmancioplaurit
andauthored
AWS ECS Detector: set cloud.{account.id,availability_zone,region} (#1171)
Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 9dcefdd commit 531ba2b

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Resource providers
6+
7+
- Add support `cloud.account.id`, `cloud.availability_zone`, `cloud.region` and `cloud.resource_id`
8+
([#1171](https://github.com/open-telemetry/opentelemetry-java-contrib/pull/1171))
9+
510
## Version 1.33.0 (2024-02-21)
611

712
### Compressors

aws-resources/src/main/java/io/opentelemetry/contrib/aws/resource/EcsResource.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ static void fetchMetadata(
9999
}
100100
}
101101

102+
private static Optional<String> getAccountId(@Nullable String arn) {
103+
return getArnPart(arn, ArnPart.ACCOUNT);
104+
}
105+
106+
private static Optional<String> getRegion(@Nullable String arn) {
107+
return getArnPart(arn, ArnPart.REGION);
108+
}
109+
110+
private static enum ArnPart {
111+
REGION(3),
112+
ACCOUNT(4);
113+
114+
final int partIndex;
115+
116+
private ArnPart(int partIndex) {
117+
this.partIndex = partIndex;
118+
}
119+
}
120+
121+
private static Optional<String> getArnPart(@Nullable String arn, ArnPart arnPart) {
122+
if (arn == null) {
123+
return Optional.empty();
124+
}
125+
126+
String[] arnParts = arn.split(":");
127+
128+
if (arnPart.partIndex >= arnParts.length) {
129+
return Optional.empty();
130+
}
131+
132+
return Optional.of(arnParts[arnPart.partIndex]);
133+
}
134+
102135
// Suppression is required for CONTAINER_IMAGE_TAG until we are ready to upgrade.
103136
@SuppressWarnings("deprecation")
104137
static void parseResponse(
@@ -109,17 +142,27 @@ static void parseResponse(
109142
return;
110143
}
111144

145+
// Either the container ARN or the task ARN, they both contain the
146+
// account id and region tokens we need later for the cloud.account.id
147+
// and cloud.region attributes.
148+
String arn = null;
149+
112150
while (parser.nextToken() != JsonToken.END_OBJECT) {
113151
String value = parser.nextTextValue();
114152
switch (parser.currentName()) {
153+
case "AvailabilityZone":
154+
attrBuilders.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, value);
155+
break;
115156
case "DockerId":
116157
attrBuilders.put(ResourceAttributes.CONTAINER_ID, value);
117158
break;
118159
case "DockerName":
119160
attrBuilders.put(ResourceAttributes.CONTAINER_NAME, value);
120161
break;
121162
case "ContainerARN":
163+
arn = value;
122164
attrBuilders.put(ResourceAttributes.AWS_ECS_CONTAINER_ARN, value);
165+
attrBuilders.put(ResourceAttributes.CLOUD_RESOURCE_ID, value);
123166
logArnBuilder.setContainerArn(value);
124167
break;
125168
case "Image":
@@ -149,6 +192,7 @@ static void parseResponse(
149192
logArnBuilder.setRegion(value);
150193
break;
151194
case "TaskARN":
195+
arn = value;
152196
attrBuilders.put(ResourceAttributes.AWS_ECS_TASK_ARN, value);
153197
break;
154198
case "LaunchType":
@@ -165,6 +209,10 @@ static void parseResponse(
165209
break;
166210
}
167211
}
212+
213+
getRegion(arn).ifPresent(region -> attrBuilders.put(ResourceAttributes.CLOUD_REGION, region));
214+
getAccountId(arn)
215+
.ifPresent(accountId -> attrBuilders.put(ResourceAttributes.CLOUD_ACCOUNT_ID, accountId));
168216
}
169217

170218
private EcsResource() {}
@@ -196,9 +244,7 @@ void setLogStreamName(@Nullable String logStreamName) {
196244
}
197245

198246
void setContainerArn(@Nullable String containerArn) {
199-
if (containerArn != null) {
200-
account = containerArn.split(":")[4];
201-
}
247+
account = getAccountId(containerArn).orElse(null);
202248
}
203249

204250
Optional<String> getLogGroupArn() {

aws-resources/src/test/java/io/opentelemetry/contrib/aws/resource/EcsResourceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ void testCreateAttributesV3() throws IOException {
5353
.containsOnly(
5454
entry(ResourceAttributes.CLOUD_PROVIDER, "aws"),
5555
entry(ResourceAttributes.CLOUD_PLATFORM, "aws_ecs"),
56+
entry(ResourceAttributes.CLOUD_ACCOUNT_ID, "012345678910"),
57+
entry(ResourceAttributes.CLOUD_REGION, "us-east-2"),
58+
entry(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, "us-east-2b"),
5659
entry(ResourceAttributes.CONTAINER_NAME, "ecs-nginx-5-nginx-curl-ccccb9f49db0dfe0d901"),
5760
entry(
5861
ResourceAttributes.CONTAINER_ID,
@@ -89,6 +92,12 @@ void testCreateAttributesV4() throws IOException {
8992
.containsOnly(
9093
entry(ResourceAttributes.CLOUD_PROVIDER, "aws"),
9194
entry(ResourceAttributes.CLOUD_PLATFORM, "aws_ecs"),
95+
entry(ResourceAttributes.CLOUD_ACCOUNT_ID, "111122223333"),
96+
entry(ResourceAttributes.CLOUD_REGION, "us-west-2"),
97+
entry(
98+
ResourceAttributes.CLOUD_RESOURCE_ID,
99+
"arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9"),
100+
entry(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, "us-west-2d"),
92101
entry(ResourceAttributes.CONTAINER_NAME, "ecs-curltest-26-curl-cca48e8dcadd97805600"),
93102
entry(
94103
ResourceAttributes.CONTAINER_ID,

0 commit comments

Comments
 (0)