-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (56 loc) · 1.99 KB
/
index.js
File metadata and controls
68 lines (56 loc) · 1.99 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
'use strict'
const singleMetric = require('@telemetry-js/metric').single
const match = require('@telemetry-js/match-metric-names')
const EventEmitter = require('events').EventEmitter
const os = require('os')
const ALL_METRICS = [
'telemetry.osmem.free.percent',
'telemetry.osmem.free.bytes',
'telemetry.osmem.used.percent',
'telemetry.osmem.used.bytes',
'telemetry.osmem.total.bytes'
]
module.exports = function (options) {
return new MemCollector(options)
}
class MemCollector extends EventEmitter {
constructor (options) {
if (!options) options = {}
super()
this._metrics = new Set(match(ALL_METRICS, options.metrics))
this._percentOptions = { unit: 'percent' }
this._byteOptions = { unit: 'bytes' }
}
// TODO: reuse metric objects between pings
ping (callback) {
const free = os.freemem()
const total = os.totalmem()
if (this._metrics.has('telemetry.osmem.used.percent')) {
const metric = singleMetric('telemetry.osmem.used.percent', this._percentOptions)
metric.record((total - free) / total * 100)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.osmem.free.percent')) {
const metric = singleMetric('telemetry.osmem.free.percent', this._percentOptions)
metric.record(free / total * 100)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.osmem.used.bytes')) {
const metric = singleMetric('telemetry.osmem.used.bytes', this._byteOptions)
metric.record(total - free)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.osmem.free.bytes')) {
const metric = singleMetric('telemetry.osmem.free.bytes', this._byteOptions)
metric.record(free)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.osmem.total.bytes')) {
const metric = singleMetric('telemetry.osmem.total.bytes', this._byteOptions)
metric.record(total)
this.emit('metric', metric)
}
// No need to dezalgo ping()
callback()
}
}