diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 2d5ea268fa..98efacaa7e 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to experimental packages in this project will be documented ### :boom: Breaking Change +* Add `resourceDetectors` option to `NodeSDK` [#3210](https://github.com/open-telemetry/opentelemetry-js/issues/3210) + * `NodeSDK.detectResources()` function is no longer able to receive config as a parameter. + Instead, the detectors are passed to the constructor. + * chore(metrics-sdk): clean up exports [#3197](https://github.com/open-telemetry/opentelemetry-js/pull/3197) @pichlermarc * removes export for: * `AccumulationRecord` @@ -18,6 +22,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* Add `resourceDetectors` option to `NodeSDK` [#3210](https://github.com/open-telemetry/opentelemetry-js/issues/3210) + ### :bug: (Bug Fix) ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-sdk-node/README.md b/experimental/packages/opentelemetry-sdk-node/README.md index 5b288ba1f3..e0ed90099c 100644 --- a/experimental/packages/opentelemetry-sdk-node/README.md +++ b/experimental/packages/opentelemetry-sdk-node/README.md @@ -114,9 +114,14 @@ or configure each instrumentation individually. Configure a resource. Resources may also be detected by using the `autoDetectResources` method of the SDK. +### resourceDetectors + +Configure resource detectors. By default, the resource detectors are [envDetector, processDetector]. +NOTE: In order to enable the detection, the parameter `autoDetectResources` has to be `true`. + ### sampler -Configure a custom sampler. By default all traces will be sampled. +Configure a custom sampler. By default, all traces will be sampled. ### spanProcessor diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index 2c6005806d..11870ebf28 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -21,6 +21,7 @@ import { registerInstrumentations } from '@opentelemetry/instrumentation'; import { + Detector, detectResources, envDetector, processDetector, @@ -62,6 +63,7 @@ export class NodeSDK { private _instrumentations: InstrumentationOption[]; private _resource: Resource; + private _resourceDetectors: Detector[]; private _autoDetectResources: boolean; @@ -74,6 +76,7 @@ export class NodeSDK { */ public constructor(configuration: Partial = {}) { this._resource = configuration.resource ?? new Resource({}); + this._resourceDetectors = configuration.resourceDetectors ?? [envDetector, processDetector]; this._serviceName = configuration.serviceName; @@ -166,12 +169,9 @@ export class NodeSDK { } /** Detect resource attributes */ - public async detectResources( - config?: ResourceDetectionConfig - ): Promise { + public async detectResources(): Promise { const internalConfig: ResourceDetectionConfig = { - detectors: [envDetector, processDetector], - ...config, + detectors: this._resourceDetectors, }; this.addResource(await detectResources(internalConfig)); diff --git a/experimental/packages/opentelemetry-sdk-node/src/types.ts b/experimental/packages/opentelemetry-sdk-node/src/types.ts index 425b375bac..722201f03f 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/types.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/types.ts @@ -17,7 +17,7 @@ import type { ContextManager, SpanAttributes } from '@opentelemetry/api'; import { TextMapPropagator } from '@opentelemetry/api'; import { InstrumentationOption } from '@opentelemetry/instrumentation'; -import { Resource } from '@opentelemetry/resources'; +import { Detector, Resource } from '@opentelemetry/resources'; import { MetricReader, View } from '@opentelemetry/sdk-metrics'; import { Sampler, @@ -35,6 +35,7 @@ export interface NodeSDKConfiguration { views: View[] instrumentations: InstrumentationOption[]; resource: Resource; + resourceDetectors: Detector[]; sampler: Sampler; serviceName?: string; spanProcessor: SpanProcessor; diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index edad60c5ec..ebc07dc2bc 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -41,7 +41,7 @@ import * as assert from 'assert'; import * as semver from 'semver'; import * as Sinon from 'sinon'; import { NodeSDK } from '../src'; -import { envDetector, processDetector } from '@opentelemetry/resources'; +import {envDetector, processDetector, Resource} from '@opentelemetry/resources'; const DefaultContextManager = semver.gte(process.version, '14.8.0') @@ -284,19 +284,47 @@ describe('Node SDK', () => { delete process.env.OTEL_RESOURCE_ATTRIBUTES; }); - describe('with a buggy detector', () => { + describe('with a custom resource', () => { it('returns a merged resource', async () => { const sdk = new NodeSDK({ autoDetectResources: true, + resourceDetectors: [processDetector, { + async detect(): Promise { + return new Resource({'customAttr': 'someValue'}); + } + }, + envDetector] + }); + await sdk.detectResources(); + const resource = sdk['_resource']; + + assert.strictEqual( + resource.attributes['customAttr'], + 'someValue' + ); + + assertServiceResource(resource, { + instanceId: '627cc493', + name: 'my-service', + namespace: 'default', + version: '0.0.1', }); - await sdk.detectResources({ - detectors: [processDetector, { + }); + }); + + describe('with a buggy detector', () => { + it('returns a merged resource', async () => { + const sdk = new NodeSDK({ + autoDetectResources: true, + resourceDetectors: [processDetector, { detect() { throw new Error('Buggy detector'); } }, envDetector] }); + + await sdk.detectResources(); const resource = sdk['_resource']; assertServiceResource(resource, {