This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtracer.ts
More file actions
133 lines (122 loc) · 4.04 KB
/
tracer.ts
File metadata and controls
133 lines (122 loc) · 4.04 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright 2018, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as cls from '../../internal/cls';
import {NoRecordSpan} from './no-record/no-record-span';
import {CoreTracerBase} from './tracer-base';
import * as types from './types';
/**
* This class represents a tracer with Continuation Local Storage (CLS).
*
* CLS helps keep tracking the root span over function calls automatically.
* It is capable of storing, propagating and retrieving arbitrary
* continuation-local data (also called "context").
* CLS comes with some performance overhead, you can read more about it here:
* https://github.com/othiym23/node-continuation-local-storage/issues/59
*/
export class CoreTracer extends CoreTracerBase implements types.Tracer {
/** Manage context automatic propagation */
private contextManager: cls.Namespace;
/** Constructs a new TraceImpl instance. */
constructor() {
super();
this.contextManager = cls.getNamespace();
this.clearCurrentTrace();
}
/** Gets the current root span. */
get currentRootSpan(): types.Span {
return this.contextManager.get('rootspan');
}
/** Sets the current root span. */
set currentRootSpan(root: types.Span) {
if (this.contextManager.active) {
this.contextManager.set('rootspan', root);
}
}
/**
* Sets span to CLS
* @param span Span to setup in context.
* @param fn A callback function that runs after context setup.
*/
withSpan<T>(span: types.Span, fn: () => T): T {
const self = this;
return self.contextManager.runAndReturn(() => {
self.currentRootSpan = span;
return fn();
});
}
/**
* Starts a root span and sets it to context.
* @param options A TraceOptions object to start a root span.
* @returns {Span} A new root span.
*/
startWithRootSpan<T>(
options: types.TraceOptions, fn: (root: types.Span) => T): T {
const rootSpan = this.startRootSpan(options);
return this.withSpan(rootSpan, () => {
return fn(rootSpan);
});
}
/** Clears the current root span. */
clearCurrentTrace() {
// TODO: Remove null reference and ts-ignore check.
//@ts-ignore
this.currentRootSpan = null;
}
/**
* Starts a span.
* @param [options] A SpanOptions object to start a child span.
*/
startChildSpan(options?: types.SpanOptions): types.Span {
if (!this.currentRootSpan) {
this.logger.debug(
'no current trace found - must start a new root span first');
}
return super.startChildSpan(Object.assign(
{
childOf: (options && options.childOf) || this.currentRootSpan ||
new NoRecordSpan()
},
options));
}
/**
* Binds the trace context to the given function.
* This is necessary in order to create child spans correctly in functions
* that are called asynchronously (for example, in a network response
* handler).
* @param fn A function to which to bind the trace context.
*/
wrap<T>(fn: types.Func<T>): types.Func<T> {
if (!this.active) {
return fn;
}
const namespace = this.contextManager;
return namespace.bind<T>(fn);
}
/**
* Binds the trace context to the given event emitter.
* This is necessary in order to create child spans correctly in event
* handlers.
* @param emitter An event emitter whose handlers should have
* the trace context binded to them.
*/
wrapEmitter(emitter: NodeJS.EventEmitter): void {
if (!this.active) {
return;
}
const namespace = this.contextManager;
namespace.bindEmitter(emitter);
}
}