Skip to content

Commit 61cfbfe

Browse files
authored
fix: preventing double enable for instrumentation that has been already enabled (#2610)
1 parent bc50c7b commit 61cfbfe

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

experimental/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ export function enableInstrumentations(
6262
if (meterProvider) {
6363
instrumentation.setMeterProvider(meterProvider);
6464
}
65-
instrumentation.enable();
65+
// instrumentations have been already enabled during creation
66+
// so enable only if user prevented that by setting enabled to false
67+
// this is to prevent double enabling but when calling register all
68+
// instrumentations should be now enabled
69+
if (!instrumentation.getConfig().enabled) {
70+
instrumentation.enable();
71+
}
6672
}
6773
}
6874

experimental/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,30 @@ describe('autoLoader', () => {
7272
}
7373
});
7474

75-
it('should enable instrumentation', () => {
75+
it('should enable disabled instrumentation', () => {
76+
if (typeof unload === 'function') {
77+
unload();
78+
unload = undefined;
79+
}
80+
instrumentation = new FooInstrumentation(
81+
'foo',
82+
'1',
83+
{ enabled: false }
84+
);
85+
enableSpy = sinon.spy(instrumentation, 'enable');
86+
setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider');
87+
unload = registerInstrumentations({
88+
instrumentations: [instrumentation],
89+
tracerProvider,
90+
meterProvider,
91+
});
7692
assert.strictEqual(enableSpy.callCount, 1);
7793
});
7894

95+
it('should NOT enable enabled instrumentation', () => {
96+
assert.strictEqual(enableSpy.callCount, 0);
97+
});
98+
7999
it('should set TracerProvider', () => {
80100
assert.strictEqual(setTracerProviderSpy.callCount, 1);
81101
assert.ok(setTracerProviderSpy.lastCall.args[0] === tracerProvider);

0 commit comments

Comments
 (0)