From 9ce9eea7cad32cbc57bd80830f21e4f366ecf929 Mon Sep 17 00:00:00 2001 From: Severin Neumann Date: Sat, 8 May 2021 09:45:55 +0200 Subject: [PATCH] =?UTF-8?q?refactor(semantic-conventions):=20use=20semanti?= =?UTF-8?q?c-conventions=20for=20resource=E2=80=A6=20(#2173)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/opentelemetry-core/package.json | 3 +- .../src/platform/browser/sdk-info.ts | 12 +- .../src/platform/node/sdk-info.ts | 13 +- packages/opentelemetry-core/tsconfig.json | 3 + .../package.json | 3 +- .../src/zipkin.ts | 8 +- .../test/common/transform.test.ts | 6 +- .../test/node/zipkin.test.ts | 12 +- .../tsconfig.json | 3 + packages/opentelemetry-node/package.json | 1 + .../test/NodeTracerProvider.test.ts | 5 +- packages/opentelemetry-node/tsconfig.json | 3 + .../package.json | 3 +- .../src/detectors/AwsBeanstalkDetector.ts | 18 ++- .../src/detectors/AwsEc2Detector.ts | 22 +-- .../src/detectors/AwsEcsDetector.ts | 12 +- .../src/detectors/AwsEksDetector.ts | 13 +- .../src/detectors/AwsLambdaDetector.ts | 14 +- .../detectors/AwsBeanstalkDetector.test.ts | 5 +- .../test/detectors/AwsEc2Detector.test.ts | 1 + .../tsconfig.json | 3 + .../package.json | 1 + .../src/detectors/GcpDetector.ts | 25 ++-- .../tsconfig.json | 3 + packages/opentelemetry-resources/package.json | 3 +- .../opentelemetry-resources/src/Resource.ts | 11 +- .../opentelemetry-resources/src/constants.ts | 136 ------------------ packages/opentelemetry-resources/src/index.ts | 1 - .../node/detectors/ProcessDetector.ts | 27 ++-- .../test/detectors/EnvDetector.test.ts | 9 +- .../test/resource-assertions.test.ts | 82 ++++++----- .../test/util/resource-assertions.ts | 105 +++++++------- .../test/util/sample-detector.ts | 15 +- .../opentelemetry-resources/tsconfig.json | 3 + .../test/WebTracerProvider.test.ts | 5 +- 35 files changed, 260 insertions(+), 329 deletions(-) delete mode 100644 packages/opentelemetry-resources/src/constants.ts diff --git a/packages/opentelemetry-core/package.json b/packages/opentelemetry-core/package.json index d2399250d6..3ce816418e 100644 --- a/packages/opentelemetry-core/package.json +++ b/packages/opentelemetry-core/package.json @@ -85,6 +85,7 @@ "@opentelemetry/api": "^1.0.0-rc.0" }, "dependencies": { - "semver": "^7.1.3" + "semver": "^7.1.3", + "@opentelemetry/semantic-conventions": "0.19.0" } } diff --git a/packages/opentelemetry-core/src/platform/browser/sdk-info.ts b/packages/opentelemetry-core/src/platform/browser/sdk-info.ts index b07b009dee..04d2ea3d08 100644 --- a/packages/opentelemetry-core/src/platform/browser/sdk-info.ts +++ b/packages/opentelemetry-core/src/platform/browser/sdk-info.ts @@ -15,11 +15,15 @@ */ import { VERSION } from '../../version'; +import { + TelemetrySdkLanguageValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; /** Constants describing the SDK in use */ export const SDK_INFO = { - NAME: 'opentelemetry', - RUNTIME: 'browser', - LANGUAGE: 'webjs', - VERSION: VERSION, + [ResourceAttributes.TELEMETRY_SDK_NAME]: 'opentelemetry', + [ResourceAttributes.PROCESS_RUNTIME_NAME]: 'browser', + [ResourceAttributes.TELEMETRY_SDK_LANGUAGE]: TelemetrySdkLanguageValues.WEBJS, + [ResourceAttributes.TELEMETRY_SDK_VERSION]: VERSION, }; diff --git a/packages/opentelemetry-core/src/platform/node/sdk-info.ts b/packages/opentelemetry-core/src/platform/node/sdk-info.ts index 0a59c93d33..87147644eb 100644 --- a/packages/opentelemetry-core/src/platform/node/sdk-info.ts +++ b/packages/opentelemetry-core/src/platform/node/sdk-info.ts @@ -15,11 +15,16 @@ */ import { VERSION } from '../../version'; +import { + TelemetrySdkLanguageValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; /** Constants describing the SDK in use */ export const SDK_INFO = { - NAME: 'opentelemetry', - RUNTIME: 'node', - LANGUAGE: 'nodejs', - VERSION: VERSION, + [ResourceAttributes.TELEMETRY_SDK_NAME]: 'opentelemetry', + [ResourceAttributes.PROCESS_RUNTIME_NAME]: 'node', + [ResourceAttributes.TELEMETRY_SDK_LANGUAGE]: + TelemetrySdkLanguageValues.NODEJS, + [ResourceAttributes.TELEMETRY_SDK_VERSION]: VERSION, }; diff --git a/packages/opentelemetry-core/tsconfig.json b/packages/opentelemetry-core/tsconfig.json index 62f7aed99b..73b0dd709c 100644 --- a/packages/opentelemetry-core/tsconfig.json +++ b/packages/opentelemetry-core/tsconfig.json @@ -11,6 +11,9 @@ "references": [ { "path": "../opentelemetry-propagator-b3" + }, + { + "path": "../opentelemetry-semantic-conventions" } ] } diff --git a/packages/opentelemetry-exporter-zipkin/package.json b/packages/opentelemetry-exporter-zipkin/package.json index bb91a3b224..748b18216d 100644 --- a/packages/opentelemetry-exporter-zipkin/package.json +++ b/packages/opentelemetry-exporter-zipkin/package.json @@ -86,6 +86,7 @@ "dependencies": { "@opentelemetry/core": "0.19.0", "@opentelemetry/resources": "0.19.0", - "@opentelemetry/tracing": "0.19.0" + "@opentelemetry/tracing": "0.19.0", + "@opentelemetry/semantic-conventions": "0.19.0" } } diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index 6fbaeb25c6..c0cd5bbe35 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -24,7 +24,7 @@ import { statusCodeTagName, statusDescriptionTagName, } from './transform'; -import { SERVICE_RESOURCE } from '@opentelemetry/resources'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; import { prepareGetHeaders } from './utils'; /** @@ -66,7 +66,7 @@ export class ZipkinExporter implements SpanExporter { ) { if (typeof this._serviceName !== 'string') { this._serviceName = String( - spans[0].resource.attributes[SERVICE_RESOURCE.NAME] || + spans[0].resource.attributes[ResourceAttributes.SERVICE_NAME] || this.DEFAULT_SERVICE_NAME ); } @@ -128,8 +128,8 @@ export class ZipkinExporter implements SpanExporter { toZipkinSpan( span, String( - span.attributes[SERVICE_RESOURCE.NAME] || - span.resource.attributes[SERVICE_RESOURCE.NAME] || + span.attributes[ResourceAttributes.SERVICE_NAME] || + span.resource.attributes[ResourceAttributes.SERVICE_NAME] || serviceName ), this._statusCodeTagName, diff --git a/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts b/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts index dcd3b6c41e..4e47d0c095 100644 --- a/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts @@ -20,7 +20,8 @@ import { hrTimeToMicroseconds, VERSION, } from '@opentelemetry/core'; -import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; +import { Resource } from '@opentelemetry/resources'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; import { BasicTracerProvider, Span } from '@opentelemetry/tracing'; import * as assert from 'assert'; import { @@ -33,7 +34,8 @@ import { import * as zipkinTypes from '../../src/types'; const tracer = new BasicTracerProvider().getTracer('default'); -const language = tracer.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE]; +const language = + tracer.resource.attributes[ResourceAttributes.TELEMETRY_SDK_LANGUAGE]; const parentId = '5c1c63257de34c67'; const spanContext: api.SpanContext = { diff --git a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts index 9c63092ec4..0dbe5908aa 100644 --- a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts @@ -27,7 +27,7 @@ import { Resource } from '@opentelemetry/resources'; import { ZipkinExporter } from '../../src'; import * as zipkinTypes from '../../src/types'; import { TraceFlags } from '@opentelemetry/api'; -import { SERVICE_RESOURCE } from '@opentelemetry/resources'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; const MICROS_PER_SECS = 1e6; @@ -417,7 +417,7 @@ describe('Zipkin Exporter - node', () => { }, ], resource: new Resource({ - [SERVICE_RESOURCE.NAME]: resource_service_name, + [ResourceAttributes.SERVICE_NAME]: resource_service_name, }), instrumentationLibrary: { name: 'default', version: '0.0.1' }, }; @@ -519,7 +519,7 @@ describe('Zipkin Exporter - node', () => { }, ], resource: new Resource({ - [SERVICE_RESOURCE.NAME]: resource_service_name, + [ResourceAttributes.SERVICE_NAME]: resource_service_name, }), instrumentationLibrary: { name: 'default', version: '0.0.1' }, }; @@ -542,7 +542,7 @@ describe('Zipkin Exporter - node', () => { links: [], events: [], resource: new Resource({ - [SERVICE_RESOURCE.NAME]: resource_service_name_prime, + [ResourceAttributes.SERVICE_NAME]: resource_service_name_prime, }), instrumentationLibrary: { name: 'default', version: '0.0.1' }, }; @@ -598,7 +598,7 @@ describe('Zipkin Exporter - node', () => { attributes: { key1: 'value1', key2: 'value2', - [SERVICE_RESOURCE.NAME]: span_service_name, + [ResourceAttributes.SERVICE_NAME]: span_service_name, }, links: [], events: [ @@ -627,7 +627,7 @@ describe('Zipkin Exporter - node', () => { code: api.SpanStatusCode.OK, }, attributes: { - [SERVICE_RESOURCE.NAME]: span_service_name_prime, + [ResourceAttributes.SERVICE_NAME]: span_service_name_prime, }, links: [], events: [], diff --git a/packages/opentelemetry-exporter-zipkin/tsconfig.json b/packages/opentelemetry-exporter-zipkin/tsconfig.json index 77140114c8..a5d1c2f05c 100644 --- a/packages/opentelemetry-exporter-zipkin/tsconfig.json +++ b/packages/opentelemetry-exporter-zipkin/tsconfig.json @@ -15,6 +15,9 @@ { "path": "../opentelemetry-resources" }, + { + "path": "../opentelemetry-semantic-conventions" + }, { "path": "../opentelemetry-tracing" } diff --git a/packages/opentelemetry-node/package.json b/packages/opentelemetry-node/package.json index 2149b39521..34da33956e 100644 --- a/packages/opentelemetry-node/package.json +++ b/packages/opentelemetry-node/package.json @@ -43,6 +43,7 @@ "devDependencies": { "@opentelemetry/api": "^1.0.0-rc.0", "@opentelemetry/resources": "0.19.0", + "@opentelemetry/semantic-conventions": "0.19.0", "@types/mocha": "8.2.2", "@types/node": "14.14.43", "@types/semver": "7.3.5", diff --git a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts index 81fe634775..24d1d0e82c 100644 --- a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts +++ b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts @@ -25,7 +25,8 @@ import { import { AlwaysOnSampler, AlwaysOffSampler } from '@opentelemetry/core'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { Span } from '@opentelemetry/tracing'; -import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; +import { Resource } from '@opentelemetry/resources'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as path from 'path'; import { ContextManager, ROOT_CONTEXT } from '@opentelemetry/api'; @@ -147,7 +148,7 @@ describe('NodeTracerProvider', () => { assert.ok(span); assert.ok(span.resource instanceof Resource); assert.equal( - span.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], + span.resource.attributes[ResourceAttributes.TELEMETRY_SDK_LANGUAGE], 'nodejs' ); }); diff --git a/packages/opentelemetry-node/tsconfig.json b/packages/opentelemetry-node/tsconfig.json index 353ee432d2..60a27cc6eb 100644 --- a/packages/opentelemetry-node/tsconfig.json +++ b/packages/opentelemetry-node/tsconfig.json @@ -24,6 +24,9 @@ { "path": "../opentelemetry-resources" }, + { + "path": "../opentelemetry-semantic-conventions" + }, { "path": "../opentelemetry-tracing" } diff --git a/packages/opentelemetry-resource-detector-aws/package.json b/packages/opentelemetry-resource-detector-aws/package.json index fde533c296..d469dc43d5 100644 --- a/packages/opentelemetry-resource-detector-aws/package.json +++ b/packages/opentelemetry-resource-detector-aws/package.json @@ -59,6 +59,7 @@ }, "dependencies": { "@opentelemetry/core": "0.19.0", - "@opentelemetry/resources": "0.19.0" + "@opentelemetry/resources": "0.19.0", + "@opentelemetry/semantic-conventions": "0.19.0" } } diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsBeanstalkDetector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsBeanstalkDetector.ts index 0cfdc3c102..2d54d9dde2 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsBeanstalkDetector.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsBeanstalkDetector.ts @@ -18,9 +18,13 @@ import { diag } from '@opentelemetry/api'; import { Detector, Resource, - SERVICE_RESOURCE, ResourceDetectionConfig, } from '@opentelemetry/resources'; +import { + CloudProviderValues, + CloudPlatformValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; import * as fs from 'fs'; import * as util from 'util'; @@ -65,10 +69,14 @@ export class AwsBeanstalkDetector implements Detector { const parsedData = JSON.parse(rawData); return new Resource({ - [SERVICE_RESOURCE.NAME]: 'elastic_beanstalk', - [SERVICE_RESOURCE.NAMESPACE]: parsedData.environment_name, - [SERVICE_RESOURCE.VERSION]: parsedData.version_label, - [SERVICE_RESOURCE.INSTANCE_ID]: parsedData.deployment_id, + [ResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS, + [ResourceAttributes.CLOUD_PLATFORM]: + CloudPlatformValues.AWS_ELASTICBEANSTALK, + [ResourceAttributes.SERVICE_NAME]: + CloudPlatformValues.AWS_ELASTICBEANSTALK, + [ResourceAttributes.SERVICE_NAMESPACE]: parsedData.environment_name, + [ResourceAttributes.SERVICE_VERSION]: parsedData.version_label, + [ResourceAttributes.SERVICE_INSTANCE_ID]: parsedData.deployment_id, }); } catch (e) { diag.debug(`AwsBeanstalkDetector failed: ${e.message}`); diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEc2Detector.ts index ef2db49fe5..99d30c9049 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEc2Detector.ts @@ -17,10 +17,13 @@ import { Detector, Resource, - CLOUD_RESOURCE, - HOST_RESOURCE, ResourceDetectionConfig, } from '@opentelemetry/resources'; +import { + CloudProviderValues, + CloudPlatformValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; import * as http from 'http'; /** @@ -64,13 +67,14 @@ class AwsEc2Detector implements Detector { const hostname = await this._fetchHost(token); return new Resource({ - [CLOUD_RESOURCE.PROVIDER]: 'aws', - [CLOUD_RESOURCE.ACCOUNT_ID]: accountId, - [CLOUD_RESOURCE.REGION]: region, - [CLOUD_RESOURCE.ZONE]: availabilityZone, - [HOST_RESOURCE.ID]: instanceId, - [HOST_RESOURCE.TYPE]: instanceType, - [HOST_RESOURCE.NAME]: hostname, + [ResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS, + [ResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AWS_EC2, + [ResourceAttributes.CLOUD_ACCOUNT_ID]: accountId, + [ResourceAttributes.CLOUD_REGION]: region, + [ResourceAttributes.CLOUD_AVAILABILITY_ZONE]: availabilityZone, + [ResourceAttributes.HOST_ID]: instanceId, + [ResourceAttributes.HOST_TYPE]: instanceType, + [ResourceAttributes.HOST_NAME]: hostname, }); } diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts index 28de093d0a..0f73666778 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEcsDetector.ts @@ -19,8 +19,12 @@ import { Detector, Resource, ResourceDetectionConfig, - CONTAINER_RESOURCE, } from '@opentelemetry/resources'; +import { + CloudProviderValues, + CloudPlatformValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; import * as util from 'util'; import * as fs from 'fs'; import * as os from 'os'; @@ -49,8 +53,10 @@ export class AwsEcsDetector implements Detector { return !hostName && !containerId ? Resource.empty() : new Resource({ - [CONTAINER_RESOURCE.NAME]: hostName || '', - [CONTAINER_RESOURCE.ID]: containerId || '', + [ResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS, + [ResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AWS_ECS, + [ResourceAttributes.CONTAINER_NAME]: hostName || '', + [ResourceAttributes.CONTAINER_ID]: containerId || '', }); } diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEksDetector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEksDetector.ts index 3375ee7557..c7348e325f 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEksDetector.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsEksDetector.ts @@ -17,10 +17,13 @@ import { Detector, Resource, - CONTAINER_RESOURCE, - K8S_RESOURCE, ResourceDetectionConfig, } from '@opentelemetry/resources'; +import { + CloudProviderValues, + CloudPlatformValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; import * as https from 'https'; import * as fs from 'fs'; import * as util from 'util'; @@ -76,8 +79,10 @@ export class AwsEksDetector implements Detector { return !containerId && !clusterName ? Resource.empty() : new Resource({ - [K8S_RESOURCE.CLUSTER_NAME]: clusterName || '', - [CONTAINER_RESOURCE.ID]: containerId || '', + [ResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AWS, + [ResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AWS_EKS, + [ResourceAttributes.K8S_CLUSTER_NAME]: clusterName || '', + [ResourceAttributes.CONTAINER_ID]: containerId || '', }); } catch (e) { diag.warn('Process is not running on K8S', e); diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts index 29cb097b95..757bd6725c 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts @@ -17,9 +17,12 @@ import { Detector, Resource, - CLOUD_RESOURCE, ResourceDetectionConfig, } from '@opentelemetry/resources'; +import { + CloudProviderValues, + ResourceAttributes, +} from '@opentelemetry/semantic-conventions'; /** * The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda @@ -37,18 +40,17 @@ export class AwsLambdaDetector implements Detector { const region = process.env.AWS_REGION; const attributes = { - [CLOUD_RESOURCE.PROVIDER]: 'aws', + [ResourceAttributes.CLOUD_PROVIDER]: String(CloudProviderValues.AWS), }; if (region) { - attributes[CLOUD_RESOURCE.REGION] = region; + attributes[ResourceAttributes.CLOUD_REGION] = region; } - // TODO(https://github.com/open-telemetry/opentelemetry-js/issues/2123): Migrate to FAAS_RESOURCE when defined. if (functionName) { - attributes['faas.name'] = functionName; + attributes[ResourceAttributes.FAAS_NAME] = functionName; } if (functionVersion) { - attributes['faas.version'] = functionVersion; + attributes[ResourceAttributes.FAAS_VERSION] = functionVersion; } return new Resource(attributes); diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts index d5c8622cf8..b0403f4627 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts @@ -21,6 +21,7 @@ import { assertEmptyResource, assertServiceResource, } from '@opentelemetry/resources/test/util/resource-assertions'; +import { CloudPlatformValues } from '@opentelemetry/semantic-conventions'; describe('BeanstalkResourceDetector', () => { const err = new Error('failed to read config file'); @@ -57,7 +58,7 @@ describe('BeanstalkResourceDetector', () => { sinon.assert.calledOnce(readStub); assert.ok(resource); assertServiceResource(resource, { - name: 'elastic_beanstalk', + name: CloudPlatformValues.AWS_ELASTICBEANSTALK, namespace: 'scorekeep', version: 'app-5a56-170119_190650-stage-170119_190650', instanceId: '32', @@ -79,7 +80,7 @@ describe('BeanstalkResourceDetector', () => { sinon.assert.calledOnce(readStub); assert.ok(resource); assertServiceResource(resource, { - name: 'elastic_beanstalk', + name: CloudPlatformValues.AWS_ELASTICBEANSTALK, namespace: 'scorekeep', version: 'app-5a56-170119_190650-stage-170119_190650', instanceId: '32', diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts index 4eebb7feba..982228ed97 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts @@ -69,6 +69,7 @@ describe('awsEc2Detector', () => { scope.done(); assert.ok(resource); + assertCloudResource(resource, { provider: 'aws', accountId: 'my-account-id', diff --git a/packages/opentelemetry-resource-detector-aws/tsconfig.json b/packages/opentelemetry-resource-detector-aws/tsconfig.json index 6549996003..b16c7037b4 100644 --- a/packages/opentelemetry-resource-detector-aws/tsconfig.json +++ b/packages/opentelemetry-resource-detector-aws/tsconfig.json @@ -14,6 +14,9 @@ }, { "path": "../opentelemetry-resources" + }, + { + "path": "../opentelemetry-semantic-conventions" } ] } diff --git a/packages/opentelemetry-resource-detector-gcp/package.json b/packages/opentelemetry-resource-detector-gcp/package.json index 218ab630d7..a2a283c9dd 100644 --- a/packages/opentelemetry-resource-detector-gcp/package.json +++ b/packages/opentelemetry-resource-detector-gcp/package.json @@ -59,6 +59,7 @@ }, "dependencies": { "@opentelemetry/resources": "0.19.0", + "@opentelemetry/semantic-conventions": "0.19.0", "gcp-metadata": "^4.1.4", "semver": "7.3.5" } diff --git a/packages/opentelemetry-resource-detector-gcp/src/detectors/GcpDetector.ts b/packages/opentelemetry-resource-detector-gcp/src/detectors/GcpDetector.ts index 9a76067f68..39477a1b00 100644 --- a/packages/opentelemetry-resource-detector-gcp/src/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resource-detector-gcp/src/detectors/GcpDetector.ts @@ -22,12 +22,12 @@ import { ResourceDetectionConfig, Resource, ResourceAttributes, - CLOUD_RESOURCE, - HOST_RESOURCE, - K8S_RESOURCE, - CONTAINER_RESOURCE, } from '@opentelemetry/resources'; import { getEnv } from '@opentelemetry/core'; +import { + CloudProviderValues, + ResourceAttributes as SemanticResourceAttributes, +} from '@opentelemetry/semantic-conventions'; /** * The GcpDetector can be used to detect if a process is running in the Google @@ -60,10 +60,11 @@ class GcpDetector implements Detector { ]); const attributes: ResourceAttributes = {}; - attributes[CLOUD_RESOURCE.ACCOUNT_ID] = projectId; - attributes[HOST_RESOURCE.ID] = instanceId; - attributes[CLOUD_RESOURCE.ZONE] = zoneId; - attributes[CLOUD_RESOURCE.PROVIDER] = 'gcp'; + attributes[SemanticResourceAttributes.CLOUD_ACCOUNT_ID] = projectId; + attributes[SemanticResourceAttributes.HOST_ID] = instanceId; + attributes[SemanticResourceAttributes.CLOUD_AVAILABILITY_ZONE] = zoneId; + attributes[SemanticResourceAttributes.CLOUD_PROVIDER] = + CloudProviderValues.GCP; if (getEnv().KUBERNETES_SERVICE_HOST) this._addK8sAttributes(attributes, clusterName); @@ -78,10 +79,10 @@ class GcpDetector implements Detector { ): void { const env = getEnv(); - attributes[K8S_RESOURCE.CLUSTER_NAME] = clusterName; - attributes[K8S_RESOURCE.NAMESPACE_NAME] = env.NAMESPACE; - attributes[K8S_RESOURCE.POD_NAME] = env.HOSTNAME; - attributes[CONTAINER_RESOURCE.NAME] = env.CONTAINER_NAME; + attributes[SemanticResourceAttributes.K8S_CLUSTER_NAME] = clusterName; + attributes[SemanticResourceAttributes.K8S_NAMESPACE_NAME] = env.NAMESPACE; + attributes[SemanticResourceAttributes.K8S_POD_NAME] = env.HOSTNAME; + attributes[SemanticResourceAttributes.CONTAINER_NAME] = env.CONTAINER_NAME; } /** Gets project id from GCP project metadata. */ diff --git a/packages/opentelemetry-resource-detector-gcp/tsconfig.json b/packages/opentelemetry-resource-detector-gcp/tsconfig.json index 6549996003..b16c7037b4 100644 --- a/packages/opentelemetry-resource-detector-gcp/tsconfig.json +++ b/packages/opentelemetry-resource-detector-gcp/tsconfig.json @@ -14,6 +14,9 @@ }, { "path": "../opentelemetry-resources" + }, + { + "path": "../opentelemetry-semantic-conventions" } ] } diff --git a/packages/opentelemetry-resources/package.json b/packages/opentelemetry-resources/package.json index a81bdd3bc4..5c3c178771 100644 --- a/packages/opentelemetry-resources/package.json +++ b/packages/opentelemetry-resources/package.json @@ -67,6 +67,7 @@ "@opentelemetry/api": "^1.0.0-rc.0" }, "dependencies": { - "@opentelemetry/core": "0.19.0" + "@opentelemetry/core": "0.19.0", + "@opentelemetry/semantic-conventions": "0.19.0" } } diff --git a/packages/opentelemetry-resources/src/Resource.ts b/packages/opentelemetry-resources/src/Resource.ts index b80d6614b0..ea5ed0ab37 100644 --- a/packages/opentelemetry-resources/src/Resource.ts +++ b/packages/opentelemetry-resources/src/Resource.ts @@ -14,8 +14,8 @@ * limitations under the License. */ +import { ResourceAttributes as SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { SDK_INFO } from '@opentelemetry/core'; -import { TELEMETRY_SDK_RESOURCE } from './constants'; import { ResourceAttributes } from './types'; /** @@ -37,9 +37,12 @@ export class Resource { */ static createTelemetrySDKResource(): Resource { return new Resource({ - [TELEMETRY_SDK_RESOURCE.LANGUAGE]: SDK_INFO.LANGUAGE, - [TELEMETRY_SDK_RESOURCE.NAME]: SDK_INFO.NAME, - [TELEMETRY_SDK_RESOURCE.VERSION]: SDK_INFO.VERSION, + [SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]: + SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE], + [SemanticResourceAttributes.TELEMETRY_SDK_NAME]: + SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_NAME], + [SemanticResourceAttributes.TELEMETRY_SDK_VERSION]: + SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_VERSION], }); } diff --git a/packages/opentelemetry-resources/src/constants.ts b/packages/opentelemetry-resources/src/constants.ts deleted file mode 100644 index 89201138cc..0000000000 --- a/packages/opentelemetry-resources/src/constants.ts +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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 CLOUD_RESOURCE = { - /** Name of the cloud provider. Example values are aws, azure, gcp. */ - PROVIDER: 'cloud.provider', - - /** The cloud account id used to identify different entities. */ - ACCOUNT_ID: 'cloud.account.id', - - /** A specific geographical location where different entities can run. */ - REGION: 'cloud.region', - - /** Zones are a sub set of the region connected through low-latency links. */ - ZONE: 'cloud.zone', -}; - -/** - * SpanAttributes defining a compute unit (e.g. Container, Process, Lambda - * Function). - * */ -export const CONTAINER_RESOURCE = { - /** The container name. */ - NAME: 'container.name', - - /** The container id. */ - ID: 'container.id', - - /** The name of the image the container was built on. */ - IMAGE_NAME: 'container.image.name', - - /** The container image tag. */ - IMAGE_TAG: 'container.image.tag', -}; - -/** SpanAttributes defining a computing instance (e.g. host). */ -export const HOST_RESOURCE = { - /** - * Unique host id. For Cloud this must be the instance_id assigned by the - * cloud provider - */ - ID: 'host.id', - - /** - * Name of the host. It may contain what hostname returns on Unix systems, - * the fully qualified, or a name specified by the user. - */ - NAME: 'host.name', - - /** Type of host. For Cloud this must be the machine type.*/ - TYPE: 'host.type', - - /** Name of the VM image or OS install the host was instantiated from. */ - IMAGE_NAME: 'host.image.name', - - /** VM image id. For Cloud, this value is from the provider. */ - IMAGE_ID: 'host.image.id', - - /** The version string of the VM image */ - IMAGE_VERSION: 'host.image.version', -}; - -/** SpanAttributes defining a deployment service (e.g. Kubernetes). */ -export const K8S_RESOURCE = { - /** The name of the cluster that the pod is running in. */ - CLUSTER_NAME: 'k8s.cluster.name', - - /** The name of the namespace that the pod is running in. */ - NAMESPACE_NAME: 'k8s.namespace.name', - - /** The name of the pod. */ - POD_NAME: 'k8s.pod.name', - - /** The name of the deployment. */ - DEPLOYMENT_NAME: 'k8s.deployment.name', -}; - -/** SpanAttributes describing the telemetry library. */ -export const TELEMETRY_SDK_RESOURCE = { - /** The name of the telemetry library. */ - NAME: 'telemetry.sdk.name', - - /** The language of telemetry library and of the code instrumented with it. */ - LANGUAGE: 'telemetry.sdk.language', - - /** The version string of the telemetry library */ - VERSION: 'telemetry.sdk.version', -}; - -/** SpanAttributes describing a service instance. */ -export const SERVICE_RESOURCE = { - /** Logical name of the service. */ - NAME: 'service.name', - - /** A namespace for `service.name`. */ - NAMESPACE: 'service.namespace', - - /** The string ID of the service instance. */ - INSTANCE_ID: 'service.instance.id', - - /** The version string of the service API or implementation. */ - VERSION: 'service.version', -}; - -/** SpanAttributes describing a Process. */ -export const PROCESS_RESOURCE = { - /** A command which launced this proces. */ - COMMAND: 'process.command', - - /** The full command with arguments as string. */ - COMMAND_LINE: 'process.command_line', - - /** A name given to currently running porcess defaults to executable (process.title) . */ - NAME: 'process.executable.name', - - /** An owner of currently running process. */ - OWNER: 'process.owner', - - /** The full path to the process executable. */ - PATH: 'process.executable.path', - - /** Process identifier of currently running process. */ - PID: 'process.id', -}; diff --git a/packages/opentelemetry-resources/src/index.ts b/packages/opentelemetry-resources/src/index.ts index 2cfef2d182..656de7291e 100644 --- a/packages/opentelemetry-resources/src/index.ts +++ b/packages/opentelemetry-resources/src/index.ts @@ -16,6 +16,5 @@ export * from './Resource'; export * from './platform'; -export * from './constants'; export * from './types'; export * from './config'; diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts index 04608266c3..bb223fc1a0 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts @@ -15,12 +15,8 @@ */ import { diag } from '@opentelemetry/api'; -import { - Detector, - Resource, - PROCESS_RESOURCE, - ResourceDetectionConfig, -} from '../../../'; +import { ResourceAttributes as SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; +import { Detector, Resource, ResourceDetectionConfig } from '../../../'; import { ResourceAttributes } from '../../../types'; /** @@ -30,10 +26,11 @@ import { ResourceAttributes } from '../../../types'; class ProcessDetector implements Detector { async detect(config?: ResourceDetectionConfig): Promise { const processResource: ResourceAttributes = { - [PROCESS_RESOURCE.PID]: process.pid, - [PROCESS_RESOURCE.NAME]: process.title || '', - [PROCESS_RESOURCE.COMMAND]: process.argv[1] || '', - [PROCESS_RESOURCE.COMMAND_LINE]: process.argv.join(' ') || '', + [SemanticResourceAttributes.PROCESS_PID]: process.pid, + [SemanticResourceAttributes.PROCESS_EXECUTABLE_NAME]: process.title || '', + [SemanticResourceAttributes.PROCESS_COMMAND]: process.argv[1] || '', + [SemanticResourceAttributes.PROCESS_COMMAND_LINE]: + process.argv.join(' ') || '', }; return this._getResourceAttributes(processResource, config); } @@ -49,10 +46,12 @@ class ProcessDetector implements Detector { _config?: ResourceDetectionConfig ) { if ( - processResource[PROCESS_RESOURCE.NAME] === '' || - processResource[PROCESS_RESOURCE.PATH] === '' || - processResource[PROCESS_RESOURCE.COMMAND] === '' || - processResource[PROCESS_RESOURCE.COMMAND_LINE] === '' + processResource[SemanticResourceAttributes.PROCESS_EXECUTABLE_NAME] === + '' || + processResource[SemanticResourceAttributes.PROCESS_EXECUTABLE_PATH] === + '' || + processResource[SemanticResourceAttributes.PROCESS_COMMAND] === '' || + processResource[SemanticResourceAttributes.PROCESS_COMMAND_LINE] === '' ) { diag.debug( 'ProcessDetector failed: Unable to find required process resources. ' diff --git a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts index 5c8d338a26..66c228fe0f 100644 --- a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -import { envDetector, K8S_RESOURCE, Resource } from '../../src'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; +import { envDetector, Resource } from '../../src'; import { assertK8sResource, assertEmptyResource, @@ -34,9 +35,9 @@ describe('envDetector()', () => { it('should return resource information from environment variable', async () => { const resource: Resource = await envDetector.detect(); assertK8sResource(resource, { - [K8S_RESOURCE.POD_NAME]: 'pod-xyz-123', - [K8S_RESOURCE.CLUSTER_NAME]: 'c1', - [K8S_RESOURCE.NAMESPACE_NAME]: 'default', + [ResourceAttributes.K8S_POD_NAME]: 'pod-xyz-123', + [ResourceAttributes.K8S_CLUSTER_NAME]: 'c1', + [ResourceAttributes.K8S_NAMESPACE_NAME]: 'default', }); }); }); diff --git a/packages/opentelemetry-resources/test/resource-assertions.test.ts b/packages/opentelemetry-resources/test/resource-assertions.test.ts index f0d20b67a3..57a470da4d 100644 --- a/packages/opentelemetry-resources/test/resource-assertions.test.ts +++ b/packages/opentelemetry-resources/test/resource-assertions.test.ts @@ -15,15 +15,8 @@ */ import { SDK_INFO } from '@opentelemetry/core'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; import { Resource } from '../src/Resource'; -import { - CLOUD_RESOURCE, - CONTAINER_RESOURCE, - HOST_RESOURCE, - K8S_RESOURCE, - TELEMETRY_SDK_RESOURCE, - SERVICE_RESOURCE, -} from '../src/constants'; import { assertCloudResource, assertContainerResource, @@ -35,16 +28,18 @@ import { describe('assertCloudResource', () => { it('requires one cloud label', () => { - const resource = new Resource({ [CLOUD_RESOURCE.PROVIDER]: 'gcp' }); + const resource = new Resource({ + [ResourceAttributes.CLOUD_PROVIDER]: 'gcp', + }); assertCloudResource(resource, {}); }); it('validates optional attributes', () => { const resource = new Resource({ - [CLOUD_RESOURCE.PROVIDER]: 'gcp', - [CLOUD_RESOURCE.ACCOUNT_ID]: 'opentelemetry', - [CLOUD_RESOURCE.REGION]: 'us-central1', - [CLOUD_RESOURCE.ZONE]: 'us-central1-a', + [ResourceAttributes.CLOUD_PROVIDER]: 'gcp', + [ResourceAttributes.CLOUD_ACCOUNT_ID]: 'opentelemetry', + [ResourceAttributes.CLOUD_REGION]: 'us-central1', + [ResourceAttributes.CLOUD_AVAILABILITY_ZONE]: 'us-central1-a', }); assertCloudResource(resource, { provider: 'gcp', @@ -58,17 +53,18 @@ describe('assertCloudResource', () => { describe('assertContainerResource', () => { it('requires one container label', () => { const resource = new Resource({ - [CONTAINER_RESOURCE.NAME]: 'opentelemetry-autoconf', + [ResourceAttributes.CONTAINER_NAME]: 'opentelemetry-autoconf', }); assertContainerResource(resource, {}); }); it('validates optional attributes', () => { const resource = new Resource({ - [CONTAINER_RESOURCE.NAME]: 'opentelemetry-autoconf', - [CONTAINER_RESOURCE.ID]: 'abc', - [CONTAINER_RESOURCE.IMAGE_NAME]: 'gcr.io/opentelemetry/operator', - [CONTAINER_RESOURCE.IMAGE_TAG]: '0.1', + [ResourceAttributes.CONTAINER_NAME]: 'opentelemetry-autoconf', + [ResourceAttributes.CONTAINER_ID]: 'abc', + [ResourceAttributes.CONTAINER_IMAGE_NAME]: + 'gcr.io/opentelemetry/operator', + [ResourceAttributes.CONTAINER_IMAGE_TAG]: '0.1', }); assertContainerResource(resource, { name: 'opentelemetry-autoconf', @@ -82,20 +78,20 @@ describe('assertContainerResource', () => { describe('assertHostResource', () => { it('requires one host label', () => { const resource = new Resource({ - [HOST_RESOURCE.ID]: 'opentelemetry-test-id', + [ResourceAttributes.HOST_ID]: 'opentelemetry-test-id', }); assertHostResource(resource, {}); }); it('validates optional attributes', () => { const resource = new Resource({ - [HOST_RESOURCE.ID]: 'opentelemetry-test-id', - [HOST_RESOURCE.NAME]: 'opentelemetry-test-name', - [HOST_RESOURCE.TYPE]: 'n1-standard-1', - [HOST_RESOURCE.IMAGE_NAME]: + [ResourceAttributes.HOST_ID]: 'opentelemetry-test-id', + [ResourceAttributes.HOST_NAME]: 'opentelemetry-test-name', + [ResourceAttributes.HOST_TYPE]: 'n1-standard-1', + [ResourceAttributes.HOST_IMAGE_NAME]: 'infra-ami-eks-worker-node-7d4ec78312, CentOS-8-x86_64-1905', - [HOST_RESOURCE.IMAGE_ID]: 'ami-07b06b442921831e5', - [HOST_RESOURCE.IMAGE_VERSION]: '0.1', + [ResourceAttributes.HOST_IMAGE_ID]: 'ami-07b06b442921831e5', + [ResourceAttributes.HOST_IMAGE_VERSION]: '0.1', }); assertHostResource(resource, { hostName: 'opentelemetry-test-hostname', @@ -112,17 +108,17 @@ describe('assertHostResource', () => { describe('assertK8sResource', () => { it('requires one k8s label', () => { const resource = new Resource({ - [K8S_RESOURCE.CLUSTER_NAME]: 'opentelemetry-cluster', + [ResourceAttributes.K8S_CLUSTER_NAME]: 'opentelemetry-cluster', }); assertK8sResource(resource, {}); }); it('validates optional attributes', () => { const resource = new Resource({ - [K8S_RESOURCE.CLUSTER_NAME]: 'opentelemetry-cluster', - [K8S_RESOURCE.NAMESPACE_NAME]: 'default', - [K8S_RESOURCE.POD_NAME]: 'opentelemetry-pod-autoconf', - [K8S_RESOURCE.DEPLOYMENT_NAME]: 'opentelemetry', + [ResourceAttributes.K8S_CLUSTER_NAME]: 'opentelemetry-cluster', + [ResourceAttributes.K8S_NAMESPACE_NAME]: 'default', + [ResourceAttributes.K8S_POD_NAME]: 'opentelemetry-pod-autoconf', + [ResourceAttributes.K8S_DEPLOYMENT_NAME]: 'opentelemetry', }); assertK8sResource(resource, { clusterName: 'opentelemetry-cluster', @@ -136,18 +132,18 @@ describe('assertK8sResource', () => { describe('assertTelemetrySDKResource', () => { it('uses default validations', () => { const resource = new Resource({ - [TELEMETRY_SDK_RESOURCE.NAME]: SDK_INFO.NAME, - [TELEMETRY_SDK_RESOURCE.LANGUAGE]: SDK_INFO.LANGUAGE, - [TELEMETRY_SDK_RESOURCE.VERSION]: SDK_INFO.VERSION, + [ResourceAttributes.TELEMETRY_SDK_NAME]: SDK_INFO.NAME, + [ResourceAttributes.TELEMETRY_SDK_LANGUAGE]: SDK_INFO.LANGUAGE, + [ResourceAttributes.TELEMETRY_SDK_VERSION]: SDK_INFO.VERSION, }); assertTelemetrySDKResource(resource, {}); }); it('validates optional attributes', () => { const resource = new Resource({ - [TELEMETRY_SDK_RESOURCE.NAME]: 'opentelemetry', - [TELEMETRY_SDK_RESOURCE.LANGUAGE]: 'nodejs', - [TELEMETRY_SDK_RESOURCE.VERSION]: '0.1.0', + [ResourceAttributes.TELEMETRY_SDK_NAME]: 'opentelemetry', + [ResourceAttributes.TELEMETRY_SDK_LANGUAGE]: 'nodejs', + [ResourceAttributes.TELEMETRY_SDK_VERSION]: '0.1.0', }); assertTelemetrySDKResource(resource, { name: 'opentelemetry', @@ -160,8 +156,9 @@ describe('assertTelemetrySDKResource', () => { describe('assertServiceResource', () => { it('validates required attributes', () => { const resource = new Resource({ - [SERVICE_RESOURCE.NAME]: 'shoppingcart', - [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', + [ResourceAttributes.SERVICE_NAME]: 'shoppingcart', + [ResourceAttributes.SERVICE_INSTANCE_ID]: + '627cc493-f310-47de-96bd-71410b7dec09', }); assertServiceResource(resource, { name: 'shoppingcart', @@ -171,10 +168,11 @@ describe('assertServiceResource', () => { it('validates optional attributes', () => { const resource = new Resource({ - [SERVICE_RESOURCE.NAME]: 'shoppingcart', - [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', - [SERVICE_RESOURCE.NAMESPACE]: 'shop', - [SERVICE_RESOURCE.VERSION]: '0.1.0', + [ResourceAttributes.SERVICE_NAME]: 'shoppingcart', + [ResourceAttributes.SERVICE_INSTANCE_ID]: + '627cc493-f310-47de-96bd-71410b7dec09', + [ResourceAttributes.SERVICE_NAMESPACE]: 'shop', + [ResourceAttributes.SERVICE_VERSION]: '0.1.0', }); assertServiceResource(resource, { name: 'shoppingcart', diff --git a/packages/opentelemetry-resources/test/util/resource-assertions.ts b/packages/opentelemetry-resources/test/util/resource-assertions.ts index e0fd11e974..5284e83575 100644 --- a/packages/opentelemetry-resources/test/util/resource-assertions.ts +++ b/packages/opentelemetry-resources/test/util/resource-assertions.ts @@ -17,15 +17,7 @@ import { SDK_INFO } from '@opentelemetry/core'; import * as assert from 'assert'; import { Resource } from '../../src/Resource'; -import { - CLOUD_RESOURCE, - CONTAINER_RESOURCE, - HOST_RESOURCE, - K8S_RESOURCE, - TELEMETRY_SDK_RESOURCE, - SERVICE_RESOURCE, - PROCESS_RESOURCE, -} from '../../src/constants'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; /** * Test utility method to validate a cloud resource @@ -42,25 +34,25 @@ export const assertCloudResource = ( zone?: string; } ) => { - assertHasOneLabel(CLOUD_RESOURCE, resource); + assertHasOneLabel('CLOUD', resource); if (validations.provider) assert.strictEqual( - resource.attributes[CLOUD_RESOURCE.PROVIDER], + resource.attributes[ResourceAttributes.CLOUD_PROVIDER], validations.provider ); if (validations.accountId) assert.strictEqual( - resource.attributes[CLOUD_RESOURCE.ACCOUNT_ID], + resource.attributes[ResourceAttributes.CLOUD_ACCOUNT_ID], validations.accountId ); if (validations.region) assert.strictEqual( - resource.attributes[CLOUD_RESOURCE.REGION], + resource.attributes[ResourceAttributes.CLOUD_REGION], validations.region ); if (validations.zone) assert.strictEqual( - resource.attributes[CLOUD_RESOURCE.ZONE], + resource.attributes[ResourceAttributes.CLOUD_AVAILABILITY_ZONE], validations.zone ); }; @@ -80,25 +72,25 @@ export const assertContainerResource = ( imageTag?: string; } ) => { - assertHasOneLabel(CONTAINER_RESOURCE, resource); + assertHasOneLabel('CONTAINER', resource); if (validations.name) assert.strictEqual( - resource.attributes[CONTAINER_RESOURCE.NAME], + resource.attributes[ResourceAttributes.CONTAINER_NAME], validations.name ); if (validations.id) assert.strictEqual( - resource.attributes[CONTAINER_RESOURCE.ID], + resource.attributes[ResourceAttributes.CONTAINER_ID], validations.id ); if (validations.imageName) assert.strictEqual( - resource.attributes[CONTAINER_RESOURCE.IMAGE_NAME], + resource.attributes[ResourceAttributes.CONTAINER_IMAGE_NAME], validations.imageName ); if (validations.imageTag) assert.strictEqual( - resource.attributes[CONTAINER_RESOURCE.IMAGE_TAG], + resource.attributes[ResourceAttributes.CONTAINER_IMAGE_TAG], validations.imageTag ); }; @@ -121,32 +113,35 @@ export const assertHostResource = ( imageVersion?: string; } ) => { - assertHasOneLabel(HOST_RESOURCE, resource); + assertHasOneLabel('HOST', resource); if (validations.id) - assert.strictEqual(resource.attributes[HOST_RESOURCE.ID], validations.id); + assert.strictEqual( + resource.attributes[ResourceAttributes.HOST_ID], + validations.id + ); if (validations.name) assert.strictEqual( - resource.attributes[HOST_RESOURCE.NAME], + resource.attributes[ResourceAttributes.HOST_NAME], validations.name ); if (validations.hostType) assert.strictEqual( - resource.attributes[HOST_RESOURCE.TYPE], + resource.attributes[ResourceAttributes.HOST_TYPE], validations.hostType ); if (validations.imageName) assert.strictEqual( - resource.attributes[HOST_RESOURCE.IMAGE_NAME], + resource.attributes[ResourceAttributes.HOST_IMAGE_NAME], validations.imageName ); if (validations.imageId) assert.strictEqual( - resource.attributes[HOST_RESOURCE.IMAGE_ID], + resource.attributes[ResourceAttributes.HOST_IMAGE_ID], validations.imageId ); if (validations.imageVersion) assert.strictEqual( - resource.attributes[HOST_RESOURCE.IMAGE_VERSION], + resource.attributes[ResourceAttributes.HOST_IMAGE_VERSION], validations.imageVersion ); }; @@ -166,25 +161,25 @@ export const assertK8sResource = ( deploymentName?: string; } ) => { - assertHasOneLabel(K8S_RESOURCE, resource); + assertHasOneLabel('K8S', resource); if (validations.clusterName) assert.strictEqual( - resource.attributes[K8S_RESOURCE.CLUSTER_NAME], + resource.attributes[ResourceAttributes.K8S_CLUSTER_NAME], validations.clusterName ); if (validations.namespaceName) assert.strictEqual( - resource.attributes[K8S_RESOURCE.NAMESPACE_NAME], + resource.attributes[ResourceAttributes.K8S_NAMESPACE_NAME], validations.namespaceName ); if (validations.podName) assert.strictEqual( - resource.attributes[K8S_RESOURCE.POD_NAME], + resource.attributes[ResourceAttributes.K8S_POD_NAME], validations.podName ); if (validations.deploymentName) assert.strictEqual( - resource.attributes[K8S_RESOURCE.DEPLOYMENT_NAME], + resource.attributes[ResourceAttributes.K8S_DEPLOYMENT_NAME], validations.deploymentName ); }; @@ -212,17 +207,17 @@ export const assertTelemetrySDKResource = ( if (validations.name) assert.strictEqual( - resource.attributes[TELEMETRY_SDK_RESOURCE.NAME], + resource.attributes[ResourceAttributes.TELEMETRY_SDK_NAME], validations.name ); if (validations.language) assert.strictEqual( - resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], + resource.attributes[ResourceAttributes.TELEMETRY_SDK_LANGUAGE], validations.language ); if (validations.version) assert.strictEqual( - resource.attributes[TELEMETRY_SDK_RESOURCE.VERSION], + resource.attributes[ResourceAttributes.TELEMETRY_SDK_VERSION], validations.version ); }; @@ -243,21 +238,21 @@ export const assertServiceResource = ( } ) => { assert.strictEqual( - resource.attributes[SERVICE_RESOURCE.NAME], + resource.attributes[ResourceAttributes.SERVICE_NAME], validations.name ); assert.strictEqual( - resource.attributes[SERVICE_RESOURCE.INSTANCE_ID], + resource.attributes[ResourceAttributes.SERVICE_INSTANCE_ID], validations.instanceId ); if (validations.namespace) assert.strictEqual( - resource.attributes[SERVICE_RESOURCE.NAMESPACE], + resource.attributes[ResourceAttributes.SERVICE_NAMESPACE], validations.namespace ); if (validations.version) assert.strictEqual( - resource.attributes[SERVICE_RESOURCE.VERSION], + resource.attributes[ResourceAttributes.SERVICE_VERSION], validations.version ); }; @@ -278,24 +273,24 @@ export const assertProcessResource = ( } ) => { assert.strictEqual( - resource.attributes[PROCESS_RESOURCE.PID], + resource.attributes[ResourceAttributes.PROCESS_PID], validations.pid ); if (validations.name) { assert.strictEqual( - resource.attributes[PROCESS_RESOURCE.NAME], + resource.attributes[ResourceAttributes.PROCESS_EXECUTABLE_NAME], validations.name ); } if (validations.command) { assert.strictEqual( - resource.attributes[PROCESS_RESOURCE.COMMAND], + resource.attributes[ResourceAttributes.PROCESS_COMMAND], validations.command ); } if (validations.commandLine) { assert.strictEqual( - resource.attributes[PROCESS_RESOURCE.COMMAND_LINE], + resource.attributes[ResourceAttributes.PROCESS_COMMAND_LINE], validations.commandLine ); } @@ -310,18 +305,24 @@ export const assertEmptyResource = (resource: Resource) => { assert.strictEqual(Object.keys(resource.attributes).length, 0); }; -const assertHasOneLabel = ( - constants: { [key: string]: string }, - resource: Resource -): void => { - const hasOne = Object.values(constants).reduce( - // eslint-disable-next-line no-prototype-builtins - (found, key) => found || resource.attributes.hasOwnProperty(key), - false - ); +const assertHasOneLabel = (prefix: string, resource: Resource): void => { + const hasOne = Object.entries(ResourceAttributes).find(([key, value]) => { + return ( + key.startsWith(prefix) && + Object.prototype.hasOwnProperty.call(resource.attributes, value) + ); + }); + assert.ok( hasOne, 'Resource must have one of the following attributes: ' + - Object.values(constants).join(', ') + Object.entries(ResourceAttributes) + .reduce((result, [key, value]) => { + if (key.startsWith(prefix)) { + result.push(value); + } + return result; + }) + .join(', ') ); }; diff --git a/packages/opentelemetry-resources/test/util/sample-detector.ts b/packages/opentelemetry-resources/test/util/sample-detector.ts index 1ed6f258ef..9e871557c8 100644 --- a/packages/opentelemetry-resources/test/util/sample-detector.ts +++ b/packages/opentelemetry-resources/test/util/sample-detector.ts @@ -14,17 +14,18 @@ * limitations under the License. */ -import { Detector, Resource, CLOUD_RESOURCE, HOST_RESOURCE } from '../../src'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; +import { Detector, Resource } from '../../src'; class SampleDetector implements Detector { async detect(): Promise { return new Resource({ - [CLOUD_RESOURCE.PROVIDER]: 'provider', - [CLOUD_RESOURCE.ACCOUNT_ID]: 'accountId', - [CLOUD_RESOURCE.REGION]: 'region', - [CLOUD_RESOURCE.ZONE]: 'zone', - [HOST_RESOURCE.ID]: 'instanceId', - [HOST_RESOURCE.TYPE]: 'instanceType', + [ResourceAttributes.CLOUD_PROVIDER]: 'provider', + [ResourceAttributes.CLOUD_ACCOUNT_ID]: 'accountId', + [ResourceAttributes.CLOUD_REGION]: 'region', + [ResourceAttributes.CLOUD_AVAILABILITY_ZONE]: 'zone', + [ResourceAttributes.HOST_ID]: 'instanceId', + [ResourceAttributes.HOST_TYPE]: 'instanceType', }); } } diff --git a/packages/opentelemetry-resources/tsconfig.json b/packages/opentelemetry-resources/tsconfig.json index 789e61fc86..51dcc3ed88 100644 --- a/packages/opentelemetry-resources/tsconfig.json +++ b/packages/opentelemetry-resources/tsconfig.json @@ -11,6 +11,9 @@ "references": [ { "path": "../opentelemetry-core" + }, + { + "path": "../opentelemetry-semantic-conventions" } ] } diff --git a/packages/opentelemetry-web/test/WebTracerProvider.test.ts b/packages/opentelemetry-web/test/WebTracerProvider.test.ts index e13d9b9ac5..a73845bfff 100644 --- a/packages/opentelemetry-web/test/WebTracerProvider.test.ts +++ b/packages/opentelemetry-web/test/WebTracerProvider.test.ts @@ -17,7 +17,8 @@ import { context, getSpan, setSpan, ContextManager } from '@opentelemetry/api'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import { B3Propagator } from '@opentelemetry/propagator-b3'; -import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; +import { Resource } from '@opentelemetry/resources'; +import { ResourceAttributes } from '@opentelemetry/semantic-conventions'; import { Span, Tracer } from '@opentelemetry/tracing'; import * as assert from 'assert'; import { WebTracerConfig } from '../src'; @@ -131,7 +132,7 @@ describe('WebTracerProvider', () => { assert.ok(span); assert.ok(span.resource instanceof Resource); assert.equal( - span.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], + span.resource.attributes[ResourceAttributes.TELEMETRY_SDK_LANGUAGE], 'webjs' ); });