diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 65d1819485..9c45fb65d0 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -20,6 +20,15 @@ All notable changes to experimental packages in this project will be documented * (internal) the `@opentelemetry/otlp-exporter-proto-base` package has been removed, and will from now on be deprecated in `npm` * feat(instrumentation): remove default value for config in base instrumentation constructor [#4695](https://github.com/open-telemetry/opentelemetry-js/pull/4695): @blumamir * fix(instrumentation)!: remove unused supportedVersions from Instrumentation interface [#4694](https://github.com/open-telemetry/opentelemetry-js/pull/4694) @blumamir +* feat(instrumentation)!: simplify `registerInstrumentations()` API + * Breaking changes: + * removes `InstrumentationOptions` type + * occurrences of `InstrumentationOptions` are now replaced by `(Instrumentation | Instrumentation[])[]` + * migrate usages of `registerInstrumentations({instrumentations: fooInstrumentation})` to `registerInstrumentations({instrumentations: [fooInstrumentation]})` + * passing Instrumentation classes to `registerInstrumentations()` is now not possible anymore. +* feat(sdk-node)!: simplify type of `instrumentations` option + * Breaking changes: + * replaces `InstrumentationOptions` with `(Instrumentation | Instrumentation[])[]` ### :rocket: (Enhancement) diff --git a/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts b/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts index 266648bcf6..3c862f25e6 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts @@ -19,7 +19,6 @@ import { logs } from '@opentelemetry/api-logs'; import { disableInstrumentations, enableInstrumentations, - parseInstrumentationOptions, } from './autoLoaderUtils'; import { AutoLoaderOptions } from './types_internal'; @@ -32,12 +31,10 @@ import { AutoLoaderOptions } from './types_internal'; export function registerInstrumentations( options: AutoLoaderOptions ): () => void { - const { instrumentations } = parseInstrumentationOptions( - options.instrumentations - ); const tracerProvider = options.tracerProvider || trace.getTracerProvider(); const meterProvider = options.meterProvider || metrics.getMeterProvider(); const loggerProvider = options.loggerProvider || logs.getLoggerProvider(); + const instrumentations = options.instrumentations?.flat() ?? []; enableInstrumentations( instrumentations, diff --git a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts index 91ef7a653d..05107b2f19 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts @@ -16,34 +16,8 @@ import { TracerProvider, MeterProvider } from '@opentelemetry/api'; import { Instrumentation } from './types'; -import { AutoLoaderResult, InstrumentationOption } from './types_internal'; import { LoggerProvider } from '@opentelemetry/api-logs'; -/** - * Parses the options and returns instrumentations, node plugins and - * web plugins - * @param options - */ -export function parseInstrumentationOptions( - options: InstrumentationOption[] = [] -): AutoLoaderResult { - let instrumentations: Instrumentation[] = []; - for (let i = 0, j = options.length; i < j; i++) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const option = options[i] as any; - if (Array.isArray(option)) { - const results = parseInstrumentationOptions(option); - instrumentations = instrumentations.concat(results.instrumentations); - } else if (typeof option === 'function') { - instrumentations.push(new option()); - } else if ((option as Instrumentation).instrumentationName) { - instrumentations.push(option); - } - } - - return { instrumentations }; -} - /** * Enable instrumentations * @param instrumentations diff --git a/experimental/packages/opentelemetry-instrumentation/src/index.ts b/experimental/packages/opentelemetry-instrumentation/src/index.ts index 0185bfc79d..1e35147192 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/index.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/index.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export * from './autoLoader'; +export { registerInstrumentations } from './autoLoader'; export { InstrumentationBase } from './platform/index'; export { InstrumentationNodeModuleDefinition } from './instrumentationNodeModuleDefinition'; export { InstrumentationNodeModuleFile } from './instrumentationNodeModuleFile'; diff --git a/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts b/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts index ad71821fc3..6d678fea90 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts @@ -15,22 +15,15 @@ */ import { TracerProvider, MeterProvider } from '@opentelemetry/api'; -import { InstrumentationBase } from './platform'; import { Instrumentation } from './types'; import { LoggerProvider } from '@opentelemetry/api-logs'; -export type InstrumentationOption = - | typeof InstrumentationBase - | (typeof InstrumentationBase)[] - | Instrumentation - | Instrumentation[]; - export interface AutoLoaderResult { instrumentations: Instrumentation[]; } export interface AutoLoaderOptions { - instrumentations?: InstrumentationOption[]; + instrumentations?: (Instrumentation | Instrumentation[])[]; tracerProvider?: TracerProvider; meterProvider?: MeterProvider; loggerProvider?: LoggerProvider; diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoaderUtils.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/autoLoaderUtils.test.ts deleted file mode 100644 index cdf7e5fd9c..0000000000 --- a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoaderUtils.test.ts +++ /dev/null @@ -1,57 +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 * as assert from 'assert'; -import { InstrumentationBase } from '../../src'; -import { parseInstrumentationOptions } from '../../src/autoLoaderUtils'; - -class FooInstrumentation extends InstrumentationBase { - constructor() { - super('foo', '1', {}); - } - - init() { - return []; - } - - override enable() {} - - override disable() {} -} - -// const fooInstrumentation = new FooInstrumentation(); - -describe('autoLoaderUtils', () => { - describe('parseInstrumentationOptions', () => { - it('should create a new instrumentation from class', () => { - const { instrumentations } = parseInstrumentationOptions([ - FooInstrumentation as typeof InstrumentationBase, - ]); - assert.strictEqual(instrumentations.length, 1); - const instrumentation = instrumentations[0]; - assert.ok(instrumentation instanceof InstrumentationBase); - }); - - it('should return an instrumentation from Instrumentation', () => { - const { instrumentations } = parseInstrumentationOptions([ - new FooInstrumentation(), - ]); - assert.strictEqual(instrumentations.length, 1); - const instrumentation = instrumentations[0]; - assert.ok(instrumentation instanceof InstrumentationBase); - }); - }); -}); diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index 90bf2d96c3..d26ffea4ef 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -23,7 +23,7 @@ import { } from '@opentelemetry/api'; import { logs } from '@opentelemetry/api-logs'; import { - InstrumentationOption, + Instrumentation, registerInstrumentations, } from '@opentelemetry/instrumentation'; import { @@ -51,10 +51,7 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import { NodeSDKConfiguration } from './types'; import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter'; import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; -import { - getResourceDetectorsFromEnv, - parseInstrumentationOptions, -} from './utils'; +import { getResourceDetectorsFromEnv } from './utils'; /** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */ @@ -85,7 +82,7 @@ export class NodeSDK { }; private _loggerProviderConfig?: LoggerProviderConfig; private _meterProviderConfig?: MeterProviderConfig; - private _instrumentations: InstrumentationOption[]; + private _instrumentations: Instrumentation[]; private _resource: IResource; private _resourceDetectors: Array; @@ -196,11 +193,7 @@ export class NodeSDK { this._meterProviderConfig = meterProviderConfig; } - let instrumentations: InstrumentationOption[] = []; - if (configuration.instrumentations) { - instrumentations = configuration.instrumentations; - } - this._instrumentations = instrumentations; + this._instrumentations = configuration.instrumentations?.flat() ?? []; } /** @@ -289,9 +282,7 @@ export class NodeSDK { // TODO: This is a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609 // If the MeterProvider is not yet registered when instrumentations are registered, all metrics are dropped. // This code is obsolete once https://github.com/open-telemetry/opentelemetry-js/issues/3622 is implemented. - for (const instrumentation of parseInstrumentationOptions( - this._instrumentations - )) { + for (const instrumentation of this._instrumentations) { instrumentation.setMeterProvider(metrics.getMeterProvider()); } } diff --git a/experimental/packages/opentelemetry-sdk-node/src/types.ts b/experimental/packages/opentelemetry-sdk-node/src/types.ts index 2ed7cae48d..c3b2a1cd84 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/types.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/types.ts @@ -16,7 +16,7 @@ import type { ContextManager } from '@opentelemetry/api'; import { TextMapPropagator } from '@opentelemetry/api'; -import { InstrumentationOption } from '@opentelemetry/instrumentation'; +import { Instrumentation } from '@opentelemetry/instrumentation'; import { Detector, DetectorSync, IResource } from '@opentelemetry/resources'; import { LogRecordProcessor } from '@opentelemetry/sdk-logs'; import { MetricReader, View } from '@opentelemetry/sdk-metrics'; @@ -35,7 +35,7 @@ export interface NodeSDKConfiguration { logRecordProcessor: LogRecordProcessor; metricReader: MetricReader; views: View[]; - instrumentations: InstrumentationOption[]; + instrumentations: (Instrumentation | Instrumentation[])[]; resource: IResource; resourceDetectors: Array; sampler: Sampler; diff --git a/experimental/packages/opentelemetry-sdk-node/src/utils.ts b/experimental/packages/opentelemetry-sdk-node/src/utils.ts index 6591224506..4bc696f402 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/utils.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/utils.ts @@ -15,10 +15,6 @@ */ import { diag } from '@opentelemetry/api'; -import { - Instrumentation, - InstrumentationOption, -} from '@opentelemetry/instrumentation'; import { DetectorSync, envDetectorSync, @@ -28,29 +24,6 @@ import { serviceInstanceIdDetectorSync, } from '@opentelemetry/resources'; -// TODO: This part of a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609 -// If the MeterProvider is not yet registered when instrumentations are registered, all metrics are dropped. -// This code is obsolete once https://github.com/open-telemetry/opentelemetry-js/issues/3622 is implemented. -export function parseInstrumentationOptions( - options: InstrumentationOption[] = [] -): Instrumentation[] { - let instrumentations: Instrumentation[] = []; - for (let i = 0, j = options.length; i < j; i++) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const option = options[i] as any; - if (Array.isArray(option)) { - const results = parseInstrumentationOptions(option); - instrumentations = instrumentations.concat(results); - } else if (typeof option === 'function') { - instrumentations.push(new option()); - } else if ((option as Instrumentation).instrumentationName) { - instrumentations.push(option); - } - } - - return instrumentations; -} - const RESOURCE_DETECTOR_ENVIRONMENT = 'env'; const RESOURCE_DETECTOR_HOST = 'host'; const RESOURCE_DETECTOR_OS = 'os';