Skip to content

Commit

Permalink
Merge branch 'main' into add-dynamodb-spec-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
haddasbronfman authored Jun 27, 2023
2 parents d63f132 + de17f77 commit 6936aa3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/

import { diag } from '@opentelemetry/api';
import { Detector, Resource } from '@opentelemetry/resources';
import {
Detector,
Resource,
ResourceAttributes,
} from '@opentelemetry/resources';
import {
CloudProviderValues,
CloudPlatformValues,
Expand Down Expand Up @@ -128,14 +132,19 @@ export class AwsEcsDetector implements Detector {
const baseArn: string = taskArn.substring(0, taskArn.lastIndexOf(':'));
const cluster: string = taskMetadata['Cluster'];

const accountId: string = AwsEcsDetector._getAccountFromArn(taskArn);
const region: string = AwsEcsDetector._getRegionFromArn(taskArn);
const availabilityZone: string | undefined =
taskMetadata?.['AvailabilityZone'];

const clusterArn = cluster.startsWith('arn:')
? cluster
: `${baseArn}:cluster/${cluster}`;

const containerArn: string = containerMetadata['ContainerARN'];

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/semantic_conventions/resource/cloud_provider/aws/ecs.yaml
return new Resource({
// https://github.com/open-telemetry/semantic-conventions/blob/main/semantic_conventions/resource/cloud_provider/aws/ecs.yaml
const attributes: ResourceAttributes = {
[SemanticResourceAttributes.AWS_ECS_CONTAINER_ARN]: containerArn,
[SemanticResourceAttributes.AWS_ECS_CLUSTER_ARN]: clusterArn,
[SemanticResourceAttributes.AWS_ECS_LAUNCHTYPE]:
Expand All @@ -144,7 +153,18 @@ export class AwsEcsDetector implements Detector {
[SemanticResourceAttributes.AWS_ECS_TASK_FAMILY]: taskMetadata['Family'],
[SemanticResourceAttributes.AWS_ECS_TASK_REVISION]:
taskMetadata['Revision'],
});

[SemanticResourceAttributes.CLOUD_ACCOUNT_ID]: accountId,
[SemanticResourceAttributes.CLOUD_REGION]: region,
};

// The availability zone is not available in all Fargate runtimes
if (availabilityZone) {
attributes[SemanticResourceAttributes.CLOUD_AVAILABILITY_ZONE] =
availabilityZone;
}

return new Resource(attributes);
}

private static async _getLogResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import * as os from 'os';
import { join } from 'path';

interface EcsResourceAttributes {
readonly accountId?: string;
readonly region?: string;
readonly zone?: string;
readonly clusterArn?: string;
readonly containerArn?: string;
readonly launchType?: 'ec2' | 'fargate';
Expand All @@ -55,6 +58,9 @@ const assertEcsResource = (
) => {
assertCloudResource(resource, {
provider: CloudProviderValues.AWS,
accountId: validations.accountId,
region: validations.region,
zone: validations.zone,
});
assert.strictEqual(
resource.attributes[SemanticResourceAttributes.CLOUD_PLATFORM],
Expand Down Expand Up @@ -336,6 +342,9 @@ describe('AwsEcsResourceDetector', () => {
describe('on Fargate', () => {
describe('with AWS CloudWatch as log driver', () => {
generateLaunchTypeTests({
accountId: '111122223333',
region: 'us-west-2',
zone: 'us-west-2a',
clusterArn: 'arn:aws:ecs:us-west-2:111122223333:cluster/default',
containerArn:
'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1',
Expand Down

0 comments on commit 6936aa3

Please sign in to comment.