Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process metrics #21

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/childProcess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const spmAgent = require('../lib/index.js') // or 'spm-agent-nodejs'

spmAgent.on('stats', function (stats) {
// console.log(stats)
})
Expand Down
3 changes: 1 addition & 2 deletions lib/eventLoopAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ module.exports = function () {
start: function (agent) {
this.elListener = function (stats) {
const metric = {
ts: Date.now(),
timestamp: new Date(),
measurement: 'nodejs.eventloop',
tags: {},
fields: {
count: stats.count,
time: stats.time,
'latency.min': stats.min,
'latency.max': stats.max,
'latency.max.avg': stats.avg
Expand Down
3 changes: 1 addition & 2 deletions lib/gcAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function getGcStats () {
})

const timerId = setInterval(function () {
const now = new Date().getTime()
const heapInfo = lastHeapInfo
const heapCompactionsJSON = heapCompactions.toJSON()
const metricValues = {
Expand All @@ -63,7 +62,7 @@ function getGcStats () {
'heap.size': heapInfo.processHeapTotal || 0,
'memory.rss': heapInfo.processMemoryRss || 0
}
agent.addMetrics({ ts: now, measurement: 'nodejs', tags: {}, fields: metricValues })
agent.addMetrics({timestamp: new Date(), measurement: 'nodejs', tags: {}, fields: metricValues })
fullGc.reset(0)
incGc.reset(0)
heapCompactions.reset(0)
Expand Down
4 changes: 2 additions & 2 deletions lib/httpServerAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function httpServerAgent () {
const stats = ctx.stats
const httpStats = stats.toJSON()
const responseTimes = histogram.toJSON()
const now = Date.now()
const now = new Date()
const metricValue = {
requests: httpStats.requests ? httpStats.requests.count : 0, // http.requestCount (int)
errors: httpStats.errRate ? httpStats.errRate.count : 0, // http.errorCount (int)
Expand All @@ -66,7 +66,7 @@ module.exports = function httpServerAgent () {
}

if (metricValue.requests > 0 || metricValue.errors > 0) {
agent.addMetrics({ ts: now, measurement: 'nodejs', tags: {}, fields: metricValue })
agent.addMetrics({ timestamp: now, measurement: 'nodejs', tags: {}, fields: metricValue })
}

// cleanup aggregate stats
Expand Down
66 changes: 55 additions & 11 deletions lib/processAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,63 @@
* Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
*/
'use strict'

const { Agent, Config } = require('spm-agent')

function getProcessInfo () {
const { uptime, pid, ppid, send } = process
const processType = send === undefined ? 'master' : 'child_process'
const processInfo = {
uptime: uptime(),
pid,
ppid,
processType
const pidusageTree = require('pidusage')
function getPsTreeMetrics (agent) {
if (process.send !== undefined) {
// child process, not collecting stats
return
}
return processInfo
console.log('process, send:', process.send)
console.log('NODE_ID', process.env.NODE_UNIQUE_ID)
pidusageTree(process.pid, function (err, results) {
if (err) {
return console.error(err)
}
const pids = Object.keys(results)
for (let i = 0; i < pids.length; i++) {
let processType = 'child_process'
if (pids[i] === String(process.pid)) {
processType = 'main_process'
}
const proc = results[pids[i]]
const processMetric = {
timestamp: new Date(),
measurement: 'nodejs.process',
tags: {
'nodejs.process.type': processType
},
fields: {
cpuPercent: proc.cpu,
memory: proc.memory,
uptime: proc.elapsed
}
}
agent.addMetrics(processMetric)
}
const processCountMetric = {
timestamp: new Date(),
measurement: 'nodejs.process',
tags: {
'nodejs.process.type': 'main_process'
},
fields: {
mainProcessCount: 1
}
}
agent.addMetrics(processCountMetric)
const processChildCountMetric = {
timestamp: new Date(),
measurement: 'nodejs.process',
tags: {
'nodejs.process.type': 'child_process'
},
fields: {
childProcessCount: Math.max(pids.length - 1, 0) || 0
}
}
agent.addMetrics(processChildCountMetric)
})
}

function getProcessStats () {
Expand Down
4 changes: 2 additions & 2 deletions lib/workerAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function getWorkers () {
return {
start: function (agent) {
const timerId = setInterval(function () {
const now = new Date().getTime()
const now = new Date()
if (cluster.isMaster || process.env.NODE_APP_INSTANCE === 0 || process.env.SPM_MASTER_MODE === '1' || process.env.STARTUP === 'true') {
agent.addMetrics({
ts: now,
timestamp: now,
measurement: 'nodejs',
tags: {},
fields: { workers: Object.keys(cluster.workers || {}).length || 1 }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"measured-core": "^1.41.0",
"request": "^2.88.0",
"spm-agent": "^2.0.0",
"spm-agent": "file:../spm-agent",
"spm-agent-os": "^1.30.13",
"zip-zip-top": "^0.2.0"
},
Expand Down
15 changes: 8 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ describe('SPM for Node.js tests', function () {
let metricCounter = 0

function checkMetrics (metric) {
const { uptime, processes } = metric.fields

if (uptime) {
metricCounter++
}
if (processes) {
metricCounter++
const { uptime, mainProcessCount } = metric.fields
if (metric.measurement === 'nodejs.process') {
if (uptime) {
metricCounter++
}
if (mainProcessCount) {
metricCounter++
}
}

if (metricCounter > 1) {
Expand Down