-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmetric.js
459 lines (332 loc) · 8.45 KB
/
metric.js
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
'use strict';
const metricConstants = {
TYPE_STATE: 'state',
TYPE_COUNTER: 'counter',
TYPE_PROFILE: 'profile',
TYPE_TRACE: 'trace',
CATEGORY_CPU: 'cpu',
CATEGORY_MEMORY: 'memory',
CATEGORY_GC: 'gc',
CATEGORY_RUNTIME: 'runtime',
CATEGORY_SPAN: 'span',
CATEGORY_CPU_PROFILE: 'cpu-profile',
CATEGORY_MEMORY_PROFILE: 'memory-profile',
CATEGORY_ASYNC_PROFILE: 'async-profile',
CATEGORY_ASYNC_TRACE: 'async-trace',
CATEGORY_ERROR_PROFILE: 'error-profile',
NAME_CPU_TIME: 'CPU time',
NAME_CPU_USAGE: 'CPU usage',
NAME_RSS: 'RSS',
NAME_USED_HEAP_SIZE: 'Used heap size',
NAME_TOTAL_HEAP_SIZE: 'Total heap size',
NAME_CPP_OBJECTS: 'C++ Objects',
NAME_GC_CYCLES: 'GC cycles',
NAME_GC_TIME: 'GC time',
NAME_EVENT_LOOP_TICKS: 'Event loop ticks',
NAME_EVENT_LOOP_IO_STAGE: 'Event loop I/O stage',
NAME_HEAP_ALLOCATION_RATE: 'Heap allocation rate',
NAME_ASYNC_CALL_TIMES: 'Async call times',
NAME_UNCAUGHT_EXCEPTIONS: 'Uncaught exceptions',
NAME_UNHANDLED_REJECTIONS: 'Unhandled rejections',
UNIT_NONE: '',
UNIT_MILLISECOND: 'millisecond',
UNIT_MICROSECOND: 'microsecond',
UNIT_NANOSECOND: 'nanosecond',
UNIT_BYTE: 'byte',
UNIT_KILOBYTE: 'kilobyte',
UNIT_PERCENT: 'percent',
TRIGGER_TIMER: 'timer',
TRIGGER_API: 'api'
};
class Metric {
constructor(agent, typ, category, name, unit) {
let self = this;
self.agent = agent;
self.id = agent.utils.generateSha1(`${agent.getOption('appName')}${agent.getOption('appEnvironment')}${agent.getOption('hostName')}${typ}${name}${category}${unit}`);
self.type = typ;
self.category = category;
self.name = name;
self.unit = unit;
self.measurement = null;
self.hasLastValue = false;
self.lastValue = null;
}
static get c() {
return metricConstants;
}
hasMeasurement() {
let self = this;
return !!self.measurement;
}
createMeasurement(trigger, value, duration, breakdown) {
let self = this;
let ready = true;
if (self.type === Metric.c.TYPE_COUNTER) {
if (!self.hasLastValue) {
ready = false;
self.hasLastValue = true;
self.lastValue = value;
}
else {
let tmpValue = value;
value = value - self.lastValue;
self.lastValue = tmpValue;
}
}
if (ready) {
self.measurement = new Measurement(
self.agent,
self.agent.utils.generateUuid(),
trigger,
value,
duration,
breakdown,
self.agent.utils.timestamp());
}
}
toJson() {
let self = this;
let measurementJson;
if (self.measurement) {
measurementJson = self.measurement.toJson();
}
let metricJson = {
id: self.id,
type: self.type,
category: self.category,
name: self.name,
unit: self.unit,
measurement: measurementJson
};
return metricJson;
}
}
exports.Metric = Metric;
class Measurement {
constructor(agent, id, trigger, value, duration, breakdown, timestamp) {
let self = this;
self.agent = agent;
self.id = id;
self.trigger = trigger;
self.value = value;
self.duration = duration;
self.breakdown = breakdown;
self.timestamp = timestamp;
}
toJson() {
let self = this;
let breakdownJson;
if (self.breakdown) {
breakdownJson = self.breakdown.toJson();
}
let measurementJson = {
id: self.id,
trigger: self.trigger,
value: self.value,
duration: self.duration,
breakdown: breakdownJson,
timestamp: self.timestamp
};
return measurementJson;
}
}
exports.Measurement = Measurement;
const breakdownConstants = {
TYPE_CALLGRAPH: 'callgraph',
TYPE_CALLSITE: 'callsite',
TYPE_ERROR: 'error'
};
const RESERVOIR_SIZE = 1000;
class Breakdown {
constructor(agent, name, typ) {
let self = this;
self.agent = agent;
self.name = name;
self.type = typ;
self.metadata = new Map();
self.measurement = 0;
self.numSamples = 0;
self.reservoir = null;
self.children = new Map();
self._overhead = 0;
}
static get c() {
return breakdownConstants;
}
toJson() {
let self = this;
let metadataJson = {};
for (let key of self.metadata.keys()) {
metadataJson[key] = self.metadata.get(key);
}
let childrenJson = [];
for (let child of self.children.values()) {
childrenJson.push(child.toJson());
}
let breakdownJson = {
name: self.name,
type: self.type,
metadata: metadataJson,
measurement: self.measurement,
num_samples: self.numSamples,
children: childrenJson
};
return breakdownJson;
}
setType(typ) {
let self = this;
self.type = typ;
}
addMetadata(key, value) {
let self = this;
self.metadata.set(key, value);
}
getMetadata(key) {
let self = this;
return self.metadata.get(key);
}
findChild(name) {
let self = this;
return self.children.get(name);
}
addChild(child) {
let self = this;
self.children.set(child.name, child);
}
removeChild(child) {
let self = this;
self.children.delete(child.name);
}
findOrAddChild(name) {
let self = this;
let child = self.findChild(name);
if (!child) {
child = new Breakdown(self.agent, name);
self.addChild(child);
}
return child;
}
increment(value, count) {
let self = this;
self.measurement += value;
self.numSamples += count;
}
filter(fromLevel, min, max) {
let self = this;
self.filterLevel(1, fromLevel, min, max);
}
filterLevel(currentLevel, fromLevel, min, max) {
let self = this;
for (let name of self.children.keys()) {
let child = self.children.get(name);
if (currentLevel >= fromLevel && (child.measurement < min || child.measurement > max)) {
self.children.delete(name);
}
else {
child.filterLevel(currentLevel + 1, fromLevel, min, max);
}
}
}
depth() {
let self = this;
let max = 0;
for (let child of self.children.values()) {
let d = child.depth();
if (d > max) {
max = d;
}
}
return max + 1;
}
floor() {
let self = this;
self.measurement = Math.floor(self.measurement);
for (let child of self.children.values()) {
child.floor();
}
}
round() {
let self = this;
self.measurement = Math.round(self.measurement);
for (let child of self.children.values()) {
child.round();
}
}
propagate() {
let self = this;
for (let child of self.children.values()) {
child.propagate();
self.measurement += child.measurement;
self.numSamples += child.numSamples;
}
}
evaluatePercent(totalSamples) {
let self = this;
if (totalSamples > 0) {
self.measurement = (self.numSamples / totalSamples) * 100;
}
for (let child of self.children.values()) {
child.evaluatePercent(totalSamples);
}
}
convertToPercent(total) {
let self = this;
if (total > 0) {
self.measurement = (self.measurement / total) * 100;
}
for (let child of self.children.values()) {
child.convertToPercent(total);
}
}
normalize(factor) {
let self = this;
self.measurement = self.measurement / factor;
self.numSamples = Math.round(Math.ceil(self.numSamples / factor));
for (let child of self.children.values()) {
child.normalize(factor);
}
}
updateP95(value) {
let self = this;
if (!self.reservoir) {
self.reservoir = [];
}
if (self.reservoir.length < RESERVOIR_SIZE) {
self.reservoir.push(value);
}
else {
let index = Math.floor(Math.random() * RESERVOIR_SIZE);
self.reservoir[index] = value;
}
self.numSamples += 1;
}
evaluateP95() {
let self = this;
if (self.reservoir && self.reservoir.length > 0) {
self.reservoir.sort();
let index = Math.floor(self.reservoir.length * 0.95);
self.measurement = self.reservoir[index];
self.reservoir = null;
}
for (let child of self.children.values()) {
child.evaluateP95();
}
}
dump(level) {
let self = this;
return self.dumpLevel(0);
}
dumpLevel(level) {
let self = this;
let s = '';
for (let i = 0; i < level; i++) {
s += ' ';
}
s += `${self.name} - ${self.measurement} (${self.numSamples})\n`;
for (let child of self.children.values()) {
s += child.dumpLevel(level + 1);
}
return s;
}
}
exports.Breakdown = Breakdown;