diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 2ecf08018b..0809bf6553 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -11,6 +11,7 @@ * feat(sdk-metrics)!: remove MeterProvider.addMetricReader() in favor of constructor option [#4419](https://github.com/open-telemetry/opentelemetry-js/pull/4419) @pichlermarc * feat(sdk-metrics)!: replace attributeKeys with custom processors option [#4532](https://github.com/open-telemetry/opentelemetry-js/pull/4532) @pichlermarc * refactor(sdk-trace-base)!: replace `SpanAttributes` with `Attributes` [#5009](https://github.com/open-telemetry/opentelemetry-js/pull/5009) @david-luna +* refactor(resources)!: replace `ResourceAttributes` with `Attributes` [#5016](https://github.com/open-telemetry/opentelemetry-js/pull/5016) @david-luna ### :rocket: (Enhancement) diff --git a/package-lock.json b/package-lock.json index 605f2d7f56..97c3233c8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31649,7 +31649,7 @@ "node": ">=18" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.10.0" + "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "packages/opentelemetry-resources/node_modules/@opentelemetry/api": { diff --git a/packages/opentelemetry-resources/package.json b/packages/opentelemetry-resources/package.json index 6cf8100684..c31cd2bb40 100644 --- a/packages/opentelemetry-resources/package.json +++ b/packages/opentelemetry-resources/package.json @@ -88,7 +88,7 @@ "webpack-merge": "5.10.0" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.10.0" + "@opentelemetry/api": ">=1.3.0 <1.10.0" }, "dependencies": { "@opentelemetry/core": "1.26.0", diff --git a/packages/opentelemetry-resources/src/IResource.ts b/packages/opentelemetry-resources/src/IResource.ts index b53a0e0244..c911904ce4 100644 --- a/packages/opentelemetry-resources/src/IResource.ts +++ b/packages/opentelemetry-resources/src/IResource.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ResourceAttributes } from './types'; +import { Attributes } from '@opentelemetry/api'; /** * An interface that represents a resource. A Resource describes the entity for which signals (metrics or trace) are @@ -33,7 +33,7 @@ export interface IResource { /** * @returns the Resource's attributes. */ - readonly attributes: ResourceAttributes; + readonly attributes: Attributes; /** * Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to diff --git a/packages/opentelemetry-resources/src/Resource.ts b/packages/opentelemetry-resources/src/Resource.ts index b6c5d8fdae..c2ec93ecff 100644 --- a/packages/opentelemetry-resources/src/Resource.ts +++ b/packages/opentelemetry-resources/src/Resource.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { diag } from '@opentelemetry/api'; +import { Attributes, diag } from '@opentelemetry/api'; import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_TELEMETRY_SDK_LANGUAGE, @@ -22,7 +22,6 @@ import { SEMRESATTRS_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; import { SDK_INFO } from '@opentelemetry/core'; -import { ResourceAttributes } from './types'; import { defaultServiceName } from './platform'; import { IResource } from './IResource'; @@ -32,9 +31,9 @@ import { IResource } from './IResource'; */ export class Resource implements IResource { static readonly EMPTY = new Resource({}); - private _syncAttributes?: ResourceAttributes; - private _asyncAttributesPromise?: Promise; - private _attributes?: ResourceAttributes; + private _syncAttributes?: Attributes; + private _asyncAttributesPromise?: Promise; + private _attributes?: Attributes; /** * Check if async attributes have resolved. This is useful to avoid awaiting @@ -72,8 +71,8 @@ export class Resource implements IResource { * information about the entity as numbers, strings or booleans * TODO: Consider to add check/validation on attributes. */ - attributes: ResourceAttributes, - asyncAttributesPromise?: Promise + attributes: Attributes, + asyncAttributesPromise?: Promise ) { this._attributes = attributes; this.asyncAttributesPending = asyncAttributesPromise != null; @@ -92,7 +91,7 @@ export class Resource implements IResource { ); } - get attributes(): ResourceAttributes { + get attributes(): Attributes { if (this.asyncAttributesPending) { diag.error( 'Accessing resource attributes before async attributes settled' diff --git a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts index 5fed355327..0e204920b7 100644 --- a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts @@ -14,12 +14,13 @@ * limitations under the License. */ +import { Attributes } from '@opentelemetry/api'; import { SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION, SEMRESATTRS_PROCESS_RUNTIME_NAME, SEMRESATTRS_PROCESS_RUNTIME_VERSION, } from '@opentelemetry/semantic-conventions'; -import { DetectorSync, ResourceAttributes } from '../types'; +import { DetectorSync } from '../types'; import { diag } from '@opentelemetry/api'; import { ResourceDetectionConfig } from '../config'; import { IResource } from '../IResource'; @@ -39,7 +40,7 @@ class BrowserDetectorSync implements DetectorSync { if (!isBrowser) { return Resource.empty(); } - const browserResource: ResourceAttributes = { + const browserResource: Attributes = { [SEMRESATTRS_PROCESS_RUNTIME_NAME]: 'browser', [SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser', [SEMRESATTRS_PROCESS_RUNTIME_VERSION]: navigator.userAgent, @@ -54,7 +55,7 @@ class BrowserDetectorSync implements DetectorSync { * @returns The sanitized resource attributes. */ private _getResourceAttributes( - browserResource: ResourceAttributes, + browserResource: Attributes, _config?: ResourceDetectionConfig ) { if (browserResource[SEMRESATTRS_PROCESS_RUNTIME_VERSION] === '') { diff --git a/packages/opentelemetry-resources/src/detectors/EnvDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/EnvDetectorSync.ts index 329d489ec4..cb64529cbd 100644 --- a/packages/opentelemetry-resources/src/detectors/EnvDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/EnvDetectorSync.ts @@ -14,11 +14,11 @@ * limitations under the License. */ -import { diag } from '@opentelemetry/api'; +import { Attributes, diag } from '@opentelemetry/api'; import { getEnv } from '@opentelemetry/core'; import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import { Resource } from '../Resource'; -import { DetectorSync, ResourceAttributes } from '../types'; +import { DetectorSync } from '../types'; import { ResourceDetectionConfig } from '../config'; import { IResource } from '../IResource'; @@ -54,7 +54,7 @@ class EnvDetectorSync implements DetectorSync { * @param config The resource detection config */ detect(_config?: ResourceDetectionConfig): IResource { - const attributes: ResourceAttributes = {}; + const attributes: Attributes = {}; const env = getEnv(); const rawAttributes = env.OTEL_RESOURCE_ATTRIBUTES; @@ -90,12 +90,10 @@ class EnvDetectorSync implements DetectorSync { * of key/value pairs. * @returns The sanitized resource attributes. */ - private _parseResourceAttributes( - rawEnvAttributes?: string - ): ResourceAttributes { + private _parseResourceAttributes(rawEnvAttributes?: string): Attributes { if (!rawEnvAttributes) return {}; - const attributes: ResourceAttributes = {}; + const attributes: Attributes = {}; const rawAttributes: string[] = rawEnvAttributes.split( this._COMMA_SEPARATOR, -1 diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts index 164b07db64..123976dc57 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts @@ -14,13 +14,14 @@ * limitations under the License. */ +import { Attributes } from '@opentelemetry/api'; import { SEMRESATTRS_HOST_ARCH, SEMRESATTRS_HOST_ID, SEMRESATTRS_HOST_NAME, } from '@opentelemetry/semantic-conventions'; import { Resource } from '../../../Resource'; -import { DetectorSync, ResourceAttributes } from '../../../types'; +import { DetectorSync } from '../../../types'; import { ResourceDetectionConfig } from '../../../config'; import { arch, hostname } from 'os'; import { normalizeArch } from './utils'; @@ -32,7 +33,7 @@ import { getMachineId } from './machine-id/getMachineId'; */ class HostDetectorSync implements DetectorSync { detect(_config?: ResourceDetectionConfig): Resource { - const attributes: ResourceAttributes = { + const attributes: Attributes = { [SEMRESATTRS_HOST_NAME]: hostname(), [SEMRESATTRS_HOST_ARCH]: normalizeArch(arch()), }; @@ -40,9 +41,9 @@ class HostDetectorSync implements DetectorSync { return new Resource(attributes, this._getAsyncAttributes()); } - private _getAsyncAttributes(): Promise { + private _getAsyncAttributes(): Promise { return getMachineId().then(machineId => { - const attributes: ResourceAttributes = {}; + const attributes: Attributes = {}; if (machineId) { attributes[SEMRESATTRS_HOST_ID] = machineId; } diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/OSDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/node/OSDetectorSync.ts index 19c10a504e..aca5fbca76 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/OSDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/OSDetectorSync.ts @@ -14,12 +14,13 @@ * limitations under the License. */ +import { Attributes } from '@opentelemetry/api'; import { SEMRESATTRS_OS_TYPE, SEMRESATTRS_OS_VERSION, } from '@opentelemetry/semantic-conventions'; import { Resource } from '../../../Resource'; -import { DetectorSync, ResourceAttributes } from '../../../types'; +import { DetectorSync } from '../../../types'; import { ResourceDetectionConfig } from '../../../config'; import { platform, release } from 'os'; import { normalizeType } from './utils'; @@ -30,7 +31,7 @@ import { normalizeType } from './utils'; */ class OSDetectorSync implements DetectorSync { detect(_config?: ResourceDetectionConfig): Resource { - const attributes: ResourceAttributes = { + const attributes: Attributes = { [SEMRESATTRS_OS_TYPE]: normalizeType(platform()), [SEMRESATTRS_OS_VERSION]: release(), }; diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetectorSync.ts index e2f9fdbf55..70523112c9 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetectorSync.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { diag } from '@opentelemetry/api'; +import { Attributes, diag } from '@opentelemetry/api'; import { SEMRESATTRS_PROCESS_COMMAND, SEMRESATTRS_PROCESS_COMMAND_ARGS, @@ -27,7 +27,7 @@ import { SEMRESATTRS_PROCESS_RUNTIME_VERSION, } from '@opentelemetry/semantic-conventions'; import { Resource } from '../../../Resource'; -import { DetectorSync, ResourceAttributes } from '../../../types'; +import { DetectorSync } from '../../../types'; import { ResourceDetectionConfig } from '../../../config'; import { IResource } from '../../../IResource'; import * as os from 'os'; @@ -38,7 +38,7 @@ import * as os from 'os'; */ class ProcessDetectorSync implements DetectorSync { detect(_config?: ResourceDetectionConfig): IResource { - const attributes: ResourceAttributes = { + const attributes: Attributes = { [SEMRESATTRS_PROCESS_PID]: process.pid, [SEMRESATTRS_PROCESS_EXECUTABLE_NAME]: process.title, [SEMRESATTRS_PROCESS_EXECUTABLE_PATH]: process.execPath, diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/ServiceInstanceIdDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/node/ServiceInstanceIdDetectorSync.ts index 7c0afae59e..306f90548d 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/ServiceInstanceIdDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/ServiceInstanceIdDetectorSync.ts @@ -14,9 +14,10 @@ * limitations under the License. */ +import { Attributes } from '@opentelemetry/api'; import { SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions'; import { Resource } from '../../../Resource'; -import { DetectorSync, ResourceAttributes } from '../../../types'; +import { DetectorSync } from '../../../types'; import { ResourceDetectionConfig } from '../../../config'; import { randomUUID } from 'crypto'; @@ -25,7 +26,7 @@ import { randomUUID } from 'crypto'; */ class ServiceInstanceIdDetectorSync implements DetectorSync { detect(_config?: ResourceDetectionConfig): Resource { - const attributes: ResourceAttributes = { + const attributes: Attributes = { [SEMRESATTRS_SERVICE_INSTANCE_ID]: randomUUID(), }; diff --git a/packages/opentelemetry-resources/src/index.ts b/packages/opentelemetry-resources/src/index.ts index 2162658c3d..4c93e1da78 100644 --- a/packages/opentelemetry-resources/src/index.ts +++ b/packages/opentelemetry-resources/src/index.ts @@ -17,7 +17,7 @@ export { Resource } from './Resource'; export { IResource } from './IResource'; export { defaultServiceName } from './platform'; -export { DetectorSync, ResourceAttributes, Detector } from './types'; +export { DetectorSync, Detector } from './types'; export { ResourceDetectionConfig } from './config'; export { browserDetector, diff --git a/packages/opentelemetry-resources/src/types.ts b/packages/opentelemetry-resources/src/types.ts index d86fc9da6e..9004cffdf0 100644 --- a/packages/opentelemetry-resources/src/types.ts +++ b/packages/opentelemetry-resources/src/types.ts @@ -15,15 +15,8 @@ */ import { ResourceDetectionConfig } from './config'; -import { Attributes } from '@opentelemetry/api'; import { IResource } from './IResource'; -/** - * Interface for Resource attributes. - */ -// TODO: replace ResourceAttributes with Attributes -export type ResourceAttributes = Attributes; - /** * @deprecated please use {@link DetectorSync} */ diff --git a/packages/opentelemetry-resources/test/Resource.test.ts b/packages/opentelemetry-resources/test/Resource.test.ts index 3916e1c985..900ca9bbeb 100644 --- a/packages/opentelemetry-resources/test/Resource.test.ts +++ b/packages/opentelemetry-resources/test/Resource.test.ts @@ -17,7 +17,7 @@ import * as sinon from 'sinon'; import * as assert from 'assert'; import { SDK_INFO } from '@opentelemetry/core'; -import { Resource, ResourceAttributes } from '../src'; +import { Resource } from '../src'; import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_TELEMETRY_SDK_LANGUAGE, @@ -25,7 +25,7 @@ import { SEMRESATTRS_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; import { describeBrowser, describeNode } from './util'; -import { diag } from '@opentelemetry/api'; +import { Attributes, diag } from '@opentelemetry/api'; import { Resource as Resource190 } from '@opentelemetry/resources_1.9.0'; describe('Resource', () => { @@ -167,7 +167,7 @@ describe('Resource', () => { it('should merge async attributes into sync attributes once resolved', async () => { //async attributes that resolve after 1 ms - const asyncAttributes = new Promise(resolve => { + const asyncAttributes = new Promise(resolve => { setTimeout( () => resolve({ async: 'fromasync', shared: 'fromasync' }), 1 @@ -248,7 +248,7 @@ describe('Resource', () => { ); //async attributes that resolve after 1 ms - const asyncAttributes = new Promise(resolve => { + const asyncAttributes = new Promise(resolve => { setTimeout( () => resolve({ promise2: 'promise2val', shared: 'promise2val' }), 1 diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts index 2c36114aaa..80cb7e4c42 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts @@ -32,12 +32,12 @@ import { Span, SpanExporter, } from '../../../src'; -import { context } from '@opentelemetry/api'; +import { Attributes, context } from '@opentelemetry/api'; import { TestRecordOnlySampler } from './TestRecordOnlySampler'; import { TestTracingSpanExporter } from './TestTracingSpanExporter'; import { TestStackContextManager } from './TestStackContextManager'; import { BatchSpanProcessorBase } from '../../../src/export/BatchSpanProcessorBase'; -import { Resource, ResourceAttributes } from '@opentelemetry/resources'; +import { Resource } from '@opentelemetry/resources'; function createSampledSpan(spanName: string): Span { const tracer = new BasicTracerProvider({ @@ -442,7 +442,7 @@ describe('BatchSpanProcessorBase', () => { const tracer = new BasicTracerProvider({ resource: new Resource( {}, - new Promise(resolve => { + new Promise(resolve => { setTimeout(() => resolve({ async: 'fromasync' }), 1); }) ), diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/SimpleSpanProcessor.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/SimpleSpanProcessor.test.ts index f1e1bf16b3..d1576ae08a 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/SimpleSpanProcessor.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/SimpleSpanProcessor.test.ts @@ -36,7 +36,8 @@ import { } from '../../../src'; import { TestStackContextManager } from './TestStackContextManager'; import { TestTracingSpanExporter } from './TestTracingSpanExporter'; -import { Resource, ResourceAttributes } from '@opentelemetry/resources'; +import { Attributes } from '@opentelemetry/api'; +import { Resource } from '@opentelemetry/resources'; import { TestExporterWithDelay } from './TestExporterWithDelay'; describe('SimpleSpanProcessor', () => { @@ -164,7 +165,7 @@ describe('SimpleSpanProcessor', () => { const providerWithAsyncResource = new BasicTracerProvider({ resource: new Resource( {}, - new Promise(resolve => { + new Promise(resolve => { setTimeout(() => resolve({ async: 'fromasync' }), 1); }) ), @@ -205,7 +206,7 @@ describe('SimpleSpanProcessor', () => { const providerWithAsyncResource = new BasicTracerProvider({ resource: new Resource( {}, - new Promise(resolve => { + new Promise(resolve => { setTimeout(() => resolve({ async: 'fromasync' }), 1); }) ),