From 61cfbfedff74bb7d7b142b529970e9370a5ceeec Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Wed, 10 Nov 2021 10:20:04 +0100 Subject: [PATCH] fix: preventing double enable for instrumentation that has been already enabled (#2610) --- .../src/autoLoaderUtils.ts | 8 ++++++- .../test/common/autoLoader.test.ts | 22 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts index e070fb3d10..6463f6cf27 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts @@ -62,7 +62,13 @@ export function enableInstrumentations( if (meterProvider) { instrumentation.setMeterProvider(meterProvider); } - instrumentation.enable(); + // 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 + // instrumentations should be now enabled + if (!instrumentation.getConfig().enabled) { + instrumentation.enable(); + } } } diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts index c378f83fe6..3cacd2cbb2 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts @@ -72,10 +72,30 @@ describe('autoLoader', () => { } }); - it('should enable instrumentation', () => { + it('should enable disabled instrumentation', () => { + if (typeof unload === 'function') { + unload(); + unload = undefined; + } + instrumentation = new FooInstrumentation( + 'foo', + '1', + { enabled: false } + ); + enableSpy = sinon.spy(instrumentation, 'enable'); + setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider'); + unload = registerInstrumentations({ + instrumentations: [instrumentation], + tracerProvider, + meterProvider, + }); assert.strictEqual(enableSpy.callCount, 1); }); + it('should NOT enable enabled instrumentation', () => { + assert.strictEqual(enableSpy.callCount, 0); + }); + it('should set TracerProvider', () => { assert.strictEqual(setTracerProviderSpy.callCount, 1); assert.ok(setTracerProviderSpy.lastCall.args[0] === tracerProvider);