forked from newrelic/node-native-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (147 loc) · 4.26 KB
/
index.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
/*
* Copyright 2021 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
var EventEmitter = require('events').EventEmitter
var util = require('util')
var preBuild = require('./lib/pre-build')
var natives = preBuild.load('native_metrics')
var DEFAULT_TIMEOUT = 15 * 1000 // 15 seconds
var GC_TYPE_NAMES = {
1: 'Scavenge',
2: 'MarkSweepCompact',
4: 'IncrementalMarking',
8: 'ProcessWeakCallbacks',
3: 'All', // Node v4 and earlier only have Scavenge and MarkSweepCompact.
15: 'All'
}
/**
* Constructs a metric emitter. This constructor is for internal use only.
*
* {@link NativeMetricEmitter#bind} is called as part of construction.
*
* @constructor
* @classdesc
* Emits events for various native events or periodic sampling.
*
* @param {number} [opts.timeout]
* The number of milliseconds between samplings. Defaults to 15 seconds.
*/
function NativeMetricEmitter(opts) {
opts = opts || { timeout: DEFAULT_TIMEOUT }
EventEmitter.call(this)
this.bound = false
this._timeout = null
this._gcBinder = new natives.GCBinder()
this.gcEnabled = true
this._loopChecker = new natives.LoopChecker()
this.loopEnabled = true
this.bind(opts.timeout)
}
util.inherits(NativeMetricEmitter, EventEmitter)
/**
* @interface LoopMetrics
*
* @description
* A mapping of loop concepts to metrics about them. All values are in
* microseconds.
*
* @property {Metric} usage - CPU usage per tick metrics.
*/
/**
* @interface GCMetrics
*
* @description
* Garbage collection results.
*
* @property {number} typeId - The numeric ID of the gc type.
* @property {string} type - The nice name version of the gc type.
* @property {Metric} metrics - Accumulated metric data in milliseconds.
*/
/**
* @interface Metric
*
* @description
* A bundle of values taken from some measurement.
*
* @property {number} total - The sum of all values measured.
* @property {number} min - The smallest value measured.
* @property {number} max - The largest value measured.
* @property {number} sumOfSquares - The sum of the square of each value.
* @property {number} count - The number of values measured.
*/
/**
* Binds the emitter to the internal, V8 hooks to start populating data.
*
* @fires NativeMetricEmitter#gc
* @fires NativeMetricEmitter#usage
*
* @param {number} [timeout]
* The number of milliseconds between samplings. Defaults to 15 seconds.
*/
NativeMetricEmitter.prototype.bind = function bind() {
if (this.bound) {
return
}
this._gcBinder.bind()
this._loopChecker.bind()
this.bound = true
}
/**
* Removes internal hooks and stops any open sampling timers.
*/
NativeMetricEmitter.prototype.unbind = function unbind() {
if (!this.bound) {
return
}
this._gcBinder.unbind()
this._loopChecker.unbind()
this.bound = false
}
/**
* Retrieves the current loop metrics and resets the counters.
*
* @return {LoopMetrics} The current loop metrics.
*/
NativeMetricEmitter.prototype.getLoopMetrics = function getLoopMetrics() {
return this._loopChecker.read()
}
/**
* Retrieves the accumulated garbage collection metrics.
*
* After retrieval, the metrics are reset internally. Only GC types that have
* happened at least once since the last retrieval are returned.
*
* @return {object.<string,GCMetrics>} An object mapping GC type names to
* information on the GC events that happened.
*/
NativeMetricEmitter.prototype.getGCMetrics = function getGCMetrics() {
var gcMetrics = this._gcBinder.read()
var results = Object.create(null)
for (var typeId in gcMetrics) {
if (gcMetrics.hasOwnProperty(typeId) && gcMetrics[typeId].count > 0) {
var typeName = GC_TYPE_NAMES[String(typeId)]
results[typeName] = {
typeId: parseInt(typeId, 10),
type: typeName,
metrics: gcMetrics[typeId]
}
}
}
return results
}
var emitter = null
/**
* Retrieves the {@link NativeMetricEmitter} singleton instance.
*
* @param {object} [opts]
* Options for constructing the emitter. See {@link NativeMetricEmitter} for
* default values. Only used on the first call to construct the instance.
*/
module.exports = function getMetricEmitter(opts) {
if (!emitter) {
emitter = new NativeMetricEmitter(opts)
}
return emitter
}