From 27e6c2da594dad9157fdd6d21dbeddba137261f8 Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Fri, 14 Aug 2020 12:03:48 -0700 Subject: [PATCH] chore!: refer to resource labels as attributes (#1419) Co-authored-by: Bartlomiej Obecny Co-authored-by: Daniel Dyla --- .../src/transform.ts | 2 +- .../src/transform.ts | 4 +- .../src/transform.ts | 4 +- .../src/zipkin.ts | 2 +- .../test/NodeTracerProvider.test.ts | 2 +- .../opentelemetry-resources/src/Resource.ts | 22 +++--- .../src/platform/node/detect-resources.ts | 4 +- .../platform/node/detectors/AwsEc2Detector.ts | 2 +- .../platform/node/detectors/EnvDetector.ts | 65 ++++++++-------- .../platform/node/detectors/GcpDetector.ts | 33 ++++---- packages/opentelemetry-resources/src/types.ts | 4 +- .../test/Resource.test.ts | 20 ++--- .../test/detect-resources.test.ts | 10 +-- .../test/detectors/EnvDetector.test.ts | 4 +- .../test/detectors/GcpDetector.test.ts | 2 +- .../test/resource-assertions.test.ts | 14 ++-- .../test/util/resource-assertions.ts | 77 +++++++++++-------- .../test/WebTracerProvider.test.ts | 2 +- 18 files changed, 147 insertions(+), 126 deletions(-) diff --git a/packages/opentelemetry-exporter-collector/src/transform.ts b/packages/opentelemetry-exporter-collector/src/transform.ts index 4c11bf8c4a..005f41a1bc 100644 --- a/packages/opentelemetry-exporter-collector/src/transform.ts +++ b/packages/opentelemetry-exporter-collector/src/transform.ts @@ -188,7 +188,7 @@ export function toCollectorResource( const attr = Object.assign( {}, additionalAttributes, - resource ? resource.labels : {} + resource ? resource.attributes : {} ); const resourceProto: opentelemetryProto.resource.v1.Resource = { attributes: toCollectorAttributes(attr), diff --git a/packages/opentelemetry-exporter-jaeger/src/transform.ts b/packages/opentelemetry-exporter-jaeger/src/transform.ts index e1ff8d88d2..17c1cbfbbf 100644 --- a/packages/opentelemetry-exporter-jaeger/src/transform.ts +++ b/packages/opentelemetry-exporter-jaeger/src/transform.ts @@ -64,10 +64,10 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan { if (span.kind !== undefined) { tags.push({ key: 'span.kind', value: SpanKind[span.kind] }); } - Object.keys(span.resource.labels).forEach(name => + Object.keys(span.resource.attributes).forEach(name => tags.push({ key: name, - value: toTagValue(span.resource.labels[name]), + value: toTagValue(span.resource.attributes[name]), }) ); diff --git a/packages/opentelemetry-exporter-zipkin/src/transform.ts b/packages/opentelemetry-exporter-zipkin/src/transform.ts index aebd956a93..1fa071542a 100644 --- a/packages/opentelemetry-exporter-zipkin/src/transform.ts +++ b/packages/opentelemetry-exporter-zipkin/src/transform.ts @@ -83,8 +83,8 @@ export function _toZipkinTags( tags[statusDescriptionTagName] = status.message; } - Object.keys(resource.labels).forEach( - name => (tags[name] = resource.labels[name]) + Object.keys(resource.attributes).forEach( + name => (tags[name] = resource.attributes[name]) ); return tags; diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index dcf5a4b2b0..54010b3af3 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -73,7 +73,7 @@ export class ZipkinExporter implements SpanExporter { ) { if (typeof this._serviceName !== 'string') { this._serviceName = String( - spans[0].resource.labels[SERVICE_RESOURCE.NAME] || + spans[0].resource.attributes[SERVICE_RESOURCE.NAME] || this.DEFAULT_SERVICE_NAME ); } diff --git a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts index 1846819bf4..d1b00006b1 100644 --- a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts +++ b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts @@ -193,7 +193,7 @@ describe('NodeTracerProvider', () => { assert.ok(span); assert.ok(span.resource instanceof Resource); assert.equal( - span.resource.labels[TELEMETRY_SDK_RESOURCE.LANGUAGE], + span.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], 'nodejs' ); }); diff --git a/packages/opentelemetry-resources/src/Resource.ts b/packages/opentelemetry-resources/src/Resource.ts index f8dc503c1c..eaf17b245f 100644 --- a/packages/opentelemetry-resources/src/Resource.ts +++ b/packages/opentelemetry-resources/src/Resource.ts @@ -16,7 +16,7 @@ import { SDK_INFO } from '@opentelemetry/core'; import { TELEMETRY_SDK_RESOURCE } from './constants'; -import { ResourceLabels } from './types'; +import { ResourceAttributes } from './types'; /** * A Resource describes the entity for which a signals (metrics or trace) are @@ -45,11 +45,11 @@ export class Resource { constructor( /** - * A dictionary of labels with string keys and values that provide information - * about the entity as numbers, strings or booleans - * TODO: Consider to add check/validation on labels. + * A dictionary of attributes with string keys and values that provide + * information about the entity as numbers, strings or booleans + * TODO: Consider to add check/validation on attributes. */ - readonly labels: ResourceLabels + readonly attributes: ResourceAttributes ) {} /** @@ -61,10 +61,14 @@ export class Resource { * @returns the newly merged Resource. */ merge(other: Resource | null): Resource { - if (!other || !Object.keys(other.labels).length) return this; + if (!other || !Object.keys(other.attributes).length) return this; - // Labels from resource overwrite labels from other resource. - const mergedLabels = Object.assign({}, other.labels, this.labels); - return new Resource(mergedLabels); + // Attributes from resource overwrite attributes from other resource. + const mergedAttributes = Object.assign( + {}, + other.attributes, + this.attributes + ); + return new Resource(mergedAttributes); } } diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index ca7add14b2..62a3ac8c19 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -71,8 +71,8 @@ export const detectResources = async ( const logResources = (logger: Logger, resources: Array) => { resources.forEach((resource, index) => { // Print only populated resources - if (Object.keys(resource.labels).length > 0) { - const resourceDebugString = util.inspect(resource.labels, { + if (Object.keys(resource.attributes).length > 0) { + const resourceDebugString = util.inspect(resource.attributes, { depth: 2, breakLength: Infinity, sorted: true, diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index 76fd11527a..fd248b822f 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -36,7 +36,7 @@ class AwsEc2Detector implements Detector { /** * Attempts to connect and obtain an AWS instance Identity document. If the * connection is succesful it returns a promise containing a {@link Resource} - * populated with instance metadata as labels. Returns a promise containing an + * populated with instance metadata. Returns a promise containing an * empty {@link Resource} if the connection or parsing of the identity * document fails. * diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 718c566f12..7e22bdf786 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -15,21 +15,21 @@ */ import { Resource } from '../../../Resource'; -import { Detector, ResourceLabels } from '../../../types'; +import { Detector, ResourceAttributes } from '../../../types'; import { ResourceDetectionConfigWithLogger } from '../../../config'; /** * EnvDetector can be used to detect the presence of and create a Resource - * from the OTEL_RESOURCE_LABELS environment variable. + * from the OTEL_RESOURCE_ATTRIBUTES environment variable. */ class EnvDetector implements Detector { - // Type, label keys, and label values should not exceed 256 characters. + // Type, attribute keys, and attribute values should not exceed 256 characters. private readonly _MAX_LENGTH = 255; - // OTEL_RESOURCE_LABELS is a comma-separated list of labels. + // OTEL_RESOURCE_ATTRIBUTES is a comma-separated list of attributes. private readonly _COMMA_SEPARATOR = ','; - // OTEL_RESOURCE_LABELS contains key value pair separated by '='. + // OTEL_RESOURCE_ATTRIBUTES contains key value pair separated by '='. private readonly _LABEL_KEY_VALUE_SPLITTER = '='; private readonly _ERROR_MESSAGE_INVALID_CHARS = @@ -43,25 +43,23 @@ class EnvDetector implements Detector { ' characters.'; /** - * Returns a {@link Resource} populated with labels from the - * OTEL_RESOURCE_LABELS environment variable. Note this is an async function - * to conform to the Detector interface. + * Returns a {@link Resource} populated with attributes from the + * OTEL_RESOURCE_ATTRIBUTES environment variable. Note this is an async + * function to conform to the Detector interface. * * @param config The resource detection config with a required logger */ async detect(config: ResourceDetectionConfigWithLogger): Promise { try { - const labelString = process.env.OTEL_RESOURCE_LABELS; - if (!labelString) { + const rawAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES; + if (!rawAttributes) { config.logger.debug( - 'EnvDetector failed: Environment variable "OTEL_RESOURCE_LABELS" is missing.' + 'EnvDetector failed: Environment variable "OTEL_RESOURCE_ATTRIBUTES" is missing.' ); return Resource.empty(); } - const labels = this._parseResourceLabels( - process.env.OTEL_RESOURCE_LABELS - ); - return new Resource(labels); + const attributes = this._parseResourceAttributes(rawAttributes); + return new Resource(attributes); } catch (e) { config.logger.debug(`EnvDetector failed: ${e.message}`); return Resource.empty(); @@ -69,24 +67,31 @@ class EnvDetector implements Detector { } /** - * Creates a label map from the OTEL_RESOURCE_LABELS environment variable. + * Creates an attribute map from the OTEL_RESOURCE_ATTRIBUTES environment + * variable. * - * OTEL_RESOURCE_LABELS: A comma-separated list of labels describing the - * source in more detail, e.g. “key1=val1,key2=val2”. Domain names and paths - * are accepted as label keys. Values may be quoted or unquoted in general. If - * a value contains whitespaces, =, or " characters, it must always be quoted. + * OTEL_RESOURCE_ATTRIBUTES: A comma-separated list of attributes describing + * the source in more detail, e.g. “key1=val1,key2=val2”. Domain names and + * paths are accepted as attribute keys. Values may be quoted or unquoted in + * general. If a value contains whitespaces, =, or " characters, it must + * always be quoted. * - * @param rawEnvLabels The resource labels as a comma-seperated list + * @param rawEnvAttributes The resource attributes as a comma-seperated list * of key/value pairs. - * @returns The sanitized resource labels. + * @returns The sanitized resource attributes. */ - private _parseResourceLabels(rawEnvLabels?: string): ResourceLabels { - if (!rawEnvLabels) return {}; + private _parseResourceAttributes( + rawEnvAttributes?: string + ): ResourceAttributes { + if (!rawEnvAttributes) return {}; - const labels: ResourceLabels = {}; - const rawLabels: string[] = rawEnvLabels.split(this._COMMA_SEPARATOR, -1); - for (const rawLabel of rawLabels) { - const keyValuePair: string[] = rawLabel.split( + const attributes: ResourceAttributes = {}; + const rawAttributes: string[] = rawEnvAttributes.split( + this._COMMA_SEPARATOR, + -1 + ); + for (const rawAttribute of rawAttributes) { + const keyValuePair: string[] = rawAttribute.split( this._LABEL_KEY_VALUE_SPLITTER, -1 ); @@ -103,9 +108,9 @@ class EnvDetector implements Detector { if (!this._isValid(value)) { throw new Error(`Label value ${this._ERROR_MESSAGE_INVALID_VALUE}`); } - labels[key] = value; + attributes[key] = value; } - return labels; + return attributes; } /** diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index a93173d1a1..4819599511 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -17,7 +17,7 @@ import * as os from 'os'; import * as gcpMetadata from 'gcp-metadata'; import { Resource } from '../../../Resource'; -import { Detector, ResourceLabels } from '../../../types'; +import { Detector, ResourceAttributes } from '../../../types'; import { CLOUD_RESOURCE, HOST_RESOURCE, @@ -35,7 +35,7 @@ class GcpDetector implements Detector { /** * Attempts to connect and obtain instance configuration data from the GCP metadata service. * If the connection is succesful it returns a promise containing a {@link Resource} - * populated with instance metadata as labels. Returns a promise containing an + * populated with instance metadata. Returns a promise containing an * empty {@link Resource} if the connection or parsing of the metadata fails. * * @param config The resource detection config with a required logger @@ -53,24 +53,27 @@ class GcpDetector implements Detector { this._getClusterName(), ]); - const labels: ResourceLabels = {}; - labels[CLOUD_RESOURCE.ACCOUNT_ID] = projectId; - labels[HOST_RESOURCE.ID] = instanceId; - labels[CLOUD_RESOURCE.ZONE] = zoneId; - labels[CLOUD_RESOURCE.PROVIDER] = 'gcp'; + const attributes: ResourceAttributes = {}; + attributes[CLOUD_RESOURCE.ACCOUNT_ID] = projectId; + attributes[HOST_RESOURCE.ID] = instanceId; + attributes[CLOUD_RESOURCE.ZONE] = zoneId; + attributes[CLOUD_RESOURCE.PROVIDER] = 'gcp'; if (process.env.KUBERNETES_SERVICE_HOST) - this._addK8sLabels(labels, clusterName); + this._addK8sAttributes(attributes, clusterName); - return new Resource(labels); + return new Resource(attributes); } - /** Add resource labels for K8s */ - private _addK8sLabels(labels: ResourceLabels, clusterName: string): void { - labels[K8S_RESOURCE.CLUSTER_NAME] = clusterName; - labels[K8S_RESOURCE.NAMESPACE_NAME] = process.env.NAMESPACE || ''; - labels[K8S_RESOURCE.POD_NAME] = process.env.HOSTNAME || os.hostname(); - labels[CONTAINER_RESOURCE.NAME] = process.env.CONTAINER_NAME || ''; + /** Add resource attributes for K8s */ + private _addK8sAttributes( + attributes: ResourceAttributes, + clusterName: string + ): void { + attributes[K8S_RESOURCE.CLUSTER_NAME] = clusterName; + attributes[K8S_RESOURCE.NAMESPACE_NAME] = process.env.NAMESPACE || ''; + attributes[K8S_RESOURCE.POD_NAME] = process.env.HOSTNAME || os.hostname(); + attributes[CONTAINER_RESOURCE.NAME] = process.env.CONTAINER_NAME || ''; } /** Gets project id from GCP project metadata. */ diff --git a/packages/opentelemetry-resources/src/types.ts b/packages/opentelemetry-resources/src/types.ts index c33562312f..d31d17515d 100644 --- a/packages/opentelemetry-resources/src/types.ts +++ b/packages/opentelemetry-resources/src/types.ts @@ -17,8 +17,8 @@ import { Resource } from './Resource'; import { ResourceDetectionConfigWithLogger } from './config'; -/** Interface for Resource labels */ -export interface ResourceLabels { +/** Interface for Resource attributes */ +export interface ResourceAttributes { [key: string]: number | string | boolean; } diff --git a/packages/opentelemetry-resources/test/Resource.test.ts b/packages/opentelemetry-resources/test/Resource.test.ts index 0c5d0feb9f..3203148fc4 100644 --- a/packages/opentelemetry-resources/test/Resource.test.ts +++ b/packages/opentelemetry-resources/test/Resource.test.ts @@ -44,11 +44,11 @@ describe('Resource', () => { 'k8s.io/location': 'location', }); const actualResource = resource1.merge(resource2); - assert.strictEqual(Object.keys(actualResource.labels).length, 5); + assert.strictEqual(Object.keys(actualResource.attributes).length, 5); assert.deepStrictEqual(actualResource, expectedResource); }); - it('should return merged resource when collision in labels', () => { + it('should return merged resource when collision in attributes', () => { const expectedResource = new Resource({ 'k8s.io/container/name': 'c1', 'k8s.io/namespace/name': 'default', @@ -56,25 +56,25 @@ describe('Resource', () => { 'k8s.io/location': 'location1', }); const actualResource = resource1.merge(resource3); - assert.strictEqual(Object.keys(actualResource.labels).length, 4); + assert.strictEqual(Object.keys(actualResource.attributes).length, 4); assert.deepStrictEqual(actualResource, expectedResource); }); it('should return merged resource when first resource is empty', () => { const actualResource = emptyResource.merge(resource2); - assert.strictEqual(Object.keys(actualResource.labels).length, 2); + assert.strictEqual(Object.keys(actualResource.attributes).length, 2); assert.deepStrictEqual(actualResource, resource2); }); it('should return merged resource when other resource is empty', () => { const actualResource = resource1.merge(emptyResource); - assert.strictEqual(Object.keys(actualResource.labels).length, 3); + assert.strictEqual(Object.keys(actualResource.attributes).length, 3); assert.deepStrictEqual(actualResource, resource1); }); it('should return merged resource when other resource is null', () => { const actualResource = resource1.merge(null); - assert.strictEqual(Object.keys(actualResource.labels).length, 3); + assert.strictEqual(Object.keys(actualResource.attributes).length, 3); assert.deepStrictEqual(actualResource, resource1); }); @@ -84,15 +84,15 @@ describe('Resource', () => { 'custom.number': 42, 'custom.boolean': true, }); - assert.equal(resource.labels['custom.string'], 'strvalue'); - assert.equal(resource.labels['custom.number'], 42); - assert.equal(resource.labels['custom.boolean'], true); + assert.equal(resource.attributes['custom.string'], 'strvalue'); + assert.equal(resource.attributes['custom.number'], 42); + assert.equal(resource.attributes['custom.boolean'], true); }); describe('.empty()', () => { it('should return an empty resource', () => { const resource = Resource.empty(); - assert.equal(Object.entries(resource.labels), 0); + assert.equal(Object.entries(resource.attributes), 0); }); it('should return the same empty resource', () => { diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index b607efa0d6..aed604b481 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -58,14 +58,14 @@ const mockedAwsResponse = { describe('detectResources', async () => { beforeEach(() => { nock.disableNetConnect(); - process.env.OTEL_RESOURCE_LABELS = + process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.instance.id=627cc493,service.name=my-service,service.namespace=default,service.version=0.0.1'; }); afterEach(() => { nock.cleanAll(); nock.enableNetConnect(); - delete process.env.OTEL_RESOURCE_LABELS; + delete process.env.OTEL_RESOURCE_ATTRIBUTES; }); describe('in GCP environment', () => { @@ -224,7 +224,7 @@ describe('detectResources', async () => { describe('with missing environemnt variable', () => { beforeEach(() => { - delete process.env.OTEL_RESOURCE_LABELS; + delete process.env.OTEL_RESOURCE_ATTRIBUTES; }); it('prints correct error messages when EnvDetector has no env variable', async () => { @@ -241,7 +241,7 @@ describe('detectResources', async () => { assert.ok( callArgsContains( mockedLoggerMethod, - 'EnvDetector failed: Environment variable "OTEL_RESOURCE_LABELS" is missing.' + 'EnvDetector failed: Environment variable "OTEL_RESOURCE_ATTRIBUTES" is missing.' ) ); }); @@ -249,7 +249,7 @@ describe('detectResources', async () => { describe('with a faulty environment variable', () => { beforeEach(() => { - process.env.OTEL_RESOURCE_LABELS = 'bad=~label'; + process.env.OTEL_RESOURCE_ATTRIBUTES = 'bad=~label'; }); it('prints correct error messages when EnvDetector has an invalid variable', async () => { diff --git a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts index 894de6e870..adc1e013a5 100644 --- a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts @@ -26,12 +26,12 @@ import { NoopLogger } from '@opentelemetry/core'; describe('envDetector()', () => { describe('with valid env', () => { before(() => { - process.env.OTEL_RESOURCE_LABELS = + process.env.OTEL_RESOURCE_ATTRIBUTES = 'k8s.pod.name="pod-xyz-123",k8s.cluster.name="c1",k8s.namespace.name="default"'; }); after(() => { - delete process.env.OTEL_RESOURCE_LABELS; + delete process.env.OTEL_RESOURCE_ATTRIBUTES; }); it('should return resource information from environment variable', async () => { diff --git a/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts b/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts index c476feb5a7..a4207c8918 100644 --- a/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts @@ -95,7 +95,7 @@ describe('gcpDetector', () => { assertHostResource(resource, { id: '4520031799277582000' }); }); - it('should populate K8s labels resource when KUBERNETES_SERVICE_HOST is set', async () => { + it('should populate K8s attributes when KUBERNETES_SERVICE_HOST is set', async () => { process.env.KUBERNETES_SERVICE_HOST = 'my-host'; process.env.NAMESPACE = 'my-namespace'; process.env.HOSTNAME = 'my-hostname'; diff --git a/packages/opentelemetry-resources/test/resource-assertions.test.ts b/packages/opentelemetry-resources/test/resource-assertions.test.ts index 20e626daac..c3a99f5998 100644 --- a/packages/opentelemetry-resources/test/resource-assertions.test.ts +++ b/packages/opentelemetry-resources/test/resource-assertions.test.ts @@ -39,7 +39,7 @@ describe('assertCloudResource', () => { assertCloudResource(resource, {}); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [CLOUD_RESOURCE.PROVIDER]: 'gcp', [CLOUD_RESOURCE.ACCOUNT_ID]: 'opentelemetry', @@ -63,7 +63,7 @@ describe('assertContainerResource', () => { assertContainerResource(resource, {}); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [CONTAINER_RESOURCE.NAME]: 'opentelemetry-autoconf', [CONTAINER_RESOURCE.IMAGE_NAME]: 'gcr.io/opentelemetry/operator', @@ -85,7 +85,7 @@ describe('assertHostResource', () => { assertHostResource(resource, {}); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [HOST_RESOURCE.HOSTNAME]: 'opentelemetry-test-hostname', [HOST_RESOURCE.ID]: 'opentelemetry-test-id', @@ -116,7 +116,7 @@ describe('assertK8sResource', () => { assertK8sResource(resource, {}); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [K8S_RESOURCE.CLUSTER_NAME]: 'opentelemetry-cluster', [K8S_RESOURCE.NAMESPACE_NAME]: 'default', @@ -142,7 +142,7 @@ describe('assertTelemetrySDKResource', () => { assertTelemetrySDKResource(resource, {}); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [TELEMETRY_SDK_RESOURCE.NAME]: 'opentelemetry', [TELEMETRY_SDK_RESOURCE.LANGUAGE]: 'nodejs', @@ -157,7 +157,7 @@ describe('assertTelemetrySDKResource', () => { }); describe('assertServiceResource', () => { - it('validates required labels', () => { + it('validates required attributes', () => { const resource = new Resource({ [SERVICE_RESOURCE.NAME]: 'shoppingcart', [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', @@ -168,7 +168,7 @@ describe('assertServiceResource', () => { }); }); - it('validates optional labels', () => { + it('validates optional attributes', () => { const resource = new Resource({ [SERVICE_RESOURCE.NAME]: 'shoppingcart', [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', diff --git a/packages/opentelemetry-resources/test/util/resource-assertions.ts b/packages/opentelemetry-resources/test/util/resource-assertions.ts index a6db54b320..422415537c 100644 --- a/packages/opentelemetry-resources/test/util/resource-assertions.ts +++ b/packages/opentelemetry-resources/test/util/resource-assertions.ts @@ -30,7 +30,7 @@ import { * Test utility method to validate a cloud resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertCloudResource = ( resource: Resource, @@ -44,28 +44,31 @@ export const assertCloudResource = ( assertHasOneLabel(CLOUD_RESOURCE, resource); if (validations.provider) assert.strictEqual( - resource.labels[CLOUD_RESOURCE.PROVIDER], + resource.attributes[CLOUD_RESOURCE.PROVIDER], validations.provider ); if (validations.accountId) assert.strictEqual( - resource.labels[CLOUD_RESOURCE.ACCOUNT_ID], + resource.attributes[CLOUD_RESOURCE.ACCOUNT_ID], validations.accountId ); if (validations.region) assert.strictEqual( - resource.labels[CLOUD_RESOURCE.REGION], + resource.attributes[CLOUD_RESOURCE.REGION], validations.region ); if (validations.zone) - assert.strictEqual(resource.labels[CLOUD_RESOURCE.ZONE], validations.zone); + assert.strictEqual( + resource.attributes[CLOUD_RESOURCE.ZONE], + validations.zone + ); }; /** * Test utility method to validate a container resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertContainerResource = ( resource: Resource, @@ -78,17 +81,17 @@ export const assertContainerResource = ( assertHasOneLabel(CONTAINER_RESOURCE, resource); if (validations.name) assert.strictEqual( - resource.labels[CONTAINER_RESOURCE.NAME], + resource.attributes[CONTAINER_RESOURCE.NAME], validations.name ); if (validations.imageName) assert.strictEqual( - resource.labels[CONTAINER_RESOURCE.IMAGE_NAME], + resource.attributes[CONTAINER_RESOURCE.IMAGE_NAME], validations.imageName ); if (validations.imageTag) assert.strictEqual( - resource.labels[CONTAINER_RESOURCE.IMAGE_TAG], + resource.attributes[CONTAINER_RESOURCE.IMAGE_TAG], validations.imageTag ); }; @@ -97,7 +100,7 @@ export const assertContainerResource = ( * Test utility method to validate a host resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertHostResource = ( resource: Resource, @@ -114,31 +117,34 @@ export const assertHostResource = ( assertHasOneLabel(HOST_RESOURCE, resource); if (validations.hostName) assert.strictEqual( - resource.labels[HOST_RESOURCE.HOSTNAME], + resource.attributes[HOST_RESOURCE.HOSTNAME], validations.hostName ); if (validations.id) - assert.strictEqual(resource.labels[HOST_RESOURCE.ID], validations.id); + assert.strictEqual(resource.attributes[HOST_RESOURCE.ID], validations.id); if (validations.name) - assert.strictEqual(resource.labels[HOST_RESOURCE.NAME], validations.name); + assert.strictEqual( + resource.attributes[HOST_RESOURCE.NAME], + validations.name + ); if (validations.hostType) assert.strictEqual( - resource.labels[HOST_RESOURCE.TYPE], + resource.attributes[HOST_RESOURCE.TYPE], validations.hostType ); if (validations.imageName) assert.strictEqual( - resource.labels[HOST_RESOURCE.IMAGE_NAME], + resource.attributes[HOST_RESOURCE.IMAGE_NAME], validations.imageName ); if (validations.imageId) assert.strictEqual( - resource.labels[HOST_RESOURCE.IMAGE_ID], + resource.attributes[HOST_RESOURCE.IMAGE_ID], validations.imageId ); if (validations.imageVersion) assert.strictEqual( - resource.labels[HOST_RESOURCE.IMAGE_VERSION], + resource.attributes[HOST_RESOURCE.IMAGE_VERSION], validations.imageVersion ); }; @@ -147,7 +153,7 @@ export const assertHostResource = ( * Test utility method to validate a K8s resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertK8sResource = ( resource: Resource, @@ -161,22 +167,22 @@ export const assertK8sResource = ( assertHasOneLabel(K8S_RESOURCE, resource); if (validations.clusterName) assert.strictEqual( - resource.labels[K8S_RESOURCE.CLUSTER_NAME], + resource.attributes[K8S_RESOURCE.CLUSTER_NAME], validations.clusterName ); if (validations.namespaceName) assert.strictEqual( - resource.labels[K8S_RESOURCE.NAMESPACE_NAME], + resource.attributes[K8S_RESOURCE.NAMESPACE_NAME], validations.namespaceName ); if (validations.podName) assert.strictEqual( - resource.labels[K8S_RESOURCE.POD_NAME], + resource.attributes[K8S_RESOURCE.POD_NAME], validations.podName ); if (validations.deploymentName) assert.strictEqual( - resource.labels[K8S_RESOURCE.DEPLOYMENT_NAME], + resource.attributes[K8S_RESOURCE.DEPLOYMENT_NAME], validations.deploymentName ); }; @@ -185,7 +191,7 @@ export const assertK8sResource = ( * Test utility method to validate a telemetry sdk resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertTelemetrySDKResource = ( resource: Resource, @@ -204,17 +210,17 @@ export const assertTelemetrySDKResource = ( if (validations.name) assert.strictEqual( - resource.labels[TELEMETRY_SDK_RESOURCE.NAME], + resource.attributes[TELEMETRY_SDK_RESOURCE.NAME], validations.name ); if (validations.language) assert.strictEqual( - resource.labels[TELEMETRY_SDK_RESOURCE.LANGUAGE], + resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], validations.language ); if (validations.version) assert.strictEqual( - resource.labels[TELEMETRY_SDK_RESOURCE.VERSION], + resource.attributes[TELEMETRY_SDK_RESOURCE.VERSION], validations.version ); }; @@ -223,7 +229,7 @@ export const assertTelemetrySDKResource = ( * Test utility method to validate a service resource * * @param resource the Resource to validate - * @param validations validations for the resource labels + * @param validations validations for the resource attributes */ export const assertServiceResource = ( resource: Resource, @@ -234,19 +240,22 @@ export const assertServiceResource = ( version?: string; } ) => { - assert.strictEqual(resource.labels[SERVICE_RESOURCE.NAME], validations.name); assert.strictEqual( - resource.labels[SERVICE_RESOURCE.INSTANCE_ID], + resource.attributes[SERVICE_RESOURCE.NAME], + validations.name + ); + assert.strictEqual( + resource.attributes[SERVICE_RESOURCE.INSTANCE_ID], validations.instanceId ); if (validations.namespace) assert.strictEqual( - resource.labels[SERVICE_RESOURCE.NAMESPACE], + resource.attributes[SERVICE_RESOURCE.NAMESPACE], validations.namespace ); if (validations.version) assert.strictEqual( - resource.labels[SERVICE_RESOURCE.VERSION], + resource.attributes[SERVICE_RESOURCE.VERSION], validations.version ); }; @@ -257,7 +266,7 @@ export const assertServiceResource = ( * @param resource the Resource to validate */ export const assertEmptyResource = (resource: Resource) => { - assert.strictEqual(Object.keys(resource.labels).length, 0); + assert.strictEqual(Object.keys(resource.attributes).length, 0); }; const assertHasOneLabel = ( @@ -266,12 +275,12 @@ const assertHasOneLabel = ( ): void => { const hasOne = Object.values(constants).reduce( // eslint-disable-next-line no-prototype-builtins - (found, key) => found || resource.labels.hasOwnProperty(key), + (found, key) => found || resource.attributes.hasOwnProperty(key), false ); assert.ok( hasOne, - 'Resource must have one of the following labels: ' + + 'Resource must have one of the following attributes: ' + Object.values(constants).join(', ') ); }; diff --git a/packages/opentelemetry-web/test/WebTracerProvider.test.ts b/packages/opentelemetry-web/test/WebTracerProvider.test.ts index 01b1590d96..e10c80405c 100644 --- a/packages/opentelemetry-web/test/WebTracerProvider.test.ts +++ b/packages/opentelemetry-web/test/WebTracerProvider.test.ts @@ -160,7 +160,7 @@ describe('WebTracerProvider', () => { assert.ok(span); assert.ok(span.resource instanceof Resource); assert.equal( - span.resource.labels[TELEMETRY_SDK_RESOURCE.LANGUAGE], + span.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE], 'webjs' ); });