Skip to content

Commit 714eeca

Browse files
committed
chore(resource-detector-aws): use exported strings for attributes
Use exported strings for Semantic Resource Attributes, Cloud Platform Values and Cloud Provider Values. Signed-off-by: maryliag <[email protected]>
1 parent 85cbc8d commit 714eeca

File tree

10 files changed

+133
-91
lines changed

10 files changed

+133
-91
lines changed

detectors/node/opentelemetry-resource-detector-aws/CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@
6666
* devDependencies
6767
* @opentelemetry/contrib-test-utils bumped from ^0.35.1 to ^0.36.0
6868

69+
### Dependencies
70+
71+
* The following workspace dependencies were updated
72+
* dependencies
73+
* @opentelemetry/semantic-conventions bumped from ^1.0.0 to ^1.22.0
74+
75+
### Enhancement
76+
77+
* refactor: use exported strings for Semantic Resource Attributes, Cloud Platform Values and Cloud Provider Values
78+
6979
## [1.4.0](https://github.com/open-telemetry/opentelemetry-js-contrib/compare/resource-detector-aws-v1.3.6...resource-detector-aws-v1.4.0) (2024-03-06)
7080

7181

detectors/node/opentelemetry-resource-detector-aws/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"dependencies": {
5858
"@opentelemetry/core": "^1.0.0",
5959
"@opentelemetry/resources": "^1.0.0",
60-
"@opentelemetry/semantic-conventions": "^1.0.0"
60+
"@opentelemetry/semantic-conventions": "^1.22.0"
6161
},
6262
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-aws#readme"
6363
}

detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsBeanstalkDetector.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ import {
2121
ResourceDetectionConfig,
2222
} from '@opentelemetry/resources';
2323
import {
24-
CloudProviderValues,
25-
CloudPlatformValues,
26-
SemanticResourceAttributes,
24+
SEMRESATTRS_CLOUD_PROVIDER,
25+
SEMRESATTRS_CLOUD_PLATFORM,
26+
SEMRESATTRS_SERVICE_NAME,
27+
SEMRESATTRS_SERVICE_NAMESPACE,
28+
SEMRESATTRS_SERVICE_VERSION,
29+
SEMRESATTRS_SERVICE_INSTANCE_ID,
30+
CLOUDPROVIDERVALUES_AWS,
31+
CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
2732
} from '@opentelemetry/semantic-conventions';
2833
import * as fs from 'fs';
2934
import * as util from 'util';
@@ -69,16 +74,12 @@ export class AwsBeanstalkDetector implements Detector {
6974
const parsedData = JSON.parse(rawData);
7075

7176
return new Resource({
72-
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS,
73-
[SemanticResourceAttributes.CLOUD_PLATFORM]:
74-
CloudPlatformValues.AWS_ELASTIC_BEANSTALK,
75-
[SemanticResourceAttributes.SERVICE_NAME]:
76-
CloudPlatformValues.AWS_ELASTIC_BEANSTALK,
77-
[SemanticResourceAttributes.SERVICE_NAMESPACE]:
78-
parsedData.environment_name,
79-
[SemanticResourceAttributes.SERVICE_VERSION]: parsedData.version_label,
80-
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]:
81-
parsedData.deployment_id,
77+
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS,
78+
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
79+
[SEMRESATTRS_SERVICE_NAME]: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
80+
[SEMRESATTRS_SERVICE_NAMESPACE]: parsedData.environment_name,
81+
[SEMRESATTRS_SERVICE_VERSION]: parsedData.version_label,
82+
[SEMRESATTRS_SERVICE_INSTANCE_ID]: parsedData.deployment_id,
8283
});
8384
} catch (e: any) {
8485
diag.debug(`AwsBeanstalkDetector failed: ${e.message}`);

detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEc2Detector.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ import {
2020
ResourceDetectionConfig,
2121
} from '@opentelemetry/resources';
2222
import {
23-
CloudProviderValues,
24-
CloudPlatformValues,
25-
SemanticResourceAttributes,
23+
SEMRESATTRS_CLOUD_PROVIDER,
24+
SEMRESATTRS_CLOUD_PLATFORM,
25+
SEMRESATTRS_CLOUD_REGION,
26+
SEMRESATTRS_CLOUD_ACCOUNT_ID,
27+
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
28+
SEMRESATTRS_HOST_ID,
29+
SEMRESATTRS_HOST_TYPE,
30+
SEMRESATTRS_HOST_NAME,
31+
CLOUDPROVIDERVALUES_AWS,
32+
CLOUDPLATFORMVALUES_AWS_EC2,
2633
} from '@opentelemetry/semantic-conventions';
2734
import * as http from 'http';
2835

@@ -62,14 +69,14 @@ class AwsEc2Detector implements Detector {
6269
const hostname = await this._fetchHost(token);
6370

6471
return new Resource({
65-
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS,
66-
[SemanticResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AWS_EC2,
67-
[SemanticResourceAttributes.CLOUD_ACCOUNT_ID]: accountId,
68-
[SemanticResourceAttributes.CLOUD_REGION]: region,
69-
[SemanticResourceAttributes.CLOUD_AVAILABILITY_ZONE]: availabilityZone,
70-
[SemanticResourceAttributes.HOST_ID]: instanceId,
71-
[SemanticResourceAttributes.HOST_TYPE]: instanceType,
72-
[SemanticResourceAttributes.HOST_NAME]: hostname,
72+
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS,
73+
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_EC2,
74+
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId,
75+
[SEMRESATTRS_CLOUD_REGION]: region,
76+
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: availabilityZone,
77+
[SEMRESATTRS_HOST_ID]: instanceId,
78+
[SEMRESATTRS_HOST_TYPE]: instanceType,
79+
[SEMRESATTRS_HOST_NAME]: hostname,
7380
});
7481
}
7582

detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts

+37-24
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,25 @@ import {
2121
ResourceAttributes,
2222
} from '@opentelemetry/resources';
2323
import {
24-
CloudProviderValues,
25-
CloudPlatformValues,
26-
SemanticResourceAttributes,
24+
SEMRESATTRS_CLOUD_PROVIDER,
25+
SEMRESATTRS_CLOUD_PLATFORM,
26+
SEMRESATTRS_CONTAINER_ID,
27+
SEMRESATTRS_CONTAINER_NAME,
28+
SEMRESATTRS_AWS_ECS_CONTAINER_ARN,
29+
SEMRESATTRS_AWS_ECS_CLUSTER_ARN,
30+
SEMRESATTRS_AWS_ECS_LAUNCHTYPE,
31+
SEMRESATTRS_AWS_ECS_TASK_ARN,
32+
SEMRESATTRS_AWS_ECS_TASK_FAMILY,
33+
SEMRESATTRS_AWS_ECS_TASK_REVISION,
34+
SEMRESATTRS_CLOUD_ACCOUNT_ID,
35+
SEMRESATTRS_CLOUD_REGION,
36+
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
37+
SEMRESATTRS_AWS_LOG_GROUP_NAMES,
38+
SEMRESATTRS_AWS_LOG_GROUP_ARNS,
39+
SEMRESATTRS_AWS_LOG_STREAM_NAMES,
40+
SEMRESATTRS_AWS_LOG_STREAM_ARNS,
41+
CLOUDPROVIDERVALUES_AWS,
42+
CLOUDPLATFORMVALUES_AWS_ECS,
2743
} from '@opentelemetry/semantic-conventions';
2844
import * as http from 'http';
2945
import * as util from 'util';
@@ -58,8 +74,8 @@ export class AwsEcsDetector implements Detector {
5874
}
5975

6076
let resource = new Resource({
61-
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS,
62-
[SemanticResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AWS_ECS,
77+
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS,
78+
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_ECS,
6379
}).merge(await AwsEcsDetector._getContainerIdAndHostnameResource());
6480

6581
const metadataUrl = getEnv().ECS_CONTAINER_METADATA_URI_V4;
@@ -114,8 +130,8 @@ export class AwsEcsDetector implements Detector {
114130

115131
if (hostName || containerId) {
116132
return new Resource({
117-
[SemanticResourceAttributes.CONTAINER_NAME]: hostName || '',
118-
[SemanticResourceAttributes.CONTAINER_ID]: containerId || '',
133+
[SEMRESATTRS_CONTAINER_NAME]: hostName || '',
134+
[SEMRESATTRS_CONTAINER_ID]: containerId || '',
119135
});
120136
}
121137

@@ -145,23 +161,20 @@ export class AwsEcsDetector implements Detector {
145161

146162
// https://github.com/open-telemetry/semantic-conventions/blob/main/semantic_conventions/resource/cloud_provider/aws/ecs.yaml
147163
const attributes: ResourceAttributes = {
148-
[SemanticResourceAttributes.AWS_ECS_CONTAINER_ARN]: containerArn,
149-
[SemanticResourceAttributes.AWS_ECS_CLUSTER_ARN]: clusterArn,
150-
[SemanticResourceAttributes.AWS_ECS_LAUNCHTYPE]:
151-
launchType?.toLowerCase(),
152-
[SemanticResourceAttributes.AWS_ECS_TASK_ARN]: taskArn,
153-
[SemanticResourceAttributes.AWS_ECS_TASK_FAMILY]: taskMetadata['Family'],
154-
[SemanticResourceAttributes.AWS_ECS_TASK_REVISION]:
155-
taskMetadata['Revision'],
156-
157-
[SemanticResourceAttributes.CLOUD_ACCOUNT_ID]: accountId,
158-
[SemanticResourceAttributes.CLOUD_REGION]: region,
164+
[SEMRESATTRS_AWS_ECS_CONTAINER_ARN]: containerArn,
165+
[SEMRESATTRS_AWS_ECS_CLUSTER_ARN]: clusterArn,
166+
[SEMRESATTRS_AWS_ECS_LAUNCHTYPE]: launchType?.toLowerCase(),
167+
[SEMRESATTRS_AWS_ECS_TASK_ARN]: taskArn,
168+
[SEMRESATTRS_AWS_ECS_TASK_FAMILY]: taskMetadata['Family'],
169+
[SEMRESATTRS_AWS_ECS_TASK_REVISION]: taskMetadata['Revision'],
170+
171+
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId,
172+
[SEMRESATTRS_CLOUD_REGION]: region,
159173
};
160174

161175
// The availability zone is not available in all Fargate runtimes
162176
if (availabilityZone) {
163-
attributes[SemanticResourceAttributes.CLOUD_AVAILABILITY_ZONE] =
164-
availabilityZone;
177+
attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = availabilityZone;
165178
}
166179

167180
return new Resource(attributes);
@@ -192,10 +205,10 @@ export class AwsEcsDetector implements Detector {
192205
const logsStreamArn = `arn:aws:logs:${logsRegion}:${awsAccount}:log-group:${logsGroupName}:log-stream:${logsStreamName}`;
193206

194207
return new Resource({
195-
[SemanticResourceAttributes.AWS_LOG_GROUP_NAMES]: [logsGroupName],
196-
[SemanticResourceAttributes.AWS_LOG_GROUP_ARNS]: [logsGroupArn],
197-
[SemanticResourceAttributes.AWS_LOG_STREAM_NAMES]: [logsStreamName],
198-
[SemanticResourceAttributes.AWS_LOG_STREAM_ARNS]: [logsStreamArn],
208+
[SEMRESATTRS_AWS_LOG_GROUP_NAMES]: [logsGroupName],
209+
[SEMRESATTRS_AWS_LOG_GROUP_ARNS]: [logsGroupArn],
210+
[SEMRESATTRS_AWS_LOG_STREAM_NAMES]: [logsStreamName],
211+
[SEMRESATTRS_AWS_LOG_STREAM_ARNS]: [logsStreamArn],
199212
});
200213
}
201214

detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsEksDetector.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import {
2020
ResourceDetectionConfig,
2121
} from '@opentelemetry/resources';
2222
import {
23-
CloudProviderValues,
24-
CloudPlatformValues,
25-
SemanticResourceAttributes,
23+
SEMRESATTRS_CLOUD_PROVIDER,
24+
SEMRESATTRS_CLOUD_PLATFORM,
25+
SEMRESATTRS_K8S_CLUSTER_NAME,
26+
SEMRESATTRS_CONTAINER_ID,
27+
CLOUDPROVIDERVALUES_AWS,
28+
CLOUDPLATFORMVALUES_AWS_EKS,
2629
} from '@opentelemetry/semantic-conventions';
2730
import * as https from 'https';
2831
import * as fs from 'fs';
@@ -79,12 +82,10 @@ export class AwsEksDetector implements Detector {
7982
return !containerId && !clusterName
8083
? Resource.empty()
8184
: new Resource({
82-
[SemanticResourceAttributes.CLOUD_PROVIDER]:
83-
CloudProviderValues.AWS,
84-
[SemanticResourceAttributes.CLOUD_PLATFORM]:
85-
CloudPlatformValues.AWS_EKS,
86-
[SemanticResourceAttributes.K8S_CLUSTER_NAME]: clusterName || '',
87-
[SemanticResourceAttributes.CONTAINER_ID]: containerId || '',
85+
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS,
86+
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_EKS,
87+
[SEMRESATTRS_K8S_CLUSTER_NAME]: clusterName || '',
88+
[SEMRESATTRS_CONTAINER_ID]: containerId || '',
8889
});
8990
} catch (e) {
9091
diag.warn('Process is not running on K8S', e);

detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ import {
2121
ResourceDetectionConfig,
2222
} from '@opentelemetry/resources';
2323
import {
24-
CloudProviderValues,
25-
CloudPlatformValues,
26-
SemanticResourceAttributes,
24+
SEMRESATTRS_CLOUD_PROVIDER,
25+
SEMRESATTRS_CLOUD_PLATFORM,
26+
SEMRESATTRS_CLOUD_REGION,
27+
SEMRESATTRS_FAAS_VERSION,
28+
SEMRESATTRS_FAAS_NAME,
29+
CLOUDPROVIDERVALUES_AWS,
30+
CLOUDPLATFORMVALUES_AWS_LAMBDA,
2731
} from '@opentelemetry/semantic-conventions';
2832

2933
/**
@@ -42,22 +46,18 @@ export class AwsLambdaDetector implements Detector {
4246
const region = process.env.AWS_REGION;
4347

4448
const attributes: ResourceAttributes = {
45-
[SemanticResourceAttributes.CLOUD_PROVIDER]: String(
46-
CloudProviderValues.AWS
47-
),
48-
[SemanticResourceAttributes.CLOUD_PLATFORM]: String(
49-
CloudPlatformValues.AWS_LAMBDA
50-
),
49+
[SEMRESATTRS_CLOUD_PROVIDER]: String(CLOUDPROVIDERVALUES_AWS),
50+
[SEMRESATTRS_CLOUD_PLATFORM]: String(CLOUDPLATFORMVALUES_AWS_LAMBDA),
5151
};
5252
if (region) {
53-
attributes[SemanticResourceAttributes.CLOUD_REGION] = region;
53+
attributes[SEMRESATTRS_CLOUD_REGION] = region;
5454
}
5555

5656
if (functionName) {
57-
attributes[SemanticResourceAttributes.FAAS_NAME] = functionName;
57+
attributes[SEMRESATTRS_FAAS_NAME] = functionName;
5858
}
5959
if (functionVersion) {
60-
attributes[SemanticResourceAttributes.FAAS_VERSION] = functionVersion;
60+
attributes[SEMRESATTRS_FAAS_VERSION] = functionVersion;
6161
}
6262

6363
return new Resource(attributes);

detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
assertEmptyResource,
2222
assertServiceResource,
2323
} from '@opentelemetry/contrib-test-utils';
24-
import { CloudPlatformValues } from '@opentelemetry/semantic-conventions';
24+
import { CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK } from '@opentelemetry/semantic-conventions';
2525

2626
describe('BeanstalkResourceDetector', () => {
2727
const err = new Error('failed to read config file');
@@ -58,7 +58,7 @@ describe('BeanstalkResourceDetector', () => {
5858
sinon.assert.calledOnce(readStub);
5959
assert.ok(resource);
6060
assertServiceResource(resource, {
61-
name: CloudPlatformValues.AWS_ELASTIC_BEANSTALK,
61+
name: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
6262
namespace: 'scorekeep',
6363
version: 'app-5a56-170119_190650-stage-170119_190650',
6464
instanceId: '32',
@@ -80,7 +80,7 @@ describe('BeanstalkResourceDetector', () => {
8080
sinon.assert.calledOnce(readStub);
8181
assert.ok(resource);
8282
assertServiceResource(resource, {
83-
name: CloudPlatformValues.AWS_ELASTIC_BEANSTALK,
83+
name: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
8484
namespace: 'scorekeep',
8585
version: 'app-5a56-170119_190650-stage-170119_190650',
8686
instanceId: '32',

0 commit comments

Comments
 (0)