Skip to content

Commit

Permalink
fix: set delegate after successful registration (open-telemetry#105)
Browse files Browse the repository at this point in the history
Set the new TracerProvider as delegate in ProxyTracerProvider
only if global registration was successful.
  • Loading branch information
Flarna authored Jul 27, 2021
1 parent 89f2f6b commit 4711f90
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api/src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ export class TraceAPI {
* @returns true if the tracer provider was successfully registered, else false
*/
public setGlobalTracerProvider(provider: TracerProvider): boolean {
this._proxyTracerProvider.setDelegate(provider);
return registerGlobal(
const success = registerGlobal(
API_NAME,
this._proxyTracerProvider,
DiagAPI.instance()
);
if (success) {
this._proxyTracerProvider.setDelegate(provider);
}
return success;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ describe('API', () => {
assert.deepStrictEqual(span, dummySpan);
});

it('should set delegate only on success', () => {
assert.strictEqual(
api.trace.setGlobalTracerProvider(new TestTracerProvider()),
true
);
assert.strictEqual(
api.trace.setGlobalTracerProvider(new NoopTracerProvider()),
false
);
assert.ok(api.trace.getTracer('name') instanceof TestTracer);
});

class TestTracer extends NoopTracer {
override startSpan(name: string, options?: SpanOptions): Span {
return dummySpan;
Expand Down

0 comments on commit 4711f90

Please sign in to comment.