|
16 | 16 |
|
17 | 17 | import * as cls from '../../internal/cls'; |
18 | 18 | import {debug} from '../../internal/util'; |
19 | | -import {RootSpan, Span} from './types'; |
20 | | -import {TraceOptions, Tracer} from './types'; |
21 | | -import {OnEndSpanEventListener, Func } from './types'; |
| 19 | +import {SamplerImpl} from '../config/sampler'; |
22 | 20 | import {Sampler, TracerConfig} from '../config/types'; |
23 | 21 | import {Config} from '../config/types'; |
24 | | -import {SpanImpl} from './span'; |
25 | | -import {SamplerImpl} from '../config/sampler'; |
26 | | -import {RootSpanImpl} from './rootspan'; |
27 | 22 |
|
| 23 | +import {RootSpanImpl} from './rootspan'; |
| 24 | +import {SpanImpl} from './span'; |
| 25 | +import {RootSpan, Span} from './types'; |
| 26 | +import {TraceOptions, Tracer} from './types'; |
| 27 | +import {Func, OnEndSpanEventListener} from './types'; |
28 | 28 |
|
29 | 29 | export class TracerImpl implements Tracer { |
30 | | - |
31 | | - //public buffer: Buffer; |
32 | | - private activeLocal: boolean; |
33 | | - private contextManager: cls.Namespace; |
34 | | - private config: TracerConfig; |
35 | | - sampler: Sampler; |
36 | | - |
37 | | - //TODO: simple solution - to be rewied in future |
38 | | - private eventListenersLocal: OnEndSpanEventListener[] = []; |
39 | | - //TODO: temp solution |
40 | | - private endedTraces: RootSpan[] = []; |
41 | | - |
42 | | - constructor() { |
43 | | - this.activeLocal = false; |
44 | | - this.contextManager = cls.createNamespace(); |
45 | | - } |
46 | | - |
47 | | - get currentRootSpan(): RootSpan { |
48 | | - return this.contextManager.get('rootspan'); |
49 | | - } |
50 | | - |
51 | | - set currentRootSpan(root: RootSpan) { |
52 | | - this.contextManager.set('rootspan', root); |
53 | | - } |
54 | | - |
55 | | - start(config: TracerConfig): Tracer { |
56 | | - this.activeLocal = true; |
57 | | - this.config = config; |
58 | | - this.sampler = new SamplerImpl().probability(config.samplingRate); |
59 | | - return this; |
60 | | - } |
61 | | - |
62 | | - get eventListeners(): OnEndSpanEventListener[] { |
63 | | - return this.eventListenersLocal; |
64 | | - } |
65 | | - |
66 | | - stop() { |
67 | | - this.activeLocal = false; |
68 | | - } |
69 | | - |
70 | | - get active(): boolean { |
71 | | - return this.activeLocal; |
72 | | - } |
73 | | - |
74 | | - startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T { |
75 | | - return this.contextManager.runAndReturn((root) => { |
76 | | - let newRoot = null; |
77 | | - if(this.active) { |
78 | | - newRoot = new RootSpanImpl (this, options); |
79 | | - if (this.sampler.shouldSample(newRoot.traceId)) { |
80 | | - newRoot.start(); |
81 | | - this.currentRootSpan = newRoot; |
82 | | - return fn(newRoot); |
83 | | - } |
84 | | - } else { |
85 | | - debug("Tracer is inactive, can't start new RootSpan"); |
86 | | - } |
87 | | - return fn(newRoot); |
88 | | - }); |
89 | | - } |
90 | | - |
91 | | - onEndSpan(root: RootSpan): void { |
92 | | - if (!root) { |
93 | | - return debug('cannot end trace - no active trace found'); |
94 | | - } |
95 | | - if (this.currentRootSpan !== root) { |
96 | | - debug('currentRootSpan != root on notifyEnd. Need more investigation.'); |
| 30 | + // public buffer: Buffer; |
| 31 | + private activeLocal: boolean; |
| 32 | + private contextManager: cls.Namespace; |
| 33 | + private config: TracerConfig; |
| 34 | + sampler: Sampler; |
| 35 | + |
| 36 | + // TODO: simple solution - to be rewied in future |
| 37 | + private eventListenersLocal: OnEndSpanEventListener[] = []; |
| 38 | + // TODO: temp solution |
| 39 | + private endedTraces: RootSpan[] = []; |
| 40 | + |
| 41 | + constructor() { |
| 42 | + this.activeLocal = false; |
| 43 | + this.contextManager = cls.createNamespace(); |
| 44 | + this.clearCurrentTrace(); |
| 45 | + } |
| 46 | + |
| 47 | + get currentRootSpan(): RootSpan { |
| 48 | + return this.contextManager.get('rootspan'); |
| 49 | + } |
| 50 | + |
| 51 | + set currentRootSpan(root: RootSpan) { |
| 52 | + this.contextManager.set('rootspan', root); |
| 53 | + } |
| 54 | + |
| 55 | + start(config: TracerConfig): Tracer { |
| 56 | + this.activeLocal = true; |
| 57 | + this.config = config; |
| 58 | + this.sampler = new SamplerImpl().probability(config.samplingRate); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + get eventListeners(): OnEndSpanEventListener[] { |
| 63 | + return this.eventListenersLocal; |
| 64 | + } |
| 65 | + |
| 66 | + stop() { |
| 67 | + this.activeLocal = false; |
| 68 | + } |
| 69 | + |
| 70 | + get active(): boolean { |
| 71 | + return this.activeLocal; |
| 72 | + } |
| 73 | + |
| 74 | + startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T { |
| 75 | + return this.contextManager.runAndReturn((root) => { |
| 76 | + let newRoot = null; |
| 77 | + if (this.active) { |
| 78 | + newRoot = new RootSpanImpl(this, options); |
| 79 | + if (this.sampler.shouldSample(newRoot.traceId)) { |
| 80 | + newRoot.start(); |
| 81 | + this.currentRootSpan = newRoot; |
| 82 | + return fn(newRoot); |
97 | 83 | } |
98 | | - this.notifyEndSpan(root); |
99 | | - //this.clearCurrentTrace(); |
| 84 | + } else { |
| 85 | + debug('Tracer is inactive, can\'t start new RootSpan'); |
| 86 | + } |
| 87 | + return fn(newRoot); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + onEndSpan(root: RootSpan): void { |
| 92 | + if (!root) { |
| 93 | + debug('cannot end trace - no active trace found'); |
| 94 | + return; |
100 | 95 | } |
101 | | - |
102 | | - registerEndSpanListener(listner: OnEndSpanEventListener) { |
103 | | - this.eventListenersLocal.push(listner); |
| 96 | + if (this.currentRootSpan !== root) { |
| 97 | + debug('currentRootSpan != root on notifyEnd. Need more investigation.'); |
| 98 | + return; |
104 | 99 | } |
105 | | - |
106 | | - private notifyEndSpan(root: RootSpan) { |
107 | | - if (this.active) { |
108 | | - debug('starting to notify listeners the end of rootspans'); |
109 | | - if (this.eventListenersLocal && this.eventListenersLocal.length > 0) { |
110 | | - this.eventListenersLocal.forEach((listener) => listener.onEndSpan(root)); |
111 | | - } |
112 | | - } else { |
113 | | - debug('this tracer is inactivate cant notify endspan'); |
| 100 | + this.notifyEndSpan(root); |
| 101 | + // this.clearCurrentTrace(); |
| 102 | + } |
| 103 | + |
| 104 | + registerEndSpanListener(listner: OnEndSpanEventListener) { |
| 105 | + this.eventListenersLocal.push(listner); |
| 106 | + } |
| 107 | + |
| 108 | + private notifyEndSpan(root: RootSpan) { |
| 109 | + if (this.active) { |
| 110 | + debug('starting to notify listeners the end of rootspans'); |
| 111 | + if (this.eventListenersLocal && this.eventListenersLocal.length > 0) { |
| 112 | + for (const listener of this.eventListenersLocal) { |
| 113 | + listener.onEndSpan(root); |
114 | 114 | } |
| 115 | + } |
| 116 | + } else { |
| 117 | + debug('this tracer is inactivate cant notify endspan'); |
115 | 118 | } |
| 119 | + } |
116 | 120 |
|
117 | | - clearCurrentTrace() { |
118 | | - this.currentRootSpan = null; |
119 | | - } |
| 121 | + clearCurrentTrace() { |
| 122 | + this.currentRootSpan = null; |
| 123 | + } |
120 | 124 |
|
121 | | - startSpan(name?: string, type?: string, parentSpanId?: string): Span { |
122 | | - let newSpan: Span = null; |
123 | | - if (!this.currentRootSpan) { |
124 | | - debug('no current trace found - must start a new root span first'); |
125 | | - } else { |
126 | | - newSpan = this.currentRootSpan.startSpan(name, type, parentSpanId); |
127 | | - } |
128 | | - return newSpan; |
| 125 | + startSpan(name?: string, type?: string, parentSpanId?: string): Span { |
| 126 | + if (!this.currentRootSpan) { |
| 127 | + debug('no current trace found - must start a new root span first'); |
| 128 | + return null; |
129 | 129 | } |
130 | 130 |
|
131 | | - wrap<T>(fn: Func<T>): Func<T> { |
132 | | - if (!this.active) { |
133 | | - return fn; |
134 | | - } |
| 131 | + return this.currentRootSpan.startSpan(name, type, parentSpanId); |
| 132 | + } |
135 | 133 |
|
136 | | - // This is safe because isActive checks the value of this.namespace. |
137 | | - const namespace = this.contextManager as cls.Namespace; |
138 | | - return namespace.bind<T>(fn); |
| 134 | + wrap<T>(fn: Func<T>): Func<T> { |
| 135 | + if (!this.active) { |
| 136 | + return fn; |
139 | 137 | } |
140 | 138 |
|
141 | | - wrapEmitter(emitter: NodeJS.EventEmitter): void { |
142 | | - if (!this.active) { |
143 | | - return; |
144 | | - } |
| 139 | + // This is safe because isActive checks the value of this.namespace. |
| 140 | + const namespace = this.contextManager as cls.Namespace; |
| 141 | + return namespace.bind<T>(fn); |
| 142 | + } |
145 | 143 |
|
146 | | - // This is safe because isActive checks the value of this.namespace. |
147 | | - const namespace = this.contextManager as cls.Namespace; |
148 | | - namespace.bindEmitter(emitter); |
| 144 | + wrapEmitter(emitter: NodeJS.EventEmitter): void { |
| 145 | + if (!this.active) { |
| 146 | + return; |
149 | 147 | } |
150 | | -} |
151 | | - |
152 | | - |
153 | 148 |
|
| 149 | + // This is safe because isActive checks the value of this.namespace. |
| 150 | + const namespace = this.contextManager as cls.Namespace; |
| 151 | + namespace.bindEmitter(emitter); |
| 152 | + } |
| 153 | +} |
0 commit comments