diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 5911c3af2f..a9bd8d8d16 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(instrumentation): allow LoggerProvider to be specified in Instrumentations [#4314](https://github.com/open-telemetry/opentelemetry-js/pull/4314) @hectorhdzg * feat(instrumentation): Make `init()` method public [#4418](https://github.com/open-telemetry/opentelemetry-js/pull/4418) * feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas * feat(node-sdk): add spanProcessors option [#4454](https://github.com/open-telemetry/opentelemetry-js/pull/4454) @naseemkullah diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index daf45f823c..4573b37616 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -78,12 +78,14 @@ "shimmer": "^1.2.1" }, "peerDependencies": { - "@opentelemetry/api": "^1.3.0" + "@opentelemetry/api": "^1.3.0", + "@opentelemetry/api-logs": "^0.46.0" }, "devDependencies": { "@babel/core": "7.23.6", "@babel/preset-env": "7.22.20", "@opentelemetry/api": "1.7.0", + "@opentelemetry/api-logs": "0.47.0", "@opentelemetry/sdk-metrics": "1.21.0", "@types/mocha": "10.0.6", "@types/node": "18.6.5", diff --git a/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts b/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts index c6a052c6c9..266648bcf6 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/autoLoader.ts @@ -15,6 +15,7 @@ */ import { trace, metrics } from '@opentelemetry/api'; +import { logs } from '@opentelemetry/api-logs'; import { disableInstrumentations, enableInstrumentations, @@ -36,8 +37,14 @@ export function registerInstrumentations( ); const tracerProvider = options.tracerProvider || trace.getTracerProvider(); const meterProvider = options.meterProvider || metrics.getMeterProvider(); + const loggerProvider = options.loggerProvider || logs.getLoggerProvider(); - enableInstrumentations(instrumentations, tracerProvider, meterProvider); + enableInstrumentations( + instrumentations, + tracerProvider, + meterProvider, + loggerProvider + ); return () => { disableInstrumentations(instrumentations); diff --git a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts index 3dc1927214..91ef7a653d 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts @@ -17,6 +17,7 @@ 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 @@ -52,7 +53,8 @@ export function parseInstrumentationOptions( export function enableInstrumentations( instrumentations: Instrumentation[], tracerProvider?: TracerProvider, - meterProvider?: MeterProvider + meterProvider?: MeterProvider, + loggerProvider?: LoggerProvider ): void { for (let i = 0, j = instrumentations.length; i < j; i++) { const instrumentation = instrumentations[i]; @@ -62,6 +64,9 @@ export function enableInstrumentations( if (meterProvider) { instrumentation.setMeterProvider(meterProvider); } + if (loggerProvider && instrumentation.setLoggerProvider) { + instrumentation.setLoggerProvider(loggerProvider); + } // instrumentations have been already enabled during creation // so enable only if user prevented that by setting enabled to false // this is to prevent double enabling but when calling register all diff --git a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts index 13b305d13c..88a6b03887 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -24,6 +24,7 @@ import { Tracer, TracerProvider, } from '@opentelemetry/api'; +import { Logger, LoggerProvider, logs } from '@opentelemetry/api-logs'; import * as shimmer from 'shimmer'; import { InstrumentationModuleDefinition, @@ -41,6 +42,7 @@ export abstract class InstrumentationAbstract private _tracer: Tracer; private _meter: Meter; + private _logger: Logger; protected _diag: DiagLogger; constructor( @@ -58,8 +60,8 @@ export abstract class InstrumentationAbstract }); this._tracer = trace.getTracer(instrumentationName, instrumentationVersion); - this._meter = metrics.getMeter(instrumentationName, instrumentationVersion); + this._logger = logs.getLogger(instrumentationName, instrumentationVersion); this._updateMetricInstruments(); } @@ -90,6 +92,22 @@ export abstract class InstrumentationAbstract this._updateMetricInstruments(); } + /* Returns logger */ + protected get logger(): Logger { + return this._logger; + } + + /** + * Sets LoggerProvider to this plugin + * @param loggerProvider + */ + public setLoggerProvider(loggerProvider: LoggerProvider): void { + this._logger = loggerProvider.getLogger( + this.instrumentationName, + this.instrumentationVersion + ); + } + /** * Sets the new metric instruments with the current Meter. */ diff --git a/experimental/packages/opentelemetry-instrumentation/src/types.ts b/experimental/packages/opentelemetry-instrumentation/src/types.ts index 760e31165f..3ef070f829 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/types.ts @@ -15,6 +15,7 @@ */ import { TracerProvider, MeterProvider } from '@opentelemetry/api'; +import { LoggerProvider } from '@opentelemetry/api-logs'; /** Interface Instrumentation to apply patch. */ export interface Instrumentation { @@ -43,6 +44,9 @@ export interface Instrumentation { /** Method to set meter provider */ setMeterProvider(meterProvider: MeterProvider): void; + /** Method to set logger provider */ + setLoggerProvider?(loggerProvider: LoggerProvider): void; + /** Method to set instrumentation config */ setConfig(config: InstrumentationConfig): void; diff --git a/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts b/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts index 2cff387711..ad71821fc3 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/types_internal.ts @@ -17,6 +17,7 @@ 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 @@ -32,4 +33,5 @@ export interface AutoLoaderOptions { instrumentations?: InstrumentationOption[]; tracerProvider?: TracerProvider; meterProvider?: MeterProvider; + loggerProvider?: LoggerProvider; } diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts index be53d1ca59..126f4c0944 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts @@ -22,6 +22,7 @@ import { } from '../../src'; import { MeterProvider } from '@opentelemetry/sdk-metrics'; +import { LoggerProvider } from '@opentelemetry/sdk-logs'; interface TestInstrumentationConfig extends InstrumentationConfig { isActive?: boolean; @@ -90,6 +91,23 @@ describe('BaseInstrumentation', () => { }); }); + describe('setLoggerProvider', () => { + it('should get a logger from provider', () => { + let called = true; + class TestLoggerProvider extends LoggerProvider { + override getLogger(name: any, version?: any, options?: any) { + called = true; + return super.getLogger(name, version, options); + } + } + instrumentation = new TestInstrumentation(); + if (instrumentation.setLoggerProvider) { + instrumentation.setLoggerProvider(new TestLoggerProvider()); + } + assert.strictEqual(called, true); + }); + }); + describe('getConfig', () => { it('should return instrumentation config', () => { const instrumentation: Instrumentation = new TestInstrumentation({ diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts index aa8c5582a3..e5532f2e5f 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts @@ -24,6 +24,7 @@ import { import * as assert from 'assert'; import * as sinon from 'sinon'; import { InstrumentationBase, registerInstrumentations } from '../../src'; +import { Logger, LoggerOptions, LoggerProvider } from '@opentelemetry/api-logs'; class DummyTracerProvider implements TracerProvider { getTracer(name: string, version?: string): Tracer { @@ -37,6 +38,12 @@ class DummyMeterProvider implements MeterProvider { } } +class DummyLoggerProvider implements LoggerProvider { + getLogger(name: string, version?: string, options?: LoggerOptions): Logger { + throw new Error('not implemented'); + } +} + class FooInstrumentation extends InstrumentationBase { init() { return []; @@ -63,17 +70,21 @@ describe('autoLoader', () => { let enableSpy: sinon.SinonSpy; let setTracerProviderSpy: sinon.SinonSpy; let setMeterProviderSpy: sinon.SinonSpy; + let setLoggerProviderSpy: sinon.SinonSpy; const tracerProvider = new DummyTracerProvider(); const meterProvider = new DummyMeterProvider(); + const loggerProvider = new DummyLoggerProvider(); beforeEach(() => { instrumentation = new FooInstrumentation('foo', '1', {}); enableSpy = sinon.spy(instrumentation, 'enable'); setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider'); setMeterProviderSpy = sinon.stub(instrumentation, 'setMeterProvider'); + setLoggerProviderSpy = sinon.stub(instrumentation, 'setLoggerProvider'); unload = registerInstrumentations({ instrumentations: [instrumentation], tracerProvider, meterProvider, + loggerProvider, }); }); @@ -96,10 +107,12 @@ describe('autoLoader', () => { enableSpy = sinon.spy(instrumentation, 'enable'); setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider'); setMeterProviderSpy = sinon.stub(instrumentation, 'setMeterProvider'); + setLoggerProviderSpy = sinon.stub(instrumentation, 'setLoggerProvider'); unload = registerInstrumentations({ instrumentations: [instrumentation], tracerProvider, meterProvider, + loggerProvider, }); assert.strictEqual(enableSpy.callCount, 1); }); @@ -119,6 +132,12 @@ describe('autoLoader', () => { assert.ok(setMeterProviderSpy.lastCall.args[0] === meterProvider); assert.strictEqual(setMeterProviderSpy.lastCall.args.length, 1); }); + + it('should set LoggerProvider', () => { + assert.strictEqual(setLoggerProviderSpy.callCount, 1); + assert.ok(setLoggerProviderSpy.lastCall.args[0] === loggerProvider); + assert.strictEqual(setLoggerProviderSpy.lastCall.args.length, 1); + }); }); }); }); diff --git a/experimental/packages/opentelemetry-instrumentation/tsconfig.esm.json b/experimental/packages/opentelemetry-instrumentation/tsconfig.esm.json index a673b46483..5affe32c2b 100644 --- a/experimental/packages/opentelemetry-instrumentation/tsconfig.esm.json +++ b/experimental/packages/opentelemetry-instrumentation/tsconfig.esm.json @@ -14,6 +14,9 @@ }, { "path": "../../../packages/sdk-metrics" + }, + { + "path": "../api-logs" } ] } diff --git a/experimental/packages/opentelemetry-instrumentation/tsconfig.esnext.json b/experimental/packages/opentelemetry-instrumentation/tsconfig.esnext.json index f70e767a6e..dae4bfa399 100644 --- a/experimental/packages/opentelemetry-instrumentation/tsconfig.esnext.json +++ b/experimental/packages/opentelemetry-instrumentation/tsconfig.esnext.json @@ -14,6 +14,9 @@ }, { "path": "../../../packages/sdk-metrics" + }, + { + "path": "../api-logs" } ] } diff --git a/experimental/packages/opentelemetry-instrumentation/tsconfig.json b/experimental/packages/opentelemetry-instrumentation/tsconfig.json index 668bce7426..5da185276f 100644 --- a/experimental/packages/opentelemetry-instrumentation/tsconfig.json +++ b/experimental/packages/opentelemetry-instrumentation/tsconfig.json @@ -15,6 +15,9 @@ }, { "path": "../../../packages/sdk-metrics" + }, + { + "path": "../api-logs" } ] } diff --git a/package-lock.json b/package-lock.json index 302a49e610..daff8df12e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3117,6 +3117,7 @@ "@babel/core": "7.23.6", "@babel/preset-env": "7.22.20", "@opentelemetry/api": "1.7.0", + "@opentelemetry/api-logs": "0.47.0", "@opentelemetry/sdk-metrics": "1.21.0", "@types/mocha": "10.0.6", "@types/node": "18.6.5", @@ -3149,7 +3150,8 @@ "node": ">=14" }, "peerDependencies": { - "@opentelemetry/api": "^1.3.0" + "@opentelemetry/api": "^1.3.0", + "@opentelemetry/api-logs": "^0.46.0" } }, "experimental/packages/opentelemetry-instrumentation-fetch": { @@ -3874,6 +3876,18 @@ "node": ">= 8" } }, + "experimental/packages/opentelemetry-instrumentation/node_modules/@opentelemetry/api-logs": { + "version": "0.47.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.47.0.tgz", + "integrity": "sha512-AR6UOVcWZkuibLR/7/OecYJasncAf6VstNV/KT5qHq1HShVFmJetcgim0KMog/ON23yHZQjT9GPVTwB0FEhPQA==", + "dev": true, + "dependencies": { + "@opentelemetry/api": "^1.0.0" + }, + "engines": { + "node": ">=14" + } + }, "experimental/packages/opentelemetry-instrumentation/node_modules/@webpack-cli/configtest": { "version": "2.1.1", "dev": true, @@ -40668,6 +40682,7 @@ "@babel/core": "7.23.6", "@babel/preset-env": "7.22.20", "@opentelemetry/api": "1.7.0", + "@opentelemetry/api-logs": "0.47.0", "@opentelemetry/sdk-metrics": "1.21.0", "@types/mocha": "10.0.6", "@types/node": "18.6.5", @@ -40702,6 +40717,15 @@ "webpack-merge": "5.10.0" }, "dependencies": { + "@opentelemetry/api-logs": { + "version": "0.47.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.47.0.tgz", + "integrity": "sha512-AR6UOVcWZkuibLR/7/OecYJasncAf6VstNV/KT5qHq1HShVFmJetcgim0KMog/ON23yHZQjT9GPVTwB0FEhPQA==", + "dev": true, + "requires": { + "@opentelemetry/api": "^1.0.0" + } + }, "@webpack-cli/configtest": { "version": "2.1.1", "dev": true,