This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathglobal_tracer.ts
57 lines (49 loc) · 1.7 KB
/
global_tracer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Tracer from './tracer';
const noopTracer = new Tracer();
let _globalTracer: Tracer | null = null;
// Allows direct importing/requiring of the global tracer:
//
// let globalTracer = require('opentracing/global');
// OR
// import globalTracer from 'opentracing/global';
//
// Acts a bridge to the global tracer that can be safely called before the
// global tracer is initialized. The purpose of the delegation is to avoid the
// sometimes nearly intractible initialization order problems that can arise in
// applications with a complex set of dependencies, while also avoiding the
// case where
class GlobalTracerDelegate extends Tracer {
startSpan(): any {
const tracer = _globalTracer || noopTracer;
return tracer.startSpan.apply(tracer, arguments);
}
inject(): any {
const tracer = _globalTracer || noopTracer;
return tracer.inject.apply(tracer, arguments);
}
extract(): any {
const tracer = _globalTracer || noopTracer;
return tracer.extract.apply(tracer, arguments);
}
}
const globalTracerDelegate = new GlobalTracerDelegate();
/**
* Set the global Tracer.
*
* The behavior is undefined if this function is called more than once.
*
* @param {Tracer} tracer - the Tracer implementation
*/
export function initGlobalTracer(tracer: Tracer): void {
_globalTracer = tracer;
}
/**
* Returns the global tracer.
*/
export function globalTracer(): Tracer {
// Return the delegate. Since the global tracer is largely a convenience
// (the user can always create their own tracers), the delegate is used to
// give the added convenience of not needing to worry about initialization
// order.
return globalTracerDelegate;
}