Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS ECS Detector: set cloud.{account.id,availability_zone,region,resource_id} #1171

Merged
merged 8 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Resource providers

- Add support `cloud.account.id`, `cloud.availability_zone`, `cloud.region` and `cloud.resource_id`
([#1171](https://github.com/open-telemetry/opentelemetry-java-contrib/pull/1171))

## Version 1.33.0 (2024-02-21)

### Compressors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ static void fetchMetadata(
}
}

private static Optional<String> getAccountId(@Nullable String arn) {
return getArnPart(arn, ArnPart.ACCOUNT);
}

private static Optional<String> getRegion(@Nullable String arn) {
return getArnPart(arn, ArnPart.REGION);
}

private static enum ArnPart {
REGION(3),
ACCOUNT(4);

final int partIndex;

private ArnPart(int partIndex) {
this.partIndex = partIndex;
}
}

private static Optional<String> getArnPart(@Nullable String arn, ArnPart arnPart) {
if (arn == null) {
mmanciop marked this conversation as resolved.
Show resolved Hide resolved
return Optional.empty();
}

String[] arnParts = arn.split(":");

if (arnPart.partIndex >= arnParts.length) {
return Optional.empty();
}

return Optional.of(arnParts[arnPart.partIndex]);
}

// Suppression is required for CONTAINER_IMAGE_TAG until we are ready to upgrade.
@SuppressWarnings("deprecation")
static void parseResponse(
Expand All @@ -109,17 +142,27 @@ static void parseResponse(
return;
}

// Either the container ARN or the task ARN, they both contain the
// account id and region tokens we need later for the cloud.account.id
// and cloud.region attributes.
String arn = null;

while (parser.nextToken() != JsonToken.END_OBJECT) {
String value = parser.nextTextValue();
switch (parser.currentName()) {
case "AvailabilityZone":
attrBuilders.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, value);
break;
case "DockerId":
attrBuilders.put(ResourceAttributes.CONTAINER_ID, value);
break;
case "DockerName":
attrBuilders.put(ResourceAttributes.CONTAINER_NAME, value);
break;
case "ContainerARN":
arn = value;
attrBuilders.put(ResourceAttributes.AWS_ECS_CONTAINER_ARN, value);
attrBuilders.put(ResourceAttributes.CLOUD_RESOURCE_ID, value);
logArnBuilder.setContainerArn(value);
break;
case "Image":
Expand Down Expand Up @@ -149,6 +192,7 @@ static void parseResponse(
logArnBuilder.setRegion(value);
break;
case "TaskARN":
arn = value;
attrBuilders.put(ResourceAttributes.AWS_ECS_TASK_ARN, value);
break;
case "LaunchType":
Expand All @@ -165,6 +209,10 @@ static void parseResponse(
break;
}
}

getRegion(arn).ifPresent(region -> attrBuilders.put(ResourceAttributes.CLOUD_REGION, region));
getAccountId(arn)
.ifPresent(accountId -> attrBuilders.put(ResourceAttributes.CLOUD_ACCOUNT_ID, accountId));
}

private EcsResource() {}
Expand Down Expand Up @@ -196,9 +244,7 @@ void setLogStreamName(@Nullable String logStreamName) {
}

void setContainerArn(@Nullable String containerArn) {
if (containerArn != null) {
account = containerArn.split(":")[4];
}
account = getAccountId(containerArn).orElse(null);
}

Optional<String> getLogGroupArn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void testCreateAttributesV3() throws IOException {
.containsOnly(
entry(ResourceAttributes.CLOUD_PROVIDER, "aws"),
entry(ResourceAttributes.CLOUD_PLATFORM, "aws_ecs"),
entry(ResourceAttributes.CLOUD_ACCOUNT_ID, "012345678910"),
entry(ResourceAttributes.CLOUD_REGION, "us-east-2"),
entry(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, "us-east-2b"),
entry(ResourceAttributes.CONTAINER_NAME, "ecs-nginx-5-nginx-curl-ccccb9f49db0dfe0d901"),
entry(
ResourceAttributes.CONTAINER_ID,
Expand Down Expand Up @@ -89,6 +92,12 @@ void testCreateAttributesV4() throws IOException {
.containsOnly(
entry(ResourceAttributes.CLOUD_PROVIDER, "aws"),
entry(ResourceAttributes.CLOUD_PLATFORM, "aws_ecs"),
entry(ResourceAttributes.CLOUD_ACCOUNT_ID, "111122223333"),
entry(ResourceAttributes.CLOUD_REGION, "us-west-2"),
entry(
ResourceAttributes.CLOUD_RESOURCE_ID,
"arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9"),
entry(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, "us-west-2d"),
entry(ResourceAttributes.CONTAINER_NAME, "ecs-curltest-26-curl-cca48e8dcadd97805600"),
entry(
ResourceAttributes.CONTAINER_ID,
Expand Down
Loading