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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ static void fetchMetadata(
}
}

@Nullable
private static String getAccountId(@Nullable String arn) {
return getArnPart(arn, 4);
}

@Nullable
private static String getRegion(@Nullable String arn) {
return getArnPart(arn, 3);
}

@Nullable
private static String getArnPart(@Nullable String arn, int partIndex) {
if (arn == null) {
mmanciop marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

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

if (partIndex < arnParts.length) {
return arnParts[partIndex];
}

return null;
}

static void parseResponse(
JsonParser parser, AttributesBuilder attrBuilders, LogArnBuilder logArnBuilder)
throws IOException {
Expand All @@ -107,16 +132,27 @@ static void parseResponse(
return;
}

String accountId = null;
String region = null;
while (parser.nextToken() != JsonToken.END_OBJECT) {
String value = parser.nextTextValue();
switch (parser.getCurrentName()) {
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":
if (accountId == null) {
accountId = getAccountId(value);
}
if (region == null) {
region = getRegion(value);
}
attrBuilders.put(ResourceAttributes.AWS_ECS_CONTAINER_ARN, value);
logArnBuilder.setContainerArn(value);
break;
Expand Down Expand Up @@ -146,6 +182,12 @@ static void parseResponse(
logArnBuilder.setRegion(value);
break;
case "TaskARN":
if (accountId == null) {
accountId = getAccountId(value);
}
if (region == null) {
region = getRegion(value);
}
mmanciop marked this conversation as resolved.
Show resolved Hide resolved
attrBuilders.put(ResourceAttributes.AWS_ECS_TASK_ARN, value);
break;
case "LaunchType":
Expand All @@ -162,6 +204,14 @@ static void parseResponse(
break;
}
}

if (accountId != null) {
attrBuilders.put(ResourceAttributes.CLOUD_ACCOUNT_ID, accountId);
}

if (region != null) {
attrBuilders.put(ResourceAttributes.CLOUD_REGION, region);
}
}

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

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

Optional<String> getLogGroupArn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -85,6 +88,9 @@ 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_AVAILABILITY_ZONE, "us-west-2d"),
entry(ResourceAttributes.CONTAINER_NAME, "ecs-curltest-26-curl-cca48e8dcadd97805600"),
entry(
ResourceAttributes.CONTAINER_ID,
Expand Down
Loading