Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

fix: set delegate after successful registration #105

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions 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 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