-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathsegment.js
More file actions
445 lines (353 loc) · 11.5 KB
/
segment.js
File metadata and controls
445 lines (353 loc) · 11.5 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var crypto = require('crypto');
var CapturedException = require('./attributes/captured_exception');
var SegmentEmitter = require('../segment_emitter');
var SegmentUtils = require('./segment_utils');
var Subsegment = require('./attributes/subsegment');
var TraceID = require('./attributes/trace_id');
var Utils = require('../utils');
var logger = require('../logger');
/**
* Represents a segment.
* @constructor
* @param {string} name - The name of the subsegment.
* @param {string} [rootId] - The trace ID of the spawning parent, included in the 'X-Amzn-Trace-Id' header of the incoming request. If one is not supplied, it will be generated.
* @param {string} [parentId] - The sub/segment ID of the spawning parent, included in the 'X-Amzn-Trace-Id' header of the incoming request.
*/
function Segment(name, rootId, parentId) {
this.init(name, rootId, parentId);
}
Segment.prototype.init = function init(name, rootId, parentId) {
if (typeof name != 'string') {
throw new Error('Segment name must be of type string.');
}
// Validate the Trace ID
var traceId;
if (rootId && typeof rootId == 'string') {
traceId = TraceID.FromString(rootId);
} else {
traceId = new TraceID();
}
var id = crypto.randomBytes(8).toString('hex');
var startTime = SegmentUtils.getCurrentTime();
this.trace_id = traceId.toString();
this.id = id;
this.start_time = startTime;
this.name = name || '';
this.in_progress = true;
this.counter = 0;
if (parentId) {
this.parent_id = parentId;
}
if (SegmentUtils.serviceData) {
this.setServiceData(SegmentUtils.serviceData);
}
if (SegmentUtils.pluginData) {
this.addPluginData(SegmentUtils.pluginData);
}
if (SegmentUtils.origin) {
this.origin = SegmentUtils.origin;
}
if (SegmentUtils.sdkData) {
this.setSDKData(SegmentUtils.sdkData);
}
};
/**
* Adds incoming request data to the http block of the segment.
* @param {IncomingRequestData} data - The data of the property to add.
*/
Segment.prototype.addIncomingRequestData = function addIncomingRequestData(data) {
this.http = data;
};
/**
* Adds a key-value pair that can be queryable through GetTraceSummaries.
* Only acceptable types are string, float/int and boolean.
* @param {string} key - The name of key to add.
* @param {boolean|string|number} value - The value to add for the given key.
*/
Segment.prototype.addAnnotation = function addAnnotation(key, value) {
if (typeof value !== 'boolean' && typeof value !== 'string' && !isFinite(value)) {
logger.getLogger().error('Failed to add annotation key: ' + key + ' value: ' + value + ' to subsegment ' +
this.name + '. Value must be of type string, number or boolean.');
return;
}
if (typeof key !== 'string') {
logger.getLogger().error('Failed to add annotation key: ' + key + ' value: ' + value + ' to subsegment ' +
this.name + '. Key must be of type string.');
return;
}
if (this.annotations === undefined) {
this.annotations = {};
}
this.annotations[key] = value;
};
/**
* Adds a User ID that can be queried from the X-Ray console. User ID
* must be a string.
* @param {string} user - The ID of the user corresponding to this segment
*/
Segment.prototype.setUser = function (user) {
if (typeof user !== 'string') {
logger.getLogger().error('Set user: ' + user + ' failed. User IDs must be of type string.');
}
this.user = user;
};
/**
* Adds a key-value pair to the metadata.default attribute when no namespace is given.
* Metadata is not queryable, but is recorded.
* @param {string} key - The name of the key to add.
* @param {object|null} value - The value of the associated key.
* @param {string} [namespace] - The property name to put the key/value pair under.
*/
Segment.prototype.addMetadata = function(key, value, namespace) {
if (typeof key !== 'string') {
logger.getLogger().error('Failed to add metadata key: ' + key + ' value: ' + value + ' to segment ' +
this.name + '. Key must be of type string.');
return;
}
if (namespace && typeof namespace !== 'string') {
logger.getLogger().error('Failed to add metadata key: ' + key + ' value: ' + value + ' to segment ' +
this.name + '. Namespace must be of type string.');
return;
}
var ns = namespace || 'default';
if (!this.metadata) {
this.metadata = {};
}
if (!this.metadata[ns]) {
this.metadata[ns] = {};
}
if (ns !== '__proto__') {
this.metadata[ns][key] = value !== null && value !== undefined ? value : '';
}
};
/**
* Adds data about the AWS X-Ray SDK onto the segment.
* @param {Object} data - Object that contains the version of the SDK, and other information.
*/
Segment.prototype.setSDKData = function setSDKData(data) {
if (!data) {
logger.getLogger().error('Add SDK data: ' + data + ' failed.' +
'Must not be empty.');
return;
}
if (!this.aws) {
this.aws = {};
}
this.aws.xray = data;
};
Segment.prototype.setMatchedSamplingRule = function setMatchedSamplingRule(ruleName) {
if (this.aws) {
this.aws = JSON.parse(JSON.stringify(this.aws));
}
if (this.aws && this.aws['xray']) {
this.aws.xray['rule_name'] = ruleName;
} else {
this.aws = {xray: {'rule_name': ruleName}};
}
};
/**
* Adds data about the service into the segment.
* @param {Object} data - Object that contains the version of the application, and other information.
*/
Segment.prototype.setServiceData = function setServiceData(data) {
if (!data) {
logger.getLogger().error('Add service data: ' + data + ' failed.' +
'Must not be empty.');
return;
}
this.service = data;
};
/**
* Adds a service with associated version data into the segment.
* @param {Object} data - The associated AWS data.
*/
Segment.prototype.addPluginData = function addPluginData(data) {
if (this.aws === undefined) {
this.aws = {};
}
Object.assign(this.aws, data);
};
/**
* Adds a new subsegment to the array of subsegments.
* @param {string} name - The name of the new subsegment to append.
*/
Segment.prototype.addNewSubsegment = function addNewSubsegment(name) {
var subsegment = new Subsegment(name);
this.addSubsegment(subsegment);
return subsegment;
};
Segment.prototype.addSubsegmentWithoutSampling = function addSubsegmentWithoutSampling(subsegment) {
this.addSubsegment(subsegment);
subsegment.notTraced = true;
};
Segment.prototype.addNewSubsegmentWithoutSampling = function addNewSubsegmentWithoutSampling(name) {
const subsegment = new Subsegment(name);
this.addSubsegment(subsegment);
subsegment.notTraced = true;
return subsegment;
};
/**
* Adds a subsegment to the array of subsegments.
* @param {Subsegment} subsegment - The subsegment to append.
*/
Segment.prototype.addSubsegment = function addSubsegment(subsegment) {
if (!(subsegment instanceof Subsegment)) {
throw new Error('Cannot add subsegment: ' + subsegment + '. Not a subsegment.');
}
if (this.subsegments === undefined) {
this.subsegments = [];
}
subsegment.segment = this;
subsegment.parent = this;
subsegment.notTraced = subsegment.parent.notTraced;
this.subsegments.push(subsegment);
if (!subsegment.end_time) {
this.incrementCounter(subsegment.counter);
}
};
/**
* Removes the subsegment from the subsegments array, used in subsegment streaming.
*/
Segment.prototype.removeSubsegment = function removeSubsegment(subsegment) {
if (!(subsegment instanceof Subsegment)) {
throw new Error('Failed to remove subsegment:' + subsegment + ' from subsegment "' + this.name +
'". Not a subsegment.');
}
if (this.subsegments !== undefined) {
var index = this.subsegments.indexOf(subsegment);
if (index >= 0) {
this.subsegments.splice(index, 1);
}
}
};
/**
* Adds error data into the segment.
* @param {Error|string} err - The error to capture.
* @param {boolean} [remote] - Flag for whether the exception caught was remote or not.
* @param {boolean} [fault] - Flag for whether the fault flag should be set.
*/
Segment.prototype.addError = function addError(err, remote, fault) {
if (err == null || typeof err !== 'object' && typeof(err) !== 'string') {
logger.getLogger().error('Failed to add error:' + err + ' to subsegment "' + this.name +
'". Not an object or string literal.');
return;
}
if (fault || fault === undefined) {
this.addFaultFlag();
}
if (this.exception) {
if (err === this.exception.ex) {
this.cause = { id: this.exception.cause };
delete this.exception;
return;
}
delete this.exception;
}
if (this.cause === undefined) {
this.cause = {
working_directory: process.cwd(),
exceptions: []
};
}
this.cause.exceptions.push(new CapturedException(err, remote));
};
/**
* Adds fault flag to the subsegment.
*/
Segment.prototype.addFaultFlag = function addFaultFlag() {
this.fault = true;
};
/**
* Adds error flag to the subsegment.
*/
Segment.prototype.addErrorFlag = function addErrorFlag() {
this.error = true;
};
/**
* Adds throttle flag to the subsegment.
*/
Segment.prototype.addThrottleFlag = function addThrottleFlag() {
this.throttle = true;
};
/**
* Returns a boolean indicating whether or not the segment has been closed.
* @returns {boolean} - Returns true if the subsegment is closed.
*/
Segment.prototype.isClosed = function isClosed() {
return !this.in_progress;
};
/**
* Each segment holds a counter of open subsegments. This increments the counter.
* @param {Number} [additional] - An additional amount to increment. Used when adding subsegment trees.
*/
Segment.prototype.incrementCounter = function incrementCounter(additional) {
this.counter = additional ? this.counter + additional + 1 : this.counter + 1;
if (this.counter > SegmentUtils.streamingThreshold && this.subsegments && this.subsegments.length > 0) {
var open = [];
this.subsegments.forEach(function(child) {
if (!child.streamSubsegments()) {
open.push(child);
}
});
this.subsegments = open;
}
};
/**
* Each segment holds a counter of open subsegments. This decrements
* the counter such that it can be called from a child and propagate up.
*/
Segment.prototype.decrementCounter = function decrementCounter() {
this.counter--;
if (this.counter <= 0 && this.isClosed()) {
this.flush();
}
};
/**
* Closes the current segment. This automatically sets the end time.
* @param {Error|string} [err] - The error to capture.
* @param {boolean} [remote] - Flag for whether the exception caught was remote or not.
* @param {boolean} [fault] - Flag for whether the fault flag should be set.
*/
Segment.prototype.close = function(err, remote, fault) {
if (!this.end_time) {
this.end_time = SegmentUtils.getCurrentTime();
}
if (err !== undefined) {
this.addError(err, remote, fault);
}
delete this.in_progress;
delete this.exception;
if (this.counter <= 0) {
this.flush();
}
};
/**
* Sends the segment to the daemon.
*/
Segment.prototype.flush = function flush() {
if (this.notTraced !== true) {
delete this.exception;
var thisCopy = Utils.objectWithoutProperties(
this,
['counter', 'notTraced'],
true
);
SegmentEmitter.send(thisCopy);
}
};
Segment.prototype.format = function format() {
var ignore = ['segment', 'parent', 'counter'];
if (this.subsegments == null || this.subsegments.length === 0) {
ignore.push('subsegments');
}
var thisCopy = Utils.objectWithoutProperties(
this,
ignore,
false
);
return JSON.stringify(thisCopy);
};
Segment.prototype.toString = function toString() {
return JSON.stringify(this);
};
module.exports = Segment;