From 033ccb0809f044d8d6cf47588be8030d7bfd5c68 Mon Sep 17 00:00:00 2001 From: Michele Mancioppi Date: Wed, 14 Feb 2024 10:29:57 +0100 Subject: [PATCH] feat: add support for cloud.resource_id to AWS ECS detector --- .../README.md | 3 ++- .../src/detectors/AwsEcsDetector.ts | 3 +++ .../detectors/SemanticResourceAttributes.ts | 23 +++++++++++++++++++ .../test/detectors/AwsEcsDetector.test.ts | 6 +++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 detectors/node/opentelemetry-resource-detector-aws/src/detectors/SemanticResourceAttributes.ts diff --git a/detectors/node/opentelemetry-resource-detector-aws/README.md b/detectors/node/opentelemetry-resource-detector-aws/README.md index aeb075c237..e903be6fef 100644 --- a/detectors/node/opentelemetry-resource-detector-aws/README.md +++ b/detectors/node/opentelemetry-resource-detector-aws/README.md @@ -84,7 +84,8 @@ Populates `container` for containers running on [Amazon ECS](https://aws.amazon. | cloud.platform | The cloud platform. In this context, it's always "aws_ecs" | | cloud.provider | The cloud provider. In this context, it's always "aws" | | cloud.region | Parsed value from the `TaskARN` | -| container.id | Value of from file `/proc/self/cgroup` | +| cloud.resource_id | Value of `ContainerARN` from `ECS_CONTAINER_METADATA_URI_V4/task` request | +| container.id | Value from file `/proc/self/cgroup` | | container.name | The hostname of the operating system | ### AWS EKS Detector diff --git a/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts index e2e35af236..0c5a9f0624 100644 --- a/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts +++ b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts @@ -41,6 +41,8 @@ import { CLOUDPROVIDERVALUES_AWS, CLOUDPLATFORMVALUES_AWS_ECS, } from '@opentelemetry/semantic-conventions'; +// Patch until the OpenTelemetry SDK is updated to ship this attribute +import { SemanticResourceAttributes as AdditionalSemanticResourceAttributes } from './SemanticResourceAttributes'; import * as http from 'http'; import * as util from 'util'; import * as fs from 'fs'; @@ -170,6 +172,7 @@ export class AwsEcsDetector implements Detector { [SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId, [SEMRESATTRS_CLOUD_REGION]: region, + [AdditionalSemanticResourceAttributes.CLOUD_RESOURCE_ID]: containerArn, }; // The availability zone is not available in all Fargate runtimes diff --git a/detectors/node/opentelemetry-resource-detector-aws/src/detectors/SemanticResourceAttributes.ts b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/SemanticResourceAttributes.ts new file mode 100644 index 0000000000..24efaebd07 --- /dev/null +++ b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/SemanticResourceAttributes.ts @@ -0,0 +1,23 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export const SemanticResourceAttributes = { + /** + * Cloud provider-specific native identifier of the monitored cloud resource + * (e.g. an ARN on AWS, a fully qualified resource ID on Azure, a full resource + * name on GCP) + */ + CLOUD_RESOURCE_ID: 'cloud.resource_id', +}; diff --git a/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts b/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts index 6c9200b9cd..659b3afaab 100644 --- a/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts +++ b/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts @@ -42,6 +42,8 @@ import { CLOUDPROVIDERVALUES_AWS, CLOUDPLATFORMVALUES_AWS_ECS, } from '@opentelemetry/semantic-conventions'; +// Patch until the OpenTelemetry SDK is updated to ship this attribute +import { SemanticResourceAttributes as AdditionalSemanticResourceAttributes } from '../../src/detectors/SemanticResourceAttributes'; import { readFileSync } from 'fs'; import * as os from 'os'; import { join } from 'path'; @@ -81,6 +83,10 @@ const assertEcsResource = ( resource.attributes[SEMRESATTRS_AWS_ECS_CONTAINER_ARN], validations.containerArn ); + assert.strictEqual( + resource.attributes[AdditionalSemanticResourceAttributes.CLOUD_RESOURCE_ID], + validations.containerArn + ); if (validations.clusterArn) assert.strictEqual( resource.attributes[SEMRESATTRS_AWS_ECS_CLUSTER_ARN],