Skip to content

Commit

Permalink
Add AWS ECS cluster detection
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrara committed Jun 27, 2024
1 parent 852fa50 commit b64de7b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.contrib.aws.resource;

import static io.opentelemetry.contrib.aws.resource.IncubatingAttributes.AWS_ECS_CLUSTER_ARN;
import static io.opentelemetry.contrib.aws.resource.IncubatingAttributes.AWS_ECS_CONTAINER_ARN;
import static io.opentelemetry.contrib.aws.resource.IncubatingAttributes.AWS_ECS_LAUNCHTYPE;
import static io.opentelemetry.contrib.aws.resource.IncubatingAttributes.AWS_ECS_TASK_ARN;
Expand Down Expand Up @@ -163,6 +164,8 @@ static void parseResponse(
// account id and region tokens we need later for the cloud.account.id
// and cloud.region attributes.
String arn = null;
// Cluster can either be ARN or short name.
String cluster = null;

while (parser.nextToken() != JsonToken.END_OBJECT) {
String value = parser.nextTextValue();
Expand All @@ -176,6 +179,9 @@ static void parseResponse(
case "DockerName":
attrBuilders.put(CONTAINER_NAME, value);
break;
case "Cluster":
cluster = value;
break;
case "ContainerARN":
arn = value;
attrBuilders.put(AWS_ECS_CONTAINER_ARN, value);
Expand Down Expand Up @@ -229,8 +235,22 @@ static void parseResponse(
}
}

getRegion(arn).ifPresent(region -> attrBuilders.put(CLOUD_REGION, region));
getAccountId(arn).ifPresent(accountId -> attrBuilders.put(CLOUD_ACCOUNT_ID, accountId));
String region = getRegion(arn).orElse(null);
String account = getAccountId(arn).orElse(null);
if (region != null) {
attrBuilders.put(CLOUD_REGION, region);
}
if (account != null) {
attrBuilders.put(CLOUD_ACCOUNT_ID, account);
}
if (cluster != null) {
if (cluster.contains(":")) {
attrBuilders.put(AWS_ECS_CLUSTER_ARN, cluster);
} else {
String clusterArn = String.format("arn:aws:ecs:%s:%s:cluster/%s", region, account, cluster);
attrBuilders.put(AWS_ECS_CLUSTER_ARN, clusterArn);
}
}
}

private EcsResource() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ private CloudProviderValues() {}
public static final AttributeKey<String> K8S_CLUSTER_NAME =
AttributeKey.stringKey("k8s.cluster.name");

public static final AttributeKey<String> AWS_ECS_CLUSTER_ARN =
AttributeKey.stringKey("aws.ecs.cluster.arn");
public static final AttributeKey<String> AWS_ECS_CONTAINER_ARN =
AttributeKey.stringKey("aws.ecs.container.arn");
public static final AttributeKey<String> AWS_ECS_LAUNCHTYPE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.contrib.aws.resource;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.semconv.incubating.AwsIncubatingAttributes.AWS_ECS_CLUSTER_ARN;
import static io.opentelemetry.semconv.incubating.AwsIncubatingAttributes.AWS_ECS_CONTAINER_ARN;
import static io.opentelemetry.semconv.incubating.AwsIncubatingAttributes.AWS_ECS_LAUNCHTYPE;
import static io.opentelemetry.semconv.incubating.AwsIncubatingAttributes.AWS_ECS_TASK_ARN;
Expand Down Expand Up @@ -78,6 +79,7 @@ void testCreateAttributesV3() throws IOException {
entry(CONTAINER_ID, "43481a6ce4842eec8fe72fc28500c6b52edcc0917f105b83379f88cac1ff3946"),
entry(CONTAINER_IMAGE_NAME, "nrdlngr/nginx-curl"),
entry(io.opentelemetry.semconv.ResourceAttributes.CONTAINER_IMAGE_TAG, "latest"),
entry(AWS_ECS_CLUSTER_ARN, "arn:aws:ecs:us-east-2:012345678910:cluster/default"),
entry(
AttributeKey.stringKey("aws.ecs.container.image.id"),
"sha256:2e00ae64383cfc865ba0a2ba37f61b50a120d2d9378559dcd458dc0de47bc165"),
Expand Down Expand Up @@ -121,6 +123,7 @@ void testCreateAttributesV4() throws IOException {
entry(
AttributeKey.stringKey("aws.ecs.container.image.id"),
"sha256:d691691e9652791a60114e67b365688d20d19940dde7c4736ea30e660d8d3553"),
entry(AWS_ECS_CLUSTER_ARN, "arn:aws:ecs:us-west-2:111122223333:cluster/default"),
entry(
AWS_ECS_CONTAINER_ARN,
"arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9"),
Expand Down
2 changes: 1 addition & 1 deletion aws-resources/src/test/resources/ecs-task-metadata-v4.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Cluster": "default",
"Cluster": "arn:aws:ecs:us-west-2:111122223333:cluster/default",
"TaskARN": "arn:aws:ecs:us-west-2:111122223333:task/default/158d1c8083dd49d6b527399fd6414f5c",
"Family": "curltest",
"Revision": "26",
Expand Down

0 comments on commit b64de7b

Please sign in to comment.