Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Add support for knowing if a global tracer has been registered #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions src/global_tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Tracer from './tracer';

const noopTracer = new Tracer();
let _globalTracer: Tracer | null = null;
let _isRegistered: boolean = false;

// Allows direct importing/requiring of the global tracer:
//
Expand Down Expand Up @@ -43,6 +44,7 @@ const globalTracerDelegate = new GlobalTracerDelegate();
*/
export function initGlobalTracer(tracer: Tracer): void {
_globalTracer = tracer;
_isRegistered = true;
}

/**
Expand All @@ -55,3 +57,10 @@ export function globalTracer(): Tracer {
// order.
return globalTracerDelegate;
}

/**
* Returns true if a global tracer has been registered, otherwise false.
*/
export function isGlobalTracerRegistered(): boolean {
return _isRegistered;
}
5 changes: 4 additions & 1 deletion src/test/opentracing_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export function opentracingAPITests(): void {
'childOf',
'followsFrom',
'initGlobalTracer',
'globalTracer'
'globalTracer',
'isGlobalTracerRegistered'
];
for (const name of funcs) {
it(name + ' should be a function', () => {
Expand All @@ -47,7 +48,9 @@ export function opentracingAPITests(): void {
});

it('should use the global tracer', () => {
expect(opentracing.isGlobalTracerRegistered()).to.equal(false);
opentracing.initGlobalTracer(new TestTracer());
expect(opentracing.isGlobalTracerRegistered()).to.equal(true);
const tracer = opentracing.globalTracer();
const span = tracer.startSpan('test');
expect(span).to.equal(dummySpan);
Expand Down