Skip to content

Commit

Permalink
AWS ECS detector: add support for cloud.{account.id,availability_zone…
Browse files Browse the repository at this point in the history
…,region} (#233)

* feat: add support for cloud.{account.id,availability_zone,region} in AWS ECS detector with v4 metadata

* fix lint
  • Loading branch information
mmanciop authored Jan 29, 2024
1 parent f5f1a71 commit 3ed4a52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Aws/src/Ecs/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function __construct(
* If running on ECS with an ECS agent v1.4, the returned resource has additionally the following
* attributes as specified in https://opentelemetry.io/docs/reference/specification/resource/semantic_conventions/cloud_provider/aws/ecs/:
*
* - cloud.account.id => <account_id> , e.g., '111122223333'
* - cloud.availability_zone => <availability_zone> , e.g., 'us-east-1a'
* - cloud.region => <availability_zone> , e.g., 'us-east-1'
* - aws.ecs.container.arn
* - aws.ecs.cluster.arn
* - aws.ecs.launchtype
Expand Down Expand Up @@ -178,6 +181,13 @@ private function getMetadataEndpointV4Resource(): ResourceInfo

$clusterArn = null;
$taskArn = null;

$cloudResource = isset($taskMetadata['AvailabilityZone'])
? ResourceInfo::create(Attributes::create([
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => $taskMetadata['AvailabilityZone'],
]))
: ResourceInfo::emptyResource();

if (isset($taskMetadata['Cluster']) && isset($taskMetadata['TaskARN'])) {
$taskArn = $taskMetadata['TaskARN'];
$lastIndexOfColon = strrpos($taskArn, ':');
Expand All @@ -186,6 +196,14 @@ private function getMetadataEndpointV4Resource(): ResourceInfo
$cluster = $taskMetadata['Cluster'];
$clusterArn = strpos($cluster, 'arn:') === 0 ? $cluster : $baseArn . ':cluster/' . $cluster;
}

$arnParts = explode(':', $taskArn);
if (count($arnParts) > 5) {
$cloudResource = $cloudResource->merge(ResourceInfo::create(Attributes::create([
ResourceAttributes::CLOUD_ACCOUNT_ID => $arnParts[4],
ResourceAttributes::CLOUD_REGION => $arnParts[3],
])));
}
}

$containerArn = isset($containerMetadata['ContainerARN']) ? $containerMetadata['ContainerARN'] : null;
Expand Down Expand Up @@ -222,6 +240,6 @@ private function getMetadataEndpointV4Resource(): ResourceInfo
ResourceAttributes::AWS_ECS_TASK_REVISION => $taskRevision,
]));

return $ecsResource->merge($logResource);
return $ecsResource->merge($cloudResource)->merge($logResource);
}
}
9 changes: 9 additions & 0 deletions src/Aws/tests/Unit/Ecs/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ public function TestV4ResourceLaunchTypeEc2()
[
ResourceAttributes::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
ResourceAttributes::CLOUD_PLATFORM => self::CLOUD_PLATFORM,
ResourceAttributes::CLOUD_ACCOUNT_ID => '111122223333',
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => 'us-west-2d',
ResourceAttributes::CLOUD_REGION => 'us-west-2',
ResourceAttributes::AWS_ECS_CONTAINER_ARN => 'arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9',
ResourceAttributes::AWS_ECS_CLUSTER_ARN => 'arn:aws:ecs:us-west-2:111122223333:cluster/default',
ResourceAttributes::AWS_ECS_LAUNCHTYPE => 'ec2',
Expand Down Expand Up @@ -366,6 +369,9 @@ public function TestV4ResourceLaunchTypeFargate()
[
ResourceAttributes::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
ResourceAttributes::CLOUD_PLATFORM => self::CLOUD_PLATFORM,
ResourceAttributes::CLOUD_ACCOUNT_ID => '111122223333',
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => 'us-west-2a',
ResourceAttributes::CLOUD_REGION => 'us-west-2',
ResourceAttributes::AWS_ECS_CONTAINER_ARN => 'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1',
ResourceAttributes::AWS_ECS_CLUSTER_ARN => 'arn:aws:ecs:us-west-2:111122223333:cluster/default',
ResourceAttributes::AWS_ECS_LAUNCHTYPE => 'fargate',
Expand Down Expand Up @@ -409,6 +415,9 @@ public function TestV4ResourceLogDriverFireLens()
[
ResourceAttributes::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
ResourceAttributes::CLOUD_PLATFORM => self::CLOUD_PLATFORM,
ResourceAttributes::CLOUD_ACCOUNT_ID => '111122223333',
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => 'us-west-2a',
ResourceAttributes::CLOUD_REGION => 'us-west-2',
ResourceAttributes::AWS_ECS_CONTAINER_ARN => 'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1',
ResourceAttributes::AWS_ECS_CLUSTER_ARN => 'arn:aws:ecs:us-west-2:111122223333:cluster/default',
ResourceAttributes::AWS_ECS_LAUNCHTYPE => 'fargate',
Expand Down

0 comments on commit 3ed4a52

Please sign in to comment.