1 | ;
|
2 | /**
|
3 | * Copyright 2018, OpenCensus Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.CoreTracer = void 0;
|
19 | const cls = require("../../internal/cls");
|
20 | const no_record_span_1 = require("./no-record/no-record-span");
|
21 | const tracer_base_1 = require("./tracer-base");
|
22 | /**
|
23 | * This class represents a tracer with Continuation Local Storage (CLS).
|
24 | *
|
25 | * CLS helps keep tracking the root span over function calls automatically.
|
26 | * It is capable of storing, propagating and retrieving arbitrary
|
27 | * continuation-local data (also called "context").
|
28 | * CLS comes with some performance overhead, you can read more about it here:
|
29 | * https://github.com/othiym23/node-continuation-local-storage/issues/59
|
30 | */
|
31 | class CoreTracer extends tracer_base_1.CoreTracerBase {
|
32 | /** Constructs a new TraceImpl instance. */
|
33 | constructor() {
|
34 | super();
|
35 | this.contextManager = cls.getNamespace();
|
36 | this.clearCurrentTrace();
|
37 | }
|
38 | /** Gets the current root span. */
|
39 | get currentRootSpan() {
|
40 | return this.contextManager.get('rootspan');
|
41 | }
|
42 | /** Sets the current root span. */
|
43 | set currentRootSpan(root) {
|
44 | this.setCurrentRootSpan(root);
|
45 | }
|
46 | /** Sets the current root span. */
|
47 | setCurrentRootSpan(root) {
|
48 | if (this.contextManager.active) {
|
49 | this.contextManager.set('rootspan', root);
|
50 | }
|
51 | }
|
52 | /**
|
53 | * Starts a root span.
|
54 | * @param options A TraceOptions object to start a root span.
|
55 | * @param fn A callback function to run after starting a root span.
|
56 | */
|
57 | startRootSpan(options, fn) {
|
58 | const self = this;
|
59 | return self.contextManager.runAndReturn(() => {
|
60 | return super.startRootSpan(options, root => {
|
61 | return fn(root);
|
62 | });
|
63 | });
|
64 | }
|
65 | /** Notifies listeners of the span start. */
|
66 | onStartSpan(span) {
|
67 | if (!this.active)
|
68 | return;
|
69 | if (!this.currentRootSpan ||
|
70 | this.currentRootSpan.traceId !== span.traceId) {
|
71 | this.logger.debug('currentRootSpan != root on notifyStart. Need more investigation.');
|
72 | }
|
73 | return super.onStartSpan(span);
|
74 | }
|
75 | /** Notifies listeners of the span end. */
|
76 | onEndSpan(span) {
|
77 | if (!this.active)
|
78 | return;
|
79 | if (!this.currentRootSpan ||
|
80 | this.currentRootSpan.traceId !== span.traceId) {
|
81 | this.logger.debug('currentRootSpan != root on notifyEnd. Need more investigation.');
|
82 | }
|
83 | super.onEndSpan(span);
|
84 | }
|
85 | /** Clears the current root span. */
|
86 | clearCurrentTrace() {
|
87 | if (this.contextManager.active) {
|
88 | this.contextManager.set('rootspan', null);
|
89 | }
|
90 | }
|
91 | /**
|
92 | * Starts a span.
|
93 | * @param [options] A SpanOptions object to start a child span.
|
94 | */
|
95 | startChildSpan(options) {
|
96 | if (!this.currentRootSpan) {
|
97 | this.logger.debug('no current trace found - must start a new root span first');
|
98 | }
|
99 | return super.startChildSpan(Object.assign({ childOf: this.currentRootSpan || new no_record_span_1.NoRecordSpan(this) }, options));
|
100 | }
|
101 | /**
|
102 | * Binds the trace context to the given function.
|
103 | * This is necessary in order to create child spans correctly in functions
|
104 | * that are called asynchronously (for example, in a network response
|
105 | * handler).
|
106 | * @param fn A function to which to bind the trace context.
|
107 | */
|
108 | wrap(fn) {
|
109 | if (!this.active) {
|
110 | return fn;
|
111 | }
|
112 | const namespace = this.contextManager;
|
113 | return namespace.bind(fn);
|
114 | }
|
115 | /**
|
116 | * Binds the trace context to the given event emitter.
|
117 | * This is necessary in order to create child spans correctly in event
|
118 | * handlers.
|
119 | * @param emitter An event emitter whose handlers should have
|
120 | * the trace context binded to them.
|
121 | */
|
122 | wrapEmitter(emitter) {
|
123 | if (!this.active) {
|
124 | return;
|
125 | }
|
126 | const namespace = this.contextManager;
|
127 | namespace.bindEmitter(emitter);
|
128 | }
|
129 | }
|
130 | exports.CoreTracer = CoreTracer;
|
131 | //# sourceMappingURL=tracer.js.map |
\ | No newline at end of file |