diff --git a/packages/opentelemetry-resources/src/IResource.ts b/packages/opentelemetry-resources/src/IResource.ts index c911904ce49..f67b06afd2c 100644 --- a/packages/opentelemetry-resources/src/IResource.ts +++ b/packages/opentelemetry-resources/src/IResource.ts @@ -23,24 +23,9 @@ import { Attributes } from '@opentelemetry/api'; */ export interface IResource { /** - * Check if async attributes have resolved. This is useful to avoid awaiting - * waitForAsyncAttributes (which will introduce asynchronous behavior) when not necessary. - * - * @returns true if the resource "attributes" property is not yet settled to its final value - */ - asyncAttributesPending?: boolean; - - /** - * @returns the Resource's attributes. - */ - readonly attributes: Attributes; - - /** - * Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to - * this Resource's attributes. This is useful in exporters to block until resource detection - * has finished. + * @returns the Resource's attributes wrapped in a promise. */ - waitForAsyncAttributes?(): Promise; + readonly attributes: Promise; /** * Returns a new, merged {@link Resource} by merging the current Resource diff --git a/packages/opentelemetry-resources/src/Resource.ts b/packages/opentelemetry-resources/src/Resource.ts index c2ec93ecff1..41d2564e4f3 100644 --- a/packages/opentelemetry-resources/src/Resource.ts +++ b/packages/opentelemetry-resources/src/Resource.ts @@ -16,32 +16,20 @@ import { Attributes, diag } from '@opentelemetry/api'; import { - SEMRESATTRS_SERVICE_NAME, - SEMRESATTRS_TELEMETRY_SDK_LANGUAGE, - SEMRESATTRS_TELEMETRY_SDK_NAME, - SEMRESATTRS_TELEMETRY_SDK_VERSION, + ATTR_SERVICE_NAME, ATTR_TELEMETRY_SDK_LANGUAGE, ATTR_TELEMETRY_SDK_NAME, ATTR_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; import { SDK_INFO } from '@opentelemetry/core'; import { defaultServiceName } from './platform'; import { IResource } from './IResource'; +import { isPromiseLike } from './utils'; /** * A Resource describes the entity for which a signals (metrics or trace) are * collected. */ export class Resource implements IResource { - static readonly EMPTY = new Resource({}); - private _syncAttributes?: Attributes; - private _asyncAttributesPromise?: Promise; - private _attributes?: Attributes; - - /** - * Check if async attributes have resolved. This is useful to avoid awaiting - * waitForAsyncAttributes (which will introduce asynchronous behavior) when not necessary. - * - * @returns true if the resource "attributes" property is not yet settled to its final value - */ - public asyncAttributesPending?: boolean; + static readonly EMPTY = new Resource(Promise.resolve({})); + private _attributes: Promise; /** * Returns an empty Resource @@ -54,15 +42,15 @@ export class Resource implements IResource { * Returns a Resource that identifies the SDK in use. */ static default(): IResource { - return new Resource({ - [SEMRESATTRS_SERVICE_NAME]: defaultServiceName(), - [SEMRESATTRS_TELEMETRY_SDK_LANGUAGE]: - SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], - [SEMRESATTRS_TELEMETRY_SDK_NAME]: - SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME], - [SEMRESATTRS_TELEMETRY_SDK_VERSION]: - SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION], - }); + return new Resource(Promise.resolve({ + [ATTR_SERVICE_NAME]: defaultServiceName(), + [ATTR_TELEMETRY_SDK_LANGUAGE]: + SDK_INFO[ATTR_TELEMETRY_SDK_LANGUAGE], + [ATTR_TELEMETRY_SDK_NAME]: + SDK_INFO[ATTR_TELEMETRY_SDK_NAME], + [ATTR_TELEMETRY_SDK_VERSION]: + SDK_INFO[ATTR_TELEMETRY_SDK_VERSION], + })); } constructor( @@ -71,45 +59,20 @@ export class Resource implements IResource { * information about the entity as numbers, strings or booleans * TODO: Consider to add check/validation on attributes. */ - attributes: Attributes, - asyncAttributesPromise?: Promise + attributes: Attributes | Promise ) { - this._attributes = attributes; - this.asyncAttributesPending = asyncAttributesPromise != null; - this._syncAttributes = this._attributes ?? {}; - this._asyncAttributesPromise = asyncAttributesPromise?.then( - asyncAttributes => { - this._attributes = Object.assign({}, this._attributes, asyncAttributes); - this.asyncAttributesPending = false; - return asyncAttributes; - }, - err => { + if (isPromiseLike(attributes)) { + this._attributes = attributes.catch(err => { diag.debug("a resource's async attributes promise rejected: %s", err); - this.asyncAttributesPending = false; return {}; - } - ); - } - - get attributes(): Attributes { - if (this.asyncAttributesPending) { - diag.error( - 'Accessing resource attributes before async attributes settled' - ); + }); + } else { + this._attributes = Promise.resolve(attributes); } - - return this._attributes ?? {}; } - /** - * Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to - * this Resource's attributes. This is useful in exporters to block until resource detection - * has finished. - */ - async waitForAsyncAttributes?(): Promise { - if (this.asyncAttributesPending) { - await this._asyncAttributesPromise; - } + get attributes(): Promise { + return this._attributes; } /** @@ -123,33 +86,20 @@ export class Resource implements IResource { merge(other: IResource | null): IResource { if (!other) return this; - // Attributes from other resource overwrite attributes from this resource. - const mergedSyncAttributes = { - ...this._syncAttributes, - //Support for old resource implementation where _syncAttributes is not defined - ...((other as Resource)._syncAttributes ?? other.attributes), - }; + return new Resource(Promise.allSettled([ + this._attributes, + other.attributes, + ]).then((attribPromises) => { + let result = {}; - if ( - !this._asyncAttributesPromise && - !(other as Resource)._asyncAttributesPromise - ) { - return new Resource(mergedSyncAttributes); - } - - const mergedAttributesPromise = Promise.all([ - this._asyncAttributesPromise, - (other as Resource)._asyncAttributesPromise, - ]).then(([thisAsyncAttributes, otherAsyncAttributes]) => { - return { - ...this._syncAttributes, - ...thisAsyncAttributes, - //Support for old resource implementation where _syncAttributes is not defined - ...((other as Resource)._syncAttributes ?? other.attributes), - ...otherAsyncAttributes, - }; - }); - - return new Resource(mergedSyncAttributes, mergedAttributesPromise); + for (const prom of attribPromises) { + if (prom.status === 'rejected') { + diag.debug("a resource's async attributes promise rejected: %s", prom.reason); + } else { + result = { ...result, ...prom.value}; + } + } + return result; + })); } } diff --git a/packages/opentelemetry-resources/src/config.ts b/packages/opentelemetry-resources/src/config.ts index 239d596e6d9..aa71cb84aab 100644 --- a/packages/opentelemetry-resources/src/config.ts +++ b/packages/opentelemetry-resources/src/config.ts @@ -14,11 +14,11 @@ * limitations under the License. */ -import type { Detector, DetectorSync } from './types'; +import type { DetectorSync } from './types'; /** * ResourceDetectionConfig provides an interface for configuring resource auto-detection. */ export interface ResourceDetectionConfig { - detectors?: Array; + detectors?: Array; } diff --git a/packages/opentelemetry-resources/src/detect-resources.ts b/packages/opentelemetry-resources/src/detect-resources.ts index 0bfa13cac8b..68f126c8b8a 100644 --- a/packages/opentelemetry-resources/src/detect-resources.ts +++ b/packages/opentelemetry-resources/src/detect-resources.ts @@ -17,43 +17,8 @@ import { Resource } from './Resource'; import { ResourceDetectionConfig } from './config'; import { diag } from '@opentelemetry/api'; -import { isPromiseLike } from './utils'; -import { Detector, DetectorSync } from './types'; import { IResource } from './IResource'; -/** - * Runs all resource detectors and returns the results merged into a single Resource. Promise - * does not resolve until all the underlying detectors have resolved, unlike - * detectResourcesSync. - * - * @deprecated use detectResourcesSync() instead. - * @param config Configuration for resource detection - */ -export const detectResources = async ( - config: ResourceDetectionConfig = {} -): Promise => { - const resources: IResource[] = await Promise.all( - (config.detectors || []).map(async d => { - try { - const resource = await d.detect(config); - diag.debug(`${d.constructor.name} found resource.`, resource); - return resource; - } catch (e) { - diag.debug(`${d.constructor.name} failed: ${e.message}`); - return Resource.empty(); - } - }) - ); - - // Future check if verbose logging is enabled issue #1903 - logResources(resources); - - return resources.reduce( - (acc, resource) => acc.merge(resource), - Resource.empty() - ); -}; - /** * Runs all resource detectors synchronously, merging their results. In case of attribute collision later resources will take precedence. * @@ -62,66 +27,15 @@ export const detectResources = async ( export const detectResourcesSync = ( config: ResourceDetectionConfig = {} ): IResource => { - const resources: IResource[] = (config.detectors ?? []).map( - (d: Detector | DetectorSync) => { - try { - const resourceOrPromise = d.detect(config); - let resource: IResource; - if (isPromiseLike(resourceOrPromise)) { - const createPromise = async () => { - const resolvedResource = await resourceOrPromise; - await resolvedResource.waitForAsyncAttributes?.(); - return resolvedResource.attributes; - }; - resource = new Resource({}, createPromise()); - } else { - resource = resourceOrPromise as IResource; - } - - if (resource.waitForAsyncAttributes) { - void resource - .waitForAsyncAttributes() - .then(() => - diag.debug(`${d.constructor.name} found resource.`, resource) - ); - } else { - diag.debug(`${d.constructor.name} found resource.`, resource); - } - - return resource; - } catch (e) { - diag.error(`${d.constructor.name} failed: ${e.message}`); - return Resource.empty(); + const detectors = config.detectors ?? []; + const resources = detectors.map(d => d.detect()); + + Promise.all(resources.map(r => r.attributes)).then((attribsArr) => { + for (const attribs of attribsArr) { + if (Object.keys(attribs).length > 0) { + diag.verbose(JSON.stringify(attribs, null, 4)); } } - ); - - const mergedResources = resources.reduce( - (acc, resource) => acc.merge(resource), - Resource.empty() - ); - - if (mergedResources.waitForAsyncAttributes) { - void mergedResources.waitForAsyncAttributes().then(() => { - // Future check if verbose logging is enabled issue #1903 - logResources(resources); - }); - } - - return mergedResources; -}; - -/** - * Writes debug information about the detected resources to the logger defined in the resource detection config, if one is provided. - * - * @param resources The array of {@link Resource} that should be logged. Empty entries will be ignored. - */ -const logResources = (resources: Array) => { - resources.forEach(resource => { - // Print only populated resources - if (Object.keys(resource.attributes).length > 0) { - const resourceDebugString = JSON.stringify(resource.attributes, null, 4); - diag.verbose(resourceDebugString); - } }); -}; + return resources.reduce((acc, res) => acc.merge(res), Resource.empty()); +} diff --git a/packages/opentelemetry-resources/src/detectors/BrowserDetector.ts b/packages/opentelemetry-resources/src/detectors/BrowserDetector.ts deleted file mode 100644 index d06fc238167..00000000000 --- a/packages/opentelemetry-resources/src/detectors/BrowserDetector.ts +++ /dev/null @@ -1,31 +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. - */ - -import { IResource } from '../IResource'; -import { ResourceDetectionConfig } from '../config'; -import { Detector } from '../types'; -import { browserDetectorSync } from './BrowserDetectorSync'; - -/** - * BrowserDetector will be used to detect the resources related to browser. - */ -class BrowserDetector implements Detector { - detect(config?: ResourceDetectionConfig): Promise { - return Promise.resolve(browserDetectorSync.detect(config)); - } -} - -export const browserDetector = new BrowserDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/detectors/EnvDetector.ts deleted file mode 100644 index 3467a8123fb..00000000000 --- a/packages/opentelemetry-resources/src/detectors/EnvDetector.ts +++ /dev/null @@ -1,39 +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. - */ - -import { Detector } from '../types'; -import { ResourceDetectionConfig } from '../config'; -import { IResource } from '../IResource'; -import { envDetectorSync } from './EnvDetectorSync'; - -/** - * EnvDetector can be used to detect the presence of and create a Resource - * from the OTEL_RESOURCE_ATTRIBUTES environment variable. - */ -class EnvDetector implements Detector { - /** - * 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 - */ - detect(config?: ResourceDetectionConfig): Promise { - return Promise.resolve(envDetectorSync.detect(config)); - } -} - -export const envDetector = new EnvDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/NoopDetector.ts b/packages/opentelemetry-resources/src/detectors/NoopDetector.ts deleted file mode 100644 index 463d8c629f1..00000000000 --- a/packages/opentelemetry-resources/src/detectors/NoopDetector.ts +++ /dev/null @@ -1,27 +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. - */ - -import { Detector } from '../types'; -import { IResource } from '../IResource'; -import { noopDetectorSync } from './NoopDetectorSync'; - -export class NoopDetector implements Detector { - detect(): Promise { - return Promise.resolve(noopDetectorSync.detect()); - } -} - -export const noopDetector = new NoopDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/index.ts b/packages/opentelemetry-resources/src/detectors/index.ts index 74ace665ea8..6befbcc84cd 100644 --- a/packages/opentelemetry-resources/src/detectors/index.ts +++ b/packages/opentelemetry-resources/src/detectors/index.ts @@ -15,15 +15,10 @@ */ export { - hostDetector, hostDetectorSync, - osDetector, osDetectorSync, - processDetector, processDetectorSync, serviceInstanceIdDetectorSync, } from './platform'; -export { browserDetector } from './BrowserDetector'; -export { envDetector } from './EnvDetector'; export { browserDetectorSync } from './BrowserDetectorSync'; export { envDetectorSync } from './EnvDetectorSync'; diff --git a/packages/opentelemetry-resources/src/detectors/platform/browser/HostDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/browser/HostDetector.ts deleted file mode 100644 index b1f022b993b..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/browser/HostDetector.ts +++ /dev/null @@ -1,19 +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. - */ - -import { noopDetector } from '../../NoopDetector'; - -export const hostDetector = noopDetector; diff --git a/packages/opentelemetry-resources/src/detectors/platform/browser/OSDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/browser/OSDetector.ts deleted file mode 100644 index 33f6fe4e88a..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/browser/OSDetector.ts +++ /dev/null @@ -1,19 +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. - */ - -import { noopDetector } from '../../NoopDetector'; - -export const osDetector = noopDetector; diff --git a/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetector.ts deleted file mode 100644 index 200a0940d62..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetector.ts +++ /dev/null @@ -1,19 +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. - */ - -import { noopDetector } from '../../NoopDetector'; - -export const processDetector = noopDetector; diff --git a/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetectorSync.ts index df98eddeffc..0cfb4c9f673 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/browser/ProcessDetectorSync.ts @@ -14,6 +14,6 @@ * limitations under the License. */ -import { noopDetector } from '../../NoopDetector'; +import { noopDetectorSync } from '../../NoopDetectorSync'; -export const processDetectorSync = noopDetector; +export const processDetectorSync = noopDetectorSync; diff --git a/packages/opentelemetry-resources/src/detectors/platform/browser/index.ts b/packages/opentelemetry-resources/src/detectors/platform/browser/index.ts index e8585d7d36f..13e79d177b6 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/browser/index.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/browser/index.ts @@ -14,10 +14,7 @@ * limitations under the License. */ -export { hostDetector } from './HostDetector'; export { hostDetectorSync } from './HostDetectorSync'; -export { osDetector } from './OSDetector'; export { osDetectorSync } from './OSDetectorSync'; -export { processDetector } from './ProcessDetector'; export { processDetectorSync } from './ProcessDetectorSync'; export { serviceInstanceIdDetectorSync } from './ServiceInstanceIdDetectorSync'; diff --git a/packages/opentelemetry-resources/src/detectors/platform/index.ts b/packages/opentelemetry-resources/src/detectors/platform/index.ts index f5393dd7bbb..96bed4c1065 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/index.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/index.ts @@ -14,11 +14,8 @@ * limitations under the License. */ export { - hostDetector, hostDetectorSync, - osDetector, osDetectorSync, - processDetector, processDetectorSync, serviceInstanceIdDetectorSync, } from './node'; diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/node/HostDetector.ts deleted file mode 100644 index fe95b3d89d5..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetector.ts +++ /dev/null @@ -1,32 +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. - */ - -import { Detector } from '../../../types'; -import { ResourceDetectionConfig } from '../../../config'; -import { IResource } from '../../../IResource'; -import { hostDetectorSync } from './HostDetectorSync'; - -/** - * HostDetector detects the resources related to the host current process is - * running on. Currently only non-cloud-based attributes are included. - */ -class HostDetector implements Detector { - detect(_config?: ResourceDetectionConfig): Promise { - return Promise.resolve(hostDetectorSync.detect(_config)); - } -} - -export const hostDetector = new HostDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts index 123976dc577..5009720b38c 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/HostDetectorSync.ts @@ -33,17 +33,15 @@ import { getMachineId } from './machine-id/getMachineId'; */ class HostDetectorSync implements DetectorSync { detect(_config?: ResourceDetectionConfig): Resource { - const attributes: Attributes = { - [SEMRESATTRS_HOST_NAME]: hostname(), - [SEMRESATTRS_HOST_ARCH]: normalizeArch(arch()), - }; - - return new Resource(attributes, this._getAsyncAttributes()); + return new Resource(this._getAttributes()) } - private _getAsyncAttributes(): Promise { + private _getAttributes(): Promise { return getMachineId().then(machineId => { - const attributes: Attributes = {}; + const attributes: Attributes = { + [SEMRESATTRS_HOST_NAME]: hostname(), + [SEMRESATTRS_HOST_ARCH]: normalizeArch(arch()), + }; if (machineId) { attributes[SEMRESATTRS_HOST_ID] = machineId; } diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/OSDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/node/OSDetector.ts deleted file mode 100644 index b8720d293a7..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/node/OSDetector.ts +++ /dev/null @@ -1,32 +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. - */ - -import { Detector } from '../../../types'; -import { ResourceDetectionConfig } from '../../../config'; -import { IResource } from '../../../IResource'; -import { osDetectorSync } from './OSDetectorSync'; - -/** - * OSDetector detects the resources related to the operating system (OS) on - * which the process represented by this resource is running. - */ -class OSDetector implements Detector { - detect(_config?: ResourceDetectionConfig): Promise { - return Promise.resolve(osDetectorSync.detect(_config)); - } -} - -export const osDetector = new OSDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetector.ts b/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetector.ts deleted file mode 100644 index 13b31a34ee8..00000000000 --- a/packages/opentelemetry-resources/src/detectors/platform/node/ProcessDetector.ts +++ /dev/null @@ -1,32 +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. - */ - -import { Detector } from '../../../types'; -import { ResourceDetectionConfig } from '../../../config'; -import { IResource } from '../../../IResource'; -import { processDetectorSync } from './ProcessDetectorSync'; - -/** - * ProcessDetector will be used to detect the resources related current process running - * and being instrumented from the NodeJS Process module. - */ -class ProcessDetector implements Detector { - detect(config?: ResourceDetectionConfig): Promise { - return Promise.resolve(processDetectorSync.detect(config)); - } -} - -export const processDetector = new ProcessDetector(); diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/index.ts b/packages/opentelemetry-resources/src/detectors/platform/node/index.ts index e8585d7d36f..13e79d177b6 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/index.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/index.ts @@ -14,10 +14,7 @@ * limitations under the License. */ -export { hostDetector } from './HostDetector'; export { hostDetectorSync } from './HostDetectorSync'; -export { osDetector } from './OSDetector'; export { osDetectorSync } from './OSDetectorSync'; -export { processDetector } from './ProcessDetector'; export { processDetectorSync } from './ProcessDetectorSync'; export { serviceInstanceIdDetectorSync } from './ServiceInstanceIdDetectorSync'; diff --git a/packages/opentelemetry-resources/src/index.ts b/packages/opentelemetry-resources/src/index.ts index 4c93e1da78c..6324dd85dbd 100644 --- a/packages/opentelemetry-resources/src/index.ts +++ b/packages/opentelemetry-resources/src/index.ts @@ -17,19 +17,14 @@ export { Resource } from './Resource'; export { IResource } from './IResource'; export { defaultServiceName } from './platform'; -export { DetectorSync, Detector } from './types'; +export { DetectorSync } from './types'; export { ResourceDetectionConfig } from './config'; export { - browserDetector, browserDetectorSync, - envDetector, envDetectorSync, - hostDetector, hostDetectorSync, - osDetector, osDetectorSync, - processDetector, processDetectorSync, serviceInstanceIdDetectorSync, } from './detectors'; -export { detectResourcesSync, detectResources } from './detect-resources'; +export { detectResourcesSync } from './detect-resources'; diff --git a/packages/opentelemetry-resources/src/types.ts b/packages/opentelemetry-resources/src/types.ts index 9004cffdf09..7f7a43cd61e 100644 --- a/packages/opentelemetry-resources/src/types.ts +++ b/packages/opentelemetry-resources/src/types.ts @@ -17,13 +17,6 @@ import { ResourceDetectionConfig } from './config'; import { IResource } from './IResource'; -/** - * @deprecated please use {@link DetectorSync} - */ -export interface Detector { - detect(config?: ResourceDetectionConfig): Promise; -} - /** * Interface for a synchronous Resource Detector. In order to detect attributes asynchronously, a detector * can pass a Promise as the second parameter to the Resource constructor. diff --git a/packages/opentelemetry-resources/test/Resource.test.ts b/packages/opentelemetry-resources/test/Resource.test.ts index 900ca9bbeba..d9d7fbbb0d5 100644 --- a/packages/opentelemetry-resources/test/Resource.test.ts +++ b/packages/opentelemetry-resources/test/Resource.test.ts @@ -25,8 +25,7 @@ import { SEMRESATTRS_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; import { describeBrowser, describeNode } from './util'; -import { Attributes, diag } from '@opentelemetry/api'; -import { Resource as Resource190 } from '@opentelemetry/resources_1.9.0'; +import { diag } from '@opentelemetry/api'; describe('Resource', () => { const resource1 = new Resource({ @@ -44,7 +43,7 @@ describe('Resource', () => { }); const emptyResource = new Resource({}); - it('should return merged resource', () => { + it('should return merged resource', async () => { const expectedResource = new Resource({ 'k8s.io/container/name': 'c1', 'k8s.io/namespace/name': 'default', @@ -53,11 +52,13 @@ describe('Resource', () => { 'k8s.io/location': 'location', }); const actualResource = resource1.merge(resource2); - assert.strictEqual(Object.keys(actualResource.attributes).length, 5); - assert.deepStrictEqual(actualResource, expectedResource); + const actualAttribs = await actualResource.attributes; + const expectedAttribs = await expectedResource.attributes; + assert.strictEqual(Object.keys(actualAttribs).length, 5); + assert.deepStrictEqual(actualAttribs, expectedAttribs); }); - it('should return merged resource when collision in attributes', () => { + it('should return merged resource when collision in attributes', async () => { const expectedResource = new Resource({ 'k8s.io/container/name': 'c2', 'k8s.io/namespace/name': 'default', @@ -65,70 +66,58 @@ describe('Resource', () => { 'k8s.io/location': 'location1', }); const actualResource = resource1.merge(resource3); - assert.strictEqual(Object.keys(actualResource.attributes).length, 4); - assert.deepStrictEqual(actualResource, expectedResource); + const actualAttribs = await actualResource.attributes; + const expectedAttribs = await expectedResource.attributes; + assert.strictEqual(Object.keys(actualAttribs).length, 4); + assert.deepStrictEqual(actualAttribs, expectedAttribs); }); - it('should return merged resource when first resource is empty', () => { + it('should return merged resource when first resource is empty', async () => { const actualResource = emptyResource.merge(resource2); - assert.strictEqual(Object.keys(actualResource.attributes).length, 2); - assert.deepStrictEqual(actualResource, resource2); + const actualAttribs = await actualResource.attributes; + const expectedAttribs = await resource2.attributes; + assert.strictEqual(Object.keys(actualAttribs).length, 2); + assert.deepStrictEqual(actualAttribs, expectedAttribs); }); - it('should return merged resource when other resource is empty', () => { + it('should return merged resource when other resource is empty', async () => { const actualResource = resource1.merge(emptyResource); - assert.strictEqual(Object.keys(actualResource.attributes).length, 3); - assert.deepStrictEqual(actualResource, resource1); + const actualAttribs = await actualResource.attributes; + const expectedAttribs = await resource1.attributes; + assert.strictEqual(Object.keys(actualAttribs).length, 3); + assert.deepStrictEqual(actualAttribs, expectedAttribs); }); - it('should return merged resource when other resource is null', () => { + it('should return merged resource when other resource is null', async () => { const actualResource = resource1.merge(null); - assert.strictEqual(Object.keys(actualResource.attributes).length, 3); - assert.deepStrictEqual(actualResource, resource1); + const actualAttribs = await actualResource.attributes; + const expectedAttribs = await resource1.attributes; + assert.strictEqual(Object.keys(actualAttribs).length, 3); + assert.deepStrictEqual(actualAttribs, expectedAttribs); }); - it('should accept string, number, and boolean values', () => { + it('should accept string, number, and boolean values', async () => { const resource = new Resource({ 'custom.string': 'strvalue', 'custom.number': 42, 'custom.boolean': true, }); - assert.strictEqual(resource.attributes['custom.string'], 'strvalue'); - assert.strictEqual(resource.attributes['custom.number'], 42); - assert.strictEqual(resource.attributes['custom.boolean'], true); - }); - - it('should log when accessing attributes before async attributes promise has settled', () => { - const debugStub = sinon.spy(diag, 'error'); - const resource = new Resource( - {}, - new Promise(resolve => { - setTimeout(resolve, 1); - }) - ); - - resource.attributes; - - assert.ok( - debugStub.calledWithMatch( - 'Accessing resource attributes before async attributes settled' - ) - ); + const attributes = await resource.attributes; + assert.strictEqual(attributes['custom.string'], 'strvalue'); + assert.strictEqual(attributes['custom.number'], 42); + assert.strictEqual(attributes['custom.boolean'], true); }); describe('.empty()', () => { - it('should return an empty resource (except required service name)', () => { + it('should return an empty resource (except required service name)', async () => { const resource = Resource.empty(); - assert.deepStrictEqual(Object.keys(resource.attributes), []); + const attributes = await resource.attributes; + assert.deepStrictEqual(Object.keys(attributes), []); }); it('should return the same empty resource', () => { assert.strictEqual(Resource.empty(), Resource.empty()); }); - - it('should return false for asyncAttributesPending immediately', () => { - assert.ok(!Resource.empty().asyncAttributesPending); - }); }); describe('asynchronous attributes', () => { @@ -136,105 +125,17 @@ describe('Resource', () => { sinon.restore(); }); - it('should return false for asyncAttributesPending if no promise provided', () => { - assert.ok(!new Resource({ foo: 'bar' }).asyncAttributesPending); - assert.ok(!Resource.empty().asyncAttributesPending); - assert.ok(!Resource.default().asyncAttributesPending); - }); - - it('should return false for asyncAttributesPending once promise settles', async () => { - const clock = sinon.useFakeTimers(); - const resourceResolve = new Resource( - {}, - new Promise(resolve => { - setTimeout(resolve, 1); - }) - ); - const resourceReject = new Resource( - {}, - new Promise((_, reject) => { - setTimeout(reject, 1); - }) - ); - - for (const resource of [resourceResolve, resourceReject]) { - assert.ok(resource.asyncAttributesPending); - await clock.nextAsync(); - await resource.waitForAsyncAttributes?.(); - assert.ok(!resource.asyncAttributesPending); - } - }); - - it('should merge async attributes into sync attributes once resolved', async () => { - //async attributes that resolve after 1 ms - const asyncAttributes = new Promise(resolve => { - setTimeout( - () => resolve({ async: 'fromasync', shared: 'fromasync' }), - 1 - ); - }); - - const resource = new Resource( - { sync: 'fromsync', shared: 'fromsync' }, - asyncAttributes - ); - - await resource.waitForAsyncAttributes?.(); - assert.deepStrictEqual(resource.attributes, { - sync: 'fromsync', - // async takes precedence - shared: 'fromasync', - async: 'fromasync', - }); - }); - - it('should merge async attributes when both resources have promises', async () => { + it('should merge async attributes correctly when resource1 fulfils after resource2', async () => { const resource1 = new Resource( - {}, - Promise.resolve({ promise1: 'promise1val', shared: 'promise1val' }) + new Promise((res) => setTimeout(() => res({ promise1: 'promise1val', shared: 'promise1val' }) ,1)) ); const resource2 = new Resource( - {}, - Promise.resolve({ promise2: 'promise2val', shared: 'promise2val' }) + { promise2: 'promise2val', shared: 'promise2val' } ); - // this one rejects - const resource3 = new Resource({}, Promise.reject(new Error('reject'))); - const resource4 = new Resource( - {}, - Promise.resolve({ promise4: 'promise4val', shared: 'promise4val' }) - ); - - const merged = resource1 - .merge(resource2) - .merge(resource3) - .merge(resource4); - - await merged.waitForAsyncAttributes?.(); - - assert.deepStrictEqual(merged.attributes, { - promise1: 'promise1val', - promise2: 'promise2val', - promise4: 'promise4val', - shared: 'promise4val', - }); - }); - - it('should merge async attributes correctly when resource1 fulfils after resource2', async () => { - const resource1 = new Resource( - {}, - Promise.resolve({ promise1: 'promise1val', shared: 'promise1val' }) - ); - - const resource2 = new Resource({ - promise2: 'promise2val', - shared: 'promise2val', - }); - const merged = resource1.merge(resource2); + const attributes = await merged.attributes; - await merged.waitForAsyncAttributes?.(); - - assert.deepStrictEqual(merged.attributes, { + assert.deepStrictEqual(attributes, { promise1: 'promise1val', promise2: 'promise2val', shared: 'promise2val', @@ -243,24 +144,15 @@ describe('Resource', () => { it('should merge async attributes correctly when resource2 fulfils after resource1', async () => { const resource1 = new Resource( - { shared: 'promise1val' }, - Promise.resolve({ promise1: 'promise1val' }) + { promise1: 'promise1val', shared: 'promise1val' } + ); + const resource2 = new Resource( + new Promise((res) => setTimeout(() => res({ promise2: 'promise2val', shared: 'promise2val' }) ,1)) ); - - //async attributes that resolve after 1 ms - const asyncAttributes = new Promise(resolve => { - setTimeout( - () => resolve({ promise2: 'promise2val', shared: 'promise2val' }), - 1 - ); - }); - const resource2 = new Resource({}, asyncAttributes); - const merged = resource1.merge(resource2); + const attributes = await merged.attributes; - await merged.waitForAsyncAttributes?.(); - - assert.deepStrictEqual(merged.attributes, { + assert.deepStrictEqual(attributes, { promise1: 'promise1val', promise2: 'promise2val', shared: 'promise2val', @@ -270,8 +162,8 @@ describe('Resource', () => { it('should log when promise rejects', async () => { const debugStub = sinon.spy(diag, 'debug'); - const resource = new Resource({}, Promise.reject(new Error('rejected'))); - await resource.waitForAsyncAttributes?.(); + const resource = new Resource(Promise.reject(new Error('rejected'))); + await resource.attributes; assert.ok( debugStub.calledWithMatch( @@ -282,71 +174,48 @@ describe('Resource', () => { }); describeNode('.default()', () => { - it('should return a default resource', () => { + it('should return a default resource', async () => { const resource = Resource.default(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], + attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], + attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], + attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_NAME], + attributes[SEMRESATTRS_SERVICE_NAME], `unknown_service:${process.argv0}` ); }); }); describeBrowser('.default()', () => { - it('should return a default resource', () => { + it('should return a default resource', async () => { const resource = Resource.default(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], + attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], + attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], + attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION] ); assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_NAME], + attributes[SEMRESATTRS_SERVICE_NAME], 'unknown_service' ); }); }); - - describe('compatibility', () => { - it('should merge resource with old implementation', () => { - const resource = Resource.EMPTY; - const oldResource = new Resource190({ fromold: 'fromold' }); - - const mergedResource = resource.merge(oldResource); - - assert.strictEqual(mergedResource.attributes['fromold'], 'fromold'); - }); - - it('should merge resource containing async attributes with old implementation', async () => { - const resource = new Resource( - {}, - Promise.resolve({ fromnew: 'fromnew' }) - ); - const oldResource = new Resource190({ fromold: 'fromold' }); - - const mergedResource = resource.merge(oldResource); - assert.strictEqual(mergedResource.attributes['fromold'], 'fromold'); - - await mergedResource.waitForAsyncAttributes?.(); - assert.strictEqual(mergedResource.attributes['fromnew'], 'fromnew'); - }); - }); }); diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index 7c3b1a212c9..f608dcbffce 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -17,7 +17,7 @@ import { diag } from '@opentelemetry/api'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { Resource, Detector, detectResourcesSync, DetectorSync } from '../src'; +import { Resource, detectResourcesSync, DetectorSync } from '../src'; import { describeNode } from './util'; describe('detectResourcesSync', () => { @@ -25,31 +25,10 @@ describe('detectResourcesSync', () => { sinon.restore(); }); - it('handles resource detectors which return Promise', async () => { - const detector: Detector = { - async detect() { - return new Resource( - { sync: 'fromsync' }, - Promise.resolve().then(() => ({ async: 'fromasync' })) - ); - }, - }; - const resource = detectResourcesSync({ - detectors: [detector], - }); - - await resource.waitForAsyncAttributes?.(); - assert.deepStrictEqual(resource.attributes, { - sync: 'fromsync', - async: 'fromasync', - }); - }); - it('handles resource detectors which return Resource with a promise inside', async () => { const detector: DetectorSync = { detect() { return new Resource( - { sync: 'fromsync' }, Promise.resolve({ async: 'fromasync' }) ); }, @@ -57,12 +36,9 @@ describe('detectResourcesSync', () => { const resource = detectResourcesSync({ detectors: [detector], }); + const attributes = await resource.attributes; - // before waiting, it should already have the sync resources - assert.deepStrictEqual(resource.attributes, { sync: 'fromsync' }); - await resource.waitForAsyncAttributes?.(); - assert.deepStrictEqual(resource.attributes, { - sync: 'fromsync', + assert.deepStrictEqual(attributes, { async: 'fromasync', }); }); @@ -75,7 +51,6 @@ describe('detectResourcesSync', () => { class DetectorRejects implements DetectorSync { detect() { return new Resource( - { sync: 'fromsync' }, Promise.reject(new Error('reject')) ); } @@ -83,7 +58,6 @@ describe('detectResourcesSync', () => { class DetectorOk implements DetectorSync { detect() { return new Resource( - { sync: 'fromsync' }, Promise.resolve({ async: 'fromasync' }) ); } @@ -92,9 +66,11 @@ describe('detectResourcesSync', () => { const resource = detectResourcesSync({ detectors: [new DetectorRejects(), new DetectorOk()], }); + const attributes = await resource.attributes; - await resource.waitForAsyncAttributes?.(); - + assert.deepStrictEqual(attributes, { + async: 'fromasync', + }); assert.ok( debugStub.calledWithMatch( "a resource's async attributes promise rejected" diff --git a/packages/opentelemetry-resources/test/detectors/browser/BrowserDetector.test.ts b/packages/opentelemetry-resources/test/detectors/browser/BrowserDetector.test.ts index 95c1b95b445..ab3fdc03cf0 100644 --- a/packages/opentelemetry-resources/test/detectors/browser/BrowserDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/browser/BrowserDetector.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ import * as sinon from 'sinon'; -import { IResource } from '../../../src'; -import { browserDetector } from '../../../src/detectors/BrowserDetector'; +import { browserDetectorSync } from '../../../src'; import { describeBrowser } from '../../util'; import { assertResource, @@ -32,8 +31,8 @@ describeBrowser('browserDetector()', () => { userAgent: 'dddd', }); - const resource: IResource = await browserDetector.detect(); - assertResource(resource, { + const resource = await browserDetectorSync.detect(); + assertResource(await resource.attributes, { version: 'dddd', runtimeDescription: 'Web Browser', runtimeName: 'browser', @@ -43,7 +42,7 @@ describeBrowser('browserDetector()', () => { sinon.stub(globalThis, 'navigator').value({ userAgent: '', }); - const resource: IResource = await browserDetector.detect(); - assertEmptyResource(resource); + const resource = await browserDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/browser/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/browser/EnvDetector.test.ts index 0a263ce4b39..594ebfdc200 100644 --- a/packages/opentelemetry-resources/test/detectors/browser/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/browser/EnvDetector.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import { RAW_ENVIRONMENT } from '@opentelemetry/core'; -import { envDetector, IResource } from '../../../src'; +import { envDetectorSync } from '../../../src'; import { assertEmptyResource, assertWebEngineResource, @@ -38,13 +38,14 @@ describeBrowser('envDetector() on web browser', () => { }); it('should return resource information from environment variable', async () => { - const resource: IResource = await envDetector.detect(); - assertWebEngineResource(resource, { + const resource = await envDetectorSync.detect(); + const attribs = await resource.attributes; + assertWebEngineResource(attribs, { name: 'chromium', version: '99', description: 'Chromium', }); - assert.strictEqual(resource.attributes['custom.key'], 'custom value'); + assert.strictEqual(attribs['custom.key'], 'custom value'); }); }); @@ -65,8 +66,9 @@ describeBrowser('envDetector() on web browser', () => { }); it('should return empty resource', async () => { - const resource: IResource = await envDetector.detect(); - assertEmptyResource(resource); + const resource = await envDetectorSync.detect(); + const attribs = await resource.attributes; + assertEmptyResource(attribs); }); }); } @@ -74,15 +76,15 @@ describeBrowser('envDetector() on web browser', () => { describe('with empty env', () => { it('should return empty resource', async () => { - const resource: IResource = await envDetector.detect(); - assertEmptyResource(resource); + const resource = await envDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); describe('with empty env', () => { it('should return empty resource', async () => { - const resource: IResource = await envDetector.detect(); - assertEmptyResource(resource); + const resource = await envDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/browser/HostDetector.test.ts b/packages/opentelemetry-resources/test/detectors/browser/HostDetector.test.ts index 6f1df6cd23f..8ea822881e9 100644 --- a/packages/opentelemetry-resources/test/detectors/browser/HostDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/browser/HostDetector.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import * as sinon from 'sinon'; -import { hostDetector, IResource } from '../../../src'; +import { hostDetectorSync } from '../../../src'; import { assertEmptyResource } from '../../util/resource-assertions'; import { describeBrowser } from '../../util'; @@ -24,7 +24,7 @@ describeBrowser('hostDetector() on web browser', () => { }); it('should return empty resource', async () => { - const resource: IResource = await hostDetector.detect(); - assertEmptyResource(resource); + const resource = await hostDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/browser/OSDetector.test.ts b/packages/opentelemetry-resources/test/detectors/browser/OSDetector.test.ts index bb69f739549..fe774558fd6 100644 --- a/packages/opentelemetry-resources/test/detectors/browser/OSDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/browser/OSDetector.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import * as sinon from 'sinon'; -import { osDetector, IResource } from '../../../src'; +import { osDetectorSync } from '../../../src'; import { assertEmptyResource } from '../../util/resource-assertions'; import { describeBrowser } from '../../util'; @@ -24,7 +24,7 @@ describeBrowser('osDetector() on web browser', () => { }); it('should return empty resource', async () => { - const resource: IResource = await osDetector.detect(); - assertEmptyResource(resource); + const resource = await osDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/browser/ProcessDetector.test.ts b/packages/opentelemetry-resources/test/detectors/browser/ProcessDetector.test.ts index 7c404e67ed0..7daf60eabe4 100644 --- a/packages/opentelemetry-resources/test/detectors/browser/ProcessDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/browser/ProcessDetector.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import * as sinon from 'sinon'; -import { processDetector, IResource } from '../../../src'; +import { processDetectorSync } from '../../../src'; import { assertEmptyResource } from '../../util/resource-assertions'; import { describeBrowser } from '../../util'; @@ -24,7 +24,7 @@ describeBrowser('processDetector() on web browser', () => { }); it('should return empty resource', async () => { - const resource: IResource = await processDetector.detect(); - assertEmptyResource(resource); + const resource = await processDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/node/BrowserDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/BrowserDetector.test.ts index e8fff65750f..efeb473a288 100644 --- a/packages/opentelemetry-resources/test/detectors/node/BrowserDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/BrowserDetector.test.ts @@ -14,13 +14,13 @@ * limitations under the License. */ import { IResource } from '../../../src'; -import { browserDetector } from '../../../src/detectors/BrowserDetector'; +import { browserDetectorSync } from '../../../src'; import { describeNode } from '../../util'; import { assertEmptyResource } from '../../util/resource-assertions'; describeNode('browserDetector()', () => { it('should return empty resources if window.document is missing', async () => { - const resource: IResource = await browserDetector.detect(); - assertEmptyResource(resource); + const resource: IResource = await browserDetectorSync.detect(); + assertEmptyResource(await resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts index b4ccdd1ad33..5846b5a4743 100644 --- a/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { envDetector, IResource } from '../../../src'; +import { envDetectorSync } from '../../../src'; import { assertK8sResource, assertEmptyResource, @@ -33,8 +33,9 @@ describeNode('envDetector() on Node.js', () => { }); it('should return resource information from environment variable', async () => { - const resource: IResource = await envDetector.detect(); - assertK8sResource(resource, { + const resource = await envDetectorSync.detect(); + const attributes = await resource.attributes; + assertK8sResource(attributes, { podName: 'pod-xyz-123', clusterName: 'c1', namespaceName: 'default', @@ -57,8 +58,9 @@ describeNode('envDetector() on Node.js', () => { }); it('should return empty resource', async () => { - const resource: IResource = await envDetector.detect(); - assertEmptyResource(resource); + const resource = await envDetectorSync.detect(); + const attributes = await resource.attributes; + assertEmptyResource(attributes); }); }); } @@ -66,8 +68,9 @@ describeNode('envDetector() on Node.js', () => { describe('with empty env', () => { it('should return empty resource', async () => { - const resource: IResource = await envDetector.detect(); - assertEmptyResource(resource); + const resource = await envDetectorSync.detect(); + const attributes = await resource.attributes; + assertEmptyResource(attributes); }); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/node/HostDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/HostDetector.test.ts index 20e26478229..c0c9b22a9fd 100644 --- a/packages/opentelemetry-resources/test/detectors/node/HostDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/HostDetector.test.ts @@ -22,9 +22,9 @@ import { SEMRESATTRS_HOST_NAME, } from '@opentelemetry/semantic-conventions'; import { describeNode } from '../../util'; -import { hostDetector, IResource } from '../../../src'; +import { hostDetectorSync } from '../../../src'; -describeNode('hostDetector() on Node.js', () => { +describeNode('hostDetectorSync() on Node.js', () => { afterEach(() => { sinon.restore(); }); @@ -39,16 +39,16 @@ describeNode('hostDetector() on Node.js', () => { sinon.stub(os, 'hostname').returns('opentelemetry-test'); sinon.stub(mid, 'getMachineId').returns(Promise.resolve(expectedHostId)); - const resource: IResource = await hostDetector.detect(); - await resource.waitForAsyncAttributes?.(); + const resource = await hostDetectorSync.detect(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_NAME], + attributes[SEMRESATTRS_HOST_NAME], 'opentelemetry-test' ); - assert.strictEqual(resource.attributes[SEMRESATTRS_HOST_ARCH], 'amd64'); + assert.strictEqual(attributes[SEMRESATTRS_HOST_ARCH], 'amd64'); assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_ID], + attributes[SEMRESATTRS_HOST_ID], expectedHostId ); }); @@ -58,10 +58,11 @@ describeNode('hostDetector() on Node.js', () => { sinon.stub(os, 'arch').returns('some-unknown-arch'); - const resource: IResource = await hostDetector.detect(); + const resource = await hostDetectorSync.detect(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_ARCH], + attributes[SEMRESATTRS_HOST_ARCH], 'some-unknown-arch' ); }); @@ -74,14 +75,14 @@ describeNode('hostDetector() on Node.js', () => { sinon.stub(os, 'hostname').returns('opentelemetry-test'); sinon.stub(mid, 'getMachineId').returns(Promise.resolve('')); - const resource: IResource = await hostDetector.detect(); - await resource.waitForAsyncAttributes?.(); + const resource = await hostDetectorSync.detect(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_NAME], + attributes[SEMRESATTRS_HOST_NAME], 'opentelemetry-test' ); - assert.strictEqual(resource.attributes[SEMRESATTRS_HOST_ARCH], 'amd64'); + assert.strictEqual(attributes[SEMRESATTRS_HOST_ARCH], 'amd64'); assert.strictEqual(false, SEMRESATTRS_HOST_ID in resource.attributes); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/node/OSDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/OSDetector.test.ts index 526f2405990..5838d38677d 100644 --- a/packages/opentelemetry-resources/test/detectors/node/OSDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/OSDetector.test.ts @@ -21,9 +21,9 @@ import { SEMRESATTRS_OS_VERSION, } from '@opentelemetry/semantic-conventions'; import { describeNode } from '../../util'; -import { osDetector, IResource } from '../../../src'; +import { osDetectorSync } from '../../../src'; -describeNode('osDetector() on Node.js', () => { +describeNode('osDetectorSync() on Node.js', () => { afterEach(() => { sinon.restore(); }); @@ -34,11 +34,12 @@ describeNode('osDetector() on Node.js', () => { sinon.stub(os, 'platform').returns('win32'); sinon.stub(os, 'release').returns('2.2.1(0.289/5/3)'); - const resource: IResource = await osDetector.detect(); + const resource = await osDetectorSync.detect(); + const attributes = await resource.attributes; - assert.strictEqual(resource.attributes[SEMRESATTRS_OS_TYPE], 'windows'); + assert.strictEqual(attributes[SEMRESATTRS_OS_TYPE], 'windows'); assert.strictEqual( - resource.attributes[SEMRESATTRS_OS_VERSION], + attributes[SEMRESATTRS_OS_VERSION], '2.2.1(0.289/5/3)' ); }); @@ -48,10 +49,11 @@ describeNode('osDetector() on Node.js', () => { sinon.stub(os, 'platform').returns('some-unknown-platform'); - const resource: IResource = await osDetector.detect(); + const resource = await osDetectorSync.detect(); + const attributes = await resource.attributes; assert.strictEqual( - resource.attributes[SEMRESATTRS_OS_TYPE], + attributes[SEMRESATTRS_OS_TYPE], 'some-unknown-platform' ); }); diff --git a/packages/opentelemetry-resources/test/detectors/node/ProcessDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/ProcessDetector.test.ts index 96fafbe44a8..586caa40978 100644 --- a/packages/opentelemetry-resources/test/detectors/node/ProcessDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/ProcessDetector.test.ts @@ -14,12 +14,12 @@ * limitations under the License. */ import * as sinon from 'sinon'; -import { processDetector, IResource } from '../../../src'; +import { processDetectorSync } from '../../../src'; import { assertResource } from '../../util/resource-assertions'; import { describeNode } from '../../util'; import * as os from 'os'; -describeNode('processDetector() on Node.js', () => { +describeNode('processDetectorSync() on Node.js', () => { afterEach(() => { sinon.restore(); }); @@ -36,8 +36,9 @@ describeNode('processDetector() on Node.js', () => { .stub(os, 'userInfo') .returns({ username: 'appOwner' } as os.UserInfo); - const resource: IResource = await processDetector.detect(); - assertResource(resource, { + const resource = await processDetectorSync.detect(); + const attributes = await resource.attributes; + assertResource(attributes, { pid: 1234, name: 'otProcess', command: '/home/ot/test.js', @@ -61,9 +62,10 @@ describeNode('processDetector() on Node.js', () => { sinon.stub(process, 'title').value(''); sinon.stub(process, 'argv').value([]); sinon.stub(process, 'versions').value({ node: '1.4.1' }); - const resource: IResource = await processDetector.detect(); + const resource = await processDetectorSync.detect(); + const attributes = await resource.attributes; // at a minium we should be able to rely on pid runtime, runtime name, and description - assertResource(resource, { + assertResource(attributes, { pid: 1234, version: '1.4.1', runtimeDescription: 'Node.js', diff --git a/packages/opentelemetry-resources/test/regression/existing-detectors-1-9-1.test.ts b/packages/opentelemetry-resources/test/regression/existing-detectors-1-9-1.test.ts deleted file mode 100644 index 56c996744f2..00000000000 --- a/packages/opentelemetry-resources/test/regression/existing-detectors-1-9-1.test.ts +++ /dev/null @@ -1,31 +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. - */ -import { Resource, Detector, ResourceDetectionConfig } from '../../src'; -import * as assert from 'assert'; - -// DO NOT MODIFY THIS DETECTOR: Previous detectors used Resource as IResource did not yet exist. -// If compilation fails at this point then the changes made are breaking. -class RegressionTestResourceDetector_1_9_1 implements Detector { - async detect(_config?: ResourceDetectionConfig): Promise { - return Resource.empty(); - } -} - -describe('Regression Test @opentelemetry/resources@1.9.1', () => { - it('constructor', () => { - assert.ok(new RegressionTestResourceDetector_1_9_1()); - }); -}); diff --git a/packages/opentelemetry-resources/test/resource-assertions.test.ts b/packages/opentelemetry-resources/test/resource-assertions.test.ts index 8fc4872279c..3b787a50c99 100644 --- a/packages/opentelemetry-resources/test/resource-assertions.test.ts +++ b/packages/opentelemetry-resources/test/resource-assertions.test.ts @@ -53,21 +53,21 @@ import { } from './util/resource-assertions'; describe('assertCloudResource', () => { - it('requires one cloud label', () => { + it('requires one cloud label', async () => { const resource = new Resource({ [SEMRESATTRS_CLOUD_PROVIDER]: 'gcp', }); - assertCloudResource(resource, {}); + assertCloudResource(await resource.attributes, {}); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_CLOUD_PROVIDER]: 'gcp', [SEMRESATTRS_CLOUD_ACCOUNT_ID]: 'opentelemetry', [SEMRESATTRS_CLOUD_REGION]: 'us-central1', [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: 'us-central1-a', }); - assertCloudResource(resource, { + assertCloudResource(await resource.attributes, { provider: 'gcp', accountId: 'opentelemetry', region: 'us-central1', @@ -77,21 +77,21 @@ describe('assertCloudResource', () => { }); describe('assertContainerResource', () => { - it('requires one container label', () => { + it('requires one container label', async () => { const resource = new Resource({ [SEMRESATTRS_CONTAINER_NAME]: 'opentelemetry-autoconf', }); - assertContainerResource(resource, {}); + assertContainerResource(await resource.attributes, {}); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_CONTAINER_NAME]: 'opentelemetry-autoconf', [SEMRESATTRS_CONTAINER_ID]: 'abc', [SEMRESATTRS_CONTAINER_IMAGE_NAME]: 'gcr.io/opentelemetry/operator', [SEMRESATTRS_CONTAINER_IMAGE_TAG]: '0.1', }); - assertContainerResource(resource, { + assertContainerResource(await resource.attributes, { name: 'opentelemetry-autoconf', id: 'abc', imageName: 'gcr.io/opentelemetry/operator', @@ -101,14 +101,14 @@ describe('assertContainerResource', () => { }); describe('assertHostResource', () => { - it('requires one host label', () => { + it('requires one host label', async () => { const resource = new Resource({ [SEMRESATTRS_HOST_ID]: 'opentelemetry-test-id', }); - assertHostResource(resource, {}); + assertHostResource(await resource.attributes, {}); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_HOST_ID]: 'opentelemetry-test-id', [SEMRESATTRS_HOST_NAME]: 'opentelemetry-test-name', @@ -118,7 +118,7 @@ describe('assertHostResource', () => { [SEMRESATTRS_HOST_IMAGE_ID]: 'ami-07b06b442921831e5', [SEMRESATTRS_HOST_IMAGE_VERSION]: '0.1', }); - assertHostResource(resource, { + assertHostResource(await resource.attributes, { hostName: 'opentelemetry-test-hostname', id: 'opentelemetry-test-id', name: 'opentelemetry-test-name', @@ -131,21 +131,21 @@ describe('assertHostResource', () => { }); describe('assertK8sResource', () => { - it('requires one k8s label', () => { + it('requires one k8s label', async () => { const resource = new Resource({ [SEMRESATTRS_K8S_CLUSTER_NAME]: 'opentelemetry-cluster', }); - assertK8sResource(resource, {}); + assertK8sResource(await resource.attributes, {}); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_K8S_CLUSTER_NAME]: 'opentelemetry-cluster', [SEMRESATTRS_K8S_NAMESPACE_NAME]: 'default', [SEMRESATTRS_K8S_POD_NAME]: 'opentelemetry-pod-autoconf', [SEMRESATTRS_K8S_DEPLOYMENT_NAME]: 'opentelemetry', }); - assertK8sResource(resource, { + assertK8sResource(await resource.attributes, { clusterName: 'opentelemetry-cluster', namespaceName: 'default', podName: 'opentelemetry-pod-autoconf', @@ -155,7 +155,7 @@ describe('assertK8sResource', () => { }); describe('assertTelemetrySDKResource', () => { - it('uses default validations', () => { + it('uses default validations', async () => { const resource = new Resource({ [SEMRESATTRS_TELEMETRY_SDK_NAME]: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME], @@ -164,16 +164,16 @@ describe('assertTelemetrySDKResource', () => { [SEMRESATTRS_TELEMETRY_SDK_VERSION]: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION], }); - assertTelemetrySDKResource(resource, {}); + assertTelemetrySDKResource(await resource.attributes, {}); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_TELEMETRY_SDK_NAME]: 'opentelemetry', [SEMRESATTRS_TELEMETRY_SDK_LANGUAGE]: 'nodejs', [SEMRESATTRS_TELEMETRY_SDK_VERSION]: '0.1.0', }); - assertTelemetrySDKResource(resource, { + assertTelemetrySDKResource(await resource.attributes, { name: 'opentelemetry', language: 'nodejs', version: '0.1.0', @@ -182,25 +182,25 @@ describe('assertTelemetrySDKResource', () => { }); describe('assertServiceResource', () => { - it('validates required attributes', () => { + it('validates required attributes', async () => { const resource = new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'shoppingcart', [SEMRESATTRS_SERVICE_INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', }); - assertServiceResource(resource, { + assertServiceResource(await resource.attributes, { name: 'shoppingcart', instanceId: '627cc493-f310-47de-96bd-71410b7dec09', }); }); - it('validates optional attributes', () => { + it('validates optional attributes', async () => { const resource = new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'shoppingcart', [SEMRESATTRS_SERVICE_INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', [SEMRESATTRS_SERVICE_NAMESPACE]: 'shop', [SEMRESATTRS_SERVICE_VERSION]: '0.1.0', }); - assertServiceResource(resource, { + assertServiceResource(await resource.attributes, { name: 'shoppingcart', instanceId: '627cc493-f310-47de-96bd-71410b7dec09', namespace: 'shop', diff --git a/packages/opentelemetry-resources/test/util/resource-assertions.ts b/packages/opentelemetry-resources/test/util/resource-assertions.ts index 61ae831fabe..1a5fbff272d 100644 --- a/packages/opentelemetry-resources/test/util/resource-assertions.ts +++ b/packages/opentelemetry-resources/test/util/resource-assertions.ts @@ -16,7 +16,6 @@ import { SDK_INFO } from '@opentelemetry/core'; import * as assert from 'assert'; -import { IResource } from '../../src/IResource'; import { SEMRESATTRS_CLOUD_ACCOUNT_ID, SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, @@ -57,6 +56,7 @@ import { SEMRESATTRS_WEBENGINE_VERSION, } from '@opentelemetry/semantic-conventions'; import * as semconv from '@opentelemetry/semantic-conventions'; +import { Attributes } from '@opentelemetry/api'; /** * Test utility method to validate a cloud resource @@ -65,7 +65,7 @@ import * as semconv from '@opentelemetry/semantic-conventions'; * @param validations validations for the resource attributes */ export const assertCloudResource = ( - resource: IResource, + attributes: Attributes, validations: { provider?: string; accountId?: string; @@ -73,25 +73,25 @@ export const assertCloudResource = ( zone?: string; } ) => { - assertHasOneLabel('CLOUD', resource); + assertHasOneLabel('CLOUD', attributes); if (validations.provider) assert.strictEqual( - resource.attributes[SEMRESATTRS_CLOUD_PROVIDER], + attributes[SEMRESATTRS_CLOUD_PROVIDER], validations.provider ); if (validations.accountId) assert.strictEqual( - resource.attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID], + attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID], validations.accountId ); if (validations.region) assert.strictEqual( - resource.attributes[SEMRESATTRS_CLOUD_REGION], + attributes[SEMRESATTRS_CLOUD_REGION], validations.region ); if (validations.zone) assert.strictEqual( - resource.attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE], + attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE], validations.zone ); }; @@ -103,7 +103,7 @@ export const assertCloudResource = ( * @param validations validations for the resource attributes */ export const assertContainerResource = ( - resource: IResource, + attributes: Attributes, validations: { name?: string; id?: string; @@ -111,25 +111,25 @@ export const assertContainerResource = ( imageTag?: string; } ) => { - assertHasOneLabel('CONTAINER', resource); + assertHasOneLabel('CONTAINER', attributes); if (validations.name) assert.strictEqual( - resource.attributes[SEMRESATTRS_CONTAINER_NAME], + attributes[SEMRESATTRS_CONTAINER_NAME], validations.name ); if (validations.id) assert.strictEqual( - resource.attributes[SEMRESATTRS_CONTAINER_ID], + attributes[SEMRESATTRS_CONTAINER_ID], validations.id ); if (validations.imageName) assert.strictEqual( - resource.attributes[SEMRESATTRS_CONTAINER_IMAGE_NAME], + attributes[SEMRESATTRS_CONTAINER_IMAGE_NAME], validations.imageName ); if (validations.imageTag) assert.strictEqual( - resource.attributes[SEMRESATTRS_CONTAINER_IMAGE_TAG], + attributes[SEMRESATTRS_CONTAINER_IMAGE_TAG], validations.imageTag ); }; @@ -141,7 +141,7 @@ export const assertContainerResource = ( * @param validations validations for the resource attributes */ export const assertHostResource = ( - resource: IResource, + attributes: Attributes, validations: { hostName?: string; id?: string; @@ -152,35 +152,35 @@ export const assertHostResource = ( imageVersion?: string; } ) => { - assertHasOneLabel('HOST', resource); + assertHasOneLabel('HOST', attributes); if (validations.id) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_ID], + attributes[SEMRESATTRS_HOST_ID], validations.id ); if (validations.name) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_NAME], + attributes[SEMRESATTRS_HOST_NAME], validations.name ); if (validations.hostType) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_TYPE], + attributes[SEMRESATTRS_HOST_TYPE], validations.hostType ); if (validations.imageName) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_IMAGE_NAME], + attributes[SEMRESATTRS_HOST_IMAGE_NAME], validations.imageName ); if (validations.imageId) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_IMAGE_ID], + attributes[SEMRESATTRS_HOST_IMAGE_ID], validations.imageId ); if (validations.imageVersion) assert.strictEqual( - resource.attributes[SEMRESATTRS_HOST_IMAGE_VERSION], + attributes[SEMRESATTRS_HOST_IMAGE_VERSION], validations.imageVersion ); }; @@ -192,7 +192,7 @@ export const assertHostResource = ( * @param validations validations for the resource attributes */ export const assertK8sResource = ( - resource: IResource, + attributes: Attributes, validations: { clusterName?: string; namespaceName?: string; @@ -200,25 +200,25 @@ export const assertK8sResource = ( deploymentName?: string; } ) => { - assertHasOneLabel('K8S', resource); + assertHasOneLabel('K8S', attributes); if (validations.clusterName) assert.strictEqual( - resource.attributes[SEMRESATTRS_K8S_CLUSTER_NAME], + attributes[SEMRESATTRS_K8S_CLUSTER_NAME], validations.clusterName ); if (validations.namespaceName) assert.strictEqual( - resource.attributes[SEMRESATTRS_K8S_NAMESPACE_NAME], + attributes[SEMRESATTRS_K8S_NAMESPACE_NAME], validations.namespaceName ); if (validations.podName) assert.strictEqual( - resource.attributes[SEMRESATTRS_K8S_POD_NAME], + attributes[SEMRESATTRS_K8S_POD_NAME], validations.podName ); if (validations.deploymentName) assert.strictEqual( - resource.attributes[SEMRESATTRS_K8S_DEPLOYMENT_NAME], + attributes[SEMRESATTRS_K8S_DEPLOYMENT_NAME], validations.deploymentName ); }; @@ -230,7 +230,7 @@ export const assertK8sResource = ( * @param validations validations for the resource attributes */ export const assertTelemetrySDKResource = ( - resource: IResource, + attributes: Attributes, validations: { name?: string; language?: string; @@ -246,17 +246,17 @@ export const assertTelemetrySDKResource = ( if (validations.name) assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], + attributes[SEMRESATTRS_TELEMETRY_SDK_NAME], validations.name ); if (validations.language) assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], + attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE], validations.language ); if (validations.version) assert.strictEqual( - resource.attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], + attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION], validations.version ); }; @@ -268,7 +268,7 @@ export const assertTelemetrySDKResource = ( * @param validations validations for the resource attributes */ export const assertServiceResource = ( - resource: IResource, + attributes: Attributes, validations: { name: string; instanceId: string; @@ -277,21 +277,21 @@ export const assertServiceResource = ( } ) => { assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_NAME], + attributes[SEMRESATTRS_SERVICE_NAME], validations.name ); assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], + attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], validations.instanceId ); if (validations.namespace) assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_NAMESPACE], + attributes[SEMRESATTRS_SERVICE_NAMESPACE], validations.namespace ); if (validations.version) assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_VERSION], + attributes[SEMRESATTRS_SERVICE_VERSION], validations.version ); }; @@ -303,7 +303,7 @@ export const assertServiceResource = ( * @param validations validations for the resource attributes */ export const assertResource = ( - resource: IResource, + attributes: Attributes, validations: { pid?: number; name?: string; @@ -317,61 +317,61 @@ export const assertResource = ( } ) => { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_PID], + attributes[SEMRESATTRS_PROCESS_PID], validations.pid ); if (validations.name) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_EXECUTABLE_NAME], + attributes[SEMRESATTRS_PROCESS_EXECUTABLE_NAME], validations.name ); } if (validations.command) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_COMMAND], + attributes[SEMRESATTRS_PROCESS_COMMAND], validations.command ); } if (validations.commandArgs) { assert.deepStrictEqual( - resource.attributes[SEMRESATTRS_PROCESS_COMMAND_ARGS], + attributes[SEMRESATTRS_PROCESS_COMMAND_ARGS], validations.commandArgs ); } if (validations.executablePath) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_EXECUTABLE_PATH], + attributes[SEMRESATTRS_PROCESS_EXECUTABLE_PATH], validations.executablePath ); } if (validations.owner) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_OWNER], + attributes[SEMRESATTRS_PROCESS_OWNER], validations.owner ); } if (validations.version) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_RUNTIME_VERSION], + attributes[SEMRESATTRS_PROCESS_RUNTIME_VERSION], validations.version ); } if (validations.runtimeName) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_RUNTIME_NAME], + attributes[SEMRESATTRS_PROCESS_RUNTIME_NAME], validations.runtimeName ); } if (validations.runtimeDescription) { assert.strictEqual( - resource.attributes[SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION], + attributes[SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION], validations.runtimeDescription ); } }; export const assertWebEngineResource = ( - resource: IResource, + attributes: Attributes, validations: { name?: string; version?: string; @@ -380,19 +380,19 @@ export const assertWebEngineResource = ( ) => { if (validations.name) { assert.strictEqual( - resource.attributes[SEMRESATTRS_WEBENGINE_NAME], + attributes[SEMRESATTRS_WEBENGINE_NAME], validations.name ); } if (validations.version) { assert.strictEqual( - resource.attributes[SEMRESATTRS_WEBENGINE_VERSION], + attributes[SEMRESATTRS_WEBENGINE_VERSION], validations.version ); } if (validations.description) { assert.strictEqual( - resource.attributes[SEMRESATTRS_WEBENGINE_DESCRIPTION], + attributes[SEMRESATTRS_WEBENGINE_DESCRIPTION], validations.description ); } @@ -401,17 +401,17 @@ export const assertWebEngineResource = ( /** * Test utility method to validate an empty resource * - * @param resource the Resource to validate + * @param attributes the Resource's attributes to validate */ -export const assertEmptyResource = (resource: IResource) => { - assert.strictEqual(Object.keys(resource.attributes).length, 0); +export const assertEmptyResource = (attributes: Attributes) => { + assert.strictEqual(Object.keys(attributes).length, 0); }; /** * Assert that the `resource` has at least one known attribute with the given * `prefix`. By "known", we mean it is an attribute defined in semconv. */ -const assertHasOneLabel = (prefix: string, resource: IResource): void => { +const assertHasOneLabel = (prefix: string, attributes: Attributes,): void => { const semconvModPrefix = `SEMRESATTRS_${prefix.toUpperCase()}_`; const knownAttrs: Set = new Set( Object.entries(semconv) @@ -420,7 +420,7 @@ const assertHasOneLabel = (prefix: string, resource: IResource): void => { ) .map(([, v]) => v as string) ); - const hasAttrs = Object.keys(resource.attributes).filter(k => + const hasAttrs = Object.keys(attributes).filter(k => knownAttrs.has(k) ); assert.ok( diff --git a/packages/opentelemetry-resources/test/util/sample-detector.ts b/packages/opentelemetry-resources/test/util/sample-detector.ts deleted file mode 100644 index abe088f8867..00000000000 --- a/packages/opentelemetry-resources/test/util/sample-detector.ts +++ /dev/null @@ -1,40 +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. - */ - -import { - SEMRESATTRS_CLOUD_ACCOUNT_ID, - SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, - SEMRESATTRS_CLOUD_PROVIDER, - SEMRESATTRS_CLOUD_REGION, - SEMRESATTRS_HOST_ID, - SEMRESATTRS_HOST_TYPE, -} from '@opentelemetry/semantic-conventions'; -import { Detector, Resource } from '../../src'; - -class SampleDetector implements Detector { - async detect(): Promise { - return new Resource({ - [SEMRESATTRS_CLOUD_PROVIDER]: 'provider', - [SEMRESATTRS_CLOUD_ACCOUNT_ID]: 'accountId', - [SEMRESATTRS_CLOUD_REGION]: 'region', - [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: 'zone', - [SEMRESATTRS_HOST_ID]: 'instanceId', - [SEMRESATTRS_HOST_TYPE]: 'instanceType', - }); - } -} - -export const sampleDetector = new SampleDetector();