Skip to content

Commit

Permalink
fix: preventing double enable for instrumentation that has been alrea…
Browse files Browse the repository at this point in the history
…dy enabled (#2610)
  • Loading branch information
obecny committed Nov 10, 2021
1 parent bc50c7b commit 61cfbfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 61cfbfe

Please sign in to comment.