1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Span = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const noop_logger_1 = require("../../common/noop-logger");
|
20 | const clock_1 = require("../../internal/clock");
|
21 | const util_1 = require("../../internal/util");
|
22 | const no_record_span_1 = require("./no-record/no-record-span");
|
23 | const types = require("./types");
|
24 | const STATUS_OK = {
|
25 | code: types.CanonicalCode.OK,
|
26 | };
|
27 |
|
28 | class Span {
|
29 |
|
30 | constructor(tracer, parent) {
|
31 |
|
32 | this.startedLocal = false;
|
33 |
|
34 | this.endedLocal = false;
|
35 |
|
36 | this.logger = noop_logger_1.noopLogger;
|
37 |
|
38 | this.attributes = {};
|
39 |
|
40 | this.annotations = [];
|
41 |
|
42 | this.messageEvents = [];
|
43 |
|
44 | this.links = [];
|
45 |
|
46 | this.remoteParent = false;
|
47 |
|
48 | this.name = 'span';
|
49 |
|
50 | this.kind = types.SpanKind.UNSPECIFIED;
|
51 |
|
52 | this.status = STATUS_OK;
|
53 |
|
54 | this.activeTraceParams = {};
|
55 |
|
56 | this.droppedAttributesCount = 0;
|
57 |
|
58 | this.droppedLinksCount = 0;
|
59 |
|
60 | this.droppedAnnotationsCount = 0;
|
61 |
|
62 | this.droppedMessageEventsCount = 0;
|
63 | this.tracer = tracer;
|
64 | this.className = this.constructor.name;
|
65 | this.id = util_1.randomSpanId();
|
66 | this.spansLocal = [];
|
67 | if (parent) {
|
68 | this.root = parent.root;
|
69 | this.parentSpan = parent;
|
70 | this.activeTraceParams = this.root.activeTraceParams;
|
71 | }
|
72 | else {
|
73 | this.root = this;
|
74 | }
|
75 | this.logger = (this.root && this.root.logger) || this.logger;
|
76 | }
|
77 |
|
78 | isRootSpan() {
|
79 | return false;
|
80 | }
|
81 |
|
82 | get traceId() {
|
83 | return this.root.traceId;
|
84 | }
|
85 |
|
86 | get traceState() {
|
87 | return this.root.traceState;
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 | get parentSpanId() {
|
94 | if (!this.parentSpan) {
|
95 | return 'no-parent';
|
96 | }
|
97 | return this.parentSpan.id;
|
98 | }
|
99 |
|
100 | get started() {
|
101 | return this.startedLocal;
|
102 | }
|
103 |
|
104 | get ended() {
|
105 | return this.endedLocal;
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 | get startTime() {
|
112 | if (!this.clock) {
|
113 | this.logger.debug('calling startTime() on null clock');
|
114 | return new Date();
|
115 | }
|
116 | return this.clock.startTime;
|
117 | }
|
118 |
|
119 | allDescendants() {
|
120 | return this.spansLocal.reduce((acc, cur) => {
|
121 | acc.push(cur);
|
122 | const desc = cur.allDescendants();
|
123 | acc = acc.concat(desc);
|
124 | return acc;
|
125 | }, []);
|
126 | }
|
127 |
|
128 | get spans() {
|
129 | return this.spansLocal;
|
130 | }
|
131 |
|
132 | get numberOfChildren() {
|
133 | return this.spansLocal.length;
|
134 | }
|
135 | |
136 |
|
137 |
|
138 |
|
139 | get endTime() {
|
140 | if (!this.clock) {
|
141 | this.logger.debug('calling endTime() on null clock');
|
142 | return new Date();
|
143 | }
|
144 | return this.clock.endTime;
|
145 | }
|
146 | |
147 |
|
148 |
|
149 | get duration() {
|
150 | if (!this.clock) {
|
151 | this.logger.debug('calling duration() on null clock');
|
152 | return 0;
|
153 | }
|
154 | return this.clock.duration;
|
155 | }
|
156 |
|
157 | get spanContext() {
|
158 | return {
|
159 | traceId: this.traceId,
|
160 | spanId: this.id,
|
161 | options: 0x1,
|
162 | traceState: this.traceState,
|
163 | };
|
164 | }
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | addAttribute(key, value) {
|
172 | if (this.attributes[key]) {
|
173 | delete this.attributes[key];
|
174 | }
|
175 | if (Object.keys(this.attributes).length >=
|
176 | this.activeTraceParams.numberOfAttributesPerSpan) {
|
177 | this.droppedAttributesCount++;
|
178 | const attributeKeyToDelete = Object.keys(this.attributes).shift();
|
179 | if (attributeKeyToDelete) {
|
180 | delete this.attributes[attributeKeyToDelete];
|
181 | }
|
182 | }
|
183 | const serializedValue = typeof value === 'object' ? JSON.stringify(value) : value;
|
184 | this.attributes[key] = serializedValue;
|
185 | }
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | addAnnotation(description, attributes = {}, timestamp = Date.now()) {
|
193 | if (this.annotations.length >=
|
194 | this.activeTraceParams.numberOfAnnontationEventsPerSpan) {
|
195 | this.annotations.shift();
|
196 | this.droppedAnnotationsCount++;
|
197 | }
|
198 | this.annotations.push({ description, attributes, timestamp });
|
199 | }
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | addLink(traceId, spanId, type, attributes = {}) {
|
208 | if (this.links.length >= this.activeTraceParams.numberOfLinksPerSpan) {
|
209 | this.links.shift();
|
210 | this.droppedLinksCount++;
|
211 | }
|
212 | this.links.push({ traceId, spanId, type, attributes });
|
213 | }
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | addMessageEvent(type, id, timestamp = Date.now(), uncompressedSize, compressedSize) {
|
224 | if (this.messageEvents.length >=
|
225 | this.activeTraceParams.numberOfMessageEventsPerSpan) {
|
226 | this.messageEvents.shift();
|
227 | this.droppedMessageEventsCount++;
|
228 | }
|
229 | this.messageEvents.push({
|
230 | type,
|
231 | id,
|
232 | timestamp,
|
233 | uncompressedSize,
|
234 | compressedSize,
|
235 | });
|
236 | }
|
237 | |
238 |
|
239 |
|
240 |
|
241 |
|
242 | setStatus(code, message) {
|
243 | this.status = { code, message };
|
244 | }
|
245 |
|
246 | start() {
|
247 | if (this.started) {
|
248 | this.logger.debug('calling %s.start() on already started %s %o', this.className, this.className, { id: this.id, name: this.name, type: this.kind });
|
249 | return;
|
250 | }
|
251 |
|
252 | if (this.parentSpan) {
|
253 | this.clock = new clock_1.Clock(this.parentSpan.clock.currentDate);
|
254 | }
|
255 | else {
|
256 | this.clock = new clock_1.Clock();
|
257 | }
|
258 | this.startedLocal = true;
|
259 | this.logger.debug('starting %s %o', this.className, {
|
260 | traceId: this.traceId,
|
261 | id: this.id,
|
262 | name: this.name,
|
263 | parentSpanId: this.parentSpanId,
|
264 | traceState: this.traceState,
|
265 | });
|
266 | if (this.isRootSpan())
|
267 | this.tracer.setCurrentRootSpan(this);
|
268 | this.tracer.onStartSpan(this);
|
269 | }
|
270 |
|
271 | end() {
|
272 | if (this.ended) {
|
273 | this.logger.debug('calling %s.end() on already ended %s %o', this.className, this.className, { id: this.id, name: this.name, type: this.kind });
|
274 | return;
|
275 | }
|
276 | if (!this.started) {
|
277 | this.logger.error('calling %s.end() on un-started %s %o', this.className, this.className, { id: this.id, name: this.name, type: this.kind });
|
278 | return;
|
279 | }
|
280 | this.startedLocal = false;
|
281 | this.endedLocal = true;
|
282 | this.clock.end();
|
283 |
|
284 |
|
285 | for (const span of this.spansLocal) {
|
286 | if (!span.ended && span.started) {
|
287 | span.truncate();
|
288 | }
|
289 | }
|
290 | this.tracer.onEndSpan(this);
|
291 | }
|
292 |
|
293 | truncate() {
|
294 | this.end();
|
295 | this.logger.debug('truncating %s %o', this.className, {
|
296 | id: this.id,
|
297 | name: this.name,
|
298 | });
|
299 | }
|
300 | |
301 |
|
302 |
|
303 |
|
304 | startChildSpan(options) {
|
305 | if (this.ended) {
|
306 | this.logger.debug('calling %s.startSpan() on ended %s %o', this.className, this.className, { id: this.id, name: this.name, kind: this.kind });
|
307 | return new no_record_span_1.NoRecordSpan(this.tracer);
|
308 | }
|
309 | if (!this.started) {
|
310 | this.logger.debug('calling %s.startSpan() on un-started %s %o', this.className, this.className, { id: this.id, name: this.name, kind: this.kind });
|
311 | return new no_record_span_1.NoRecordSpan(this.tracer);
|
312 | }
|
313 | const child = new Span(this.tracer, this);
|
314 | if (options && options.name)
|
315 | child.name = options.name;
|
316 | if (options && options.kind)
|
317 | child.kind = options.kind;
|
318 | child.start();
|
319 | this.spansLocal.push(child);
|
320 | return child;
|
321 | }
|
322 | }
|
323 | exports.Span = Span;
|
324 |
|
\ | No newline at end of file |