-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the officially recommended metrics (#25)
* Set to Number param instead of float * Add the officially recommended metrics
- Loading branch information
Showing
14 changed files
with
334 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
var processCpuTotal = require('./metrics/processCpuTotal'); | ||
var processStartTime = require('./metrics/processStartTime'); | ||
var osMemoryHeap = require('./metrics/osMemoryHeap'); | ||
var processOpenFileDescriptors = require('./metrics/processOpenFileDescriptors'); | ||
var processMaxFileDescriptors = require('./metrics/processMaxFileDescriptors'); | ||
|
||
var metrics = { | ||
processCpuTotal: processCpuTotal, | ||
processStartTime: processStartTime, | ||
osMemoryHeap: osMemoryHeap, | ||
processOpenFileDescriptors: processOpenFileDescriptors, | ||
processMaxFileDescriptors: processMaxFileDescriptors | ||
}; | ||
|
||
var existingInterval = null; | ||
|
||
module.exports = function startDefaultMetrics (disabledMetrics, interval) { | ||
if(existingInterval !== null) { | ||
clearInterval(existingInterval); | ||
} | ||
|
||
disabledMetrics = disabledMetrics || []; | ||
interval = interval || 10000; | ||
|
||
var metricsInUse = Object.keys(metrics) | ||
.filter(function (metric) { | ||
return disabledMetrics.indexOf(metric) < 0; | ||
}) | ||
.map(function (metric) { | ||
return metrics[metric](); | ||
}); | ||
|
||
function updateAllMetrics () { | ||
metricsInUse.forEach(function (metric) { | ||
metric.call(); | ||
}); | ||
} | ||
|
||
updateAllMetrics(); | ||
|
||
existingInterval = setInterval(updateAllMetrics, interval).unref(); | ||
|
||
return existingInterval; | ||
}; | ||
|
||
module.exports.metricsList = Object.keys(metrics); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
var linuxVariant = require('./osMemoryHeapLinux'); | ||
var notLinuxVariant = function () { | ||
var residentMemGauge = new Gauge('process_resident_memory_bytes', 'Resident memory size in bytes.'); | ||
|
||
return function () { | ||
var memoryUsage = process.memoryUsage(); | ||
|
||
// I don't think the other things returned from `process.memoryUsage()` is relevant to a standard export | ||
residentMemGauge.set(null, memoryUsage.rss); | ||
}; | ||
}; | ||
|
||
module.exports = function () { | ||
return process.platform === 'linux' ? linuxVariant() : notLinuxVariant(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
var fs = require('fs'); | ||
|
||
var values = ['VmSize', 'VmRSS', 'VmData']; | ||
|
||
function structureOutput (input) { | ||
var returnValue = {}; | ||
|
||
input.split('\n') | ||
.filter(function (s) { | ||
return values.some(function (value) { | ||
return s.indexOf(value) === 0; | ||
}); | ||
}) | ||
.forEach(function (string) { | ||
var split = string.split(':'); | ||
|
||
// Get the value | ||
var value = split[1].trim(); | ||
// Remove trailing ` kb` | ||
value = value.substr(0, value.length - 3); | ||
// Make it into a number in bytes bytes | ||
value = Number(value) * 1000; | ||
|
||
returnValue[split[0]] = value; | ||
}); | ||
|
||
return returnValue; | ||
} | ||
|
||
module.exports = function () { | ||
var residentMemGauge = new Gauge('process_resident_memory_bytes', 'Resident memory size in bytes.'); | ||
var virtualMemGauge = new Gauge('process_virtual_memory_bytes', 'Virtual memory size in bytes.'); | ||
var heapSizeMemGauge = new Gauge('process_heap_bytes', 'Process heap size in bytes.'); | ||
|
||
return function () { | ||
fs.readFile('/proc/self/status', 'utf8', function (err, status) { | ||
if(err) { | ||
return; | ||
} | ||
var structuredOutput = structureOutput(status); | ||
|
||
residentMemGauge.set(null, structuredOutput.VmRSS); | ||
virtualMemGauge.set(null, structuredOutput.VmSize); | ||
heapSizeMemGauge.set(null, structuredOutput.VmData); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
|
||
module.exports = function () { | ||
// Don't do anything if the function doesn't exist (introduced in node@6.1.0) | ||
if(typeof process.cpuUsage !== 'function') { | ||
return function () { | ||
}; | ||
} | ||
|
||
var cpuUserGauge = new Gauge('process_cpu_seconds_total', 'Total user and system CPU time spent in seconds.'); | ||
|
||
return function () { | ||
var cpuUsage = process.cpuUsage(); | ||
var totalUsageMicros = cpuUsage.user + cpuUsage.system; | ||
|
||
cpuUserGauge.set(null, totalUsageMicros / 1e6); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
var fs = require('fs'); | ||
|
||
module.exports = function () { | ||
var isSet = false; | ||
|
||
return function () { | ||
if(isSet || process.platform !== 'linux') { | ||
return; | ||
} | ||
|
||
fs.readFile('/proc/sys/fs/file-max', 'utf8', function (err, maxFds) { | ||
if(err) { | ||
return; | ||
} | ||
|
||
isSet = true; | ||
|
||
var fileDescriptorsGauge = new Gauge('process_max_fds', 'Maximum number of open file descriptors.'); | ||
|
||
fileDescriptorsGauge.set(null, maxFds); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
var fs = require('fs'); | ||
|
||
module.exports = function () { | ||
if(process !== 'linux') { | ||
return function () { | ||
}; | ||
} | ||
|
||
var fileDescriptorsGauge = new Gauge('process_open_fds', 'Number of open file descriptors.'); | ||
|
||
return function () { | ||
fs.readdir('/proc/self/fd', function (err, list) { | ||
if(err) { | ||
return; | ||
} | ||
|
||
// Minus 1, as this invocation created one | ||
fileDescriptorsGauge.set(null, list.length - 1); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
var Gauge = require('../gauge'); | ||
var nowInSeconds = Math.round(Date.now() / 1000 - process.uptime()); | ||
|
||
module.exports = function () { | ||
var cpuUserGauge = new Gauge('process_start_time_seconds', 'Start time of the process since unix epoch in seconds.'); | ||
var isSet = false; | ||
|
||
return function () { | ||
if(isSet) { | ||
return; | ||
} | ||
cpuUserGauge.set(null, nowInSeconds); | ||
isSet = true; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.