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

add process.thread.count metric #26

Merged
merged 4 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
47 changes: 45 additions & 2 deletions lib/processAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,50 @@
'use strict'
const { Agent, Config } = require('spm-agent')
const pidUsageTree = require('pidusage-tree')
const fs = require('fs')
const execProcess = require('child_process').spawnSync
const pm2Enabled = /^true/i.test(process.env.PM2)
const infraToken = Config.tokens.infra

const getThreadCount = function (processId) {
// thread count meric
var threadCount = 0
if (/linux/i.test(process.platform)) {
try {
const pidStatus = String(fs.readFileSync(`/proc/${processId}/status`))
const threadCountLine = pidStatus.match(/Threads:\s(\d+)/)
if (threadCountLine && threadCountLine.length > 1) {
threadCount = Number(threadCountLine[1])
}
} catch (err) {
// console.error(err)
}
}
if (/win/i.test(process.platform)) {
try {
var child = execProcess('powershell.exe', ['-Command', 'Get-CimInstance', '-Class Win32_Thread', '-Filter', `ProcessHandle = ${processId}`], { env: process.env })
var tc = Number(String(child.stdout))
if (tc) {
threadCount = tc
}
} catch (winErr) {
console.error(winErr)
}
}
if (/darwin/i.test(process.platform)) {
try {
const child = execProcess('/bin/ps', ['M', `${processId}`], { env: process.env })
const darwinTc = (String(child.stdout) || '').split('\n').length
if (darwinTc > 0) {
threadCount = darwinTc - 1 // remove headline
}
} catch (darwinErr) {
// console.error(darwinErr)
}
}
return threadCount
}

const addMetrics = function (agent, pidToCheck) {
const timestamp = new Date()
pidUsageTree(pidToCheck, function (err, results) {
Expand All @@ -36,7 +77,8 @@ const addMetrics = function (agent, pidToCheck) {
fields: {
'cpu.percent': masterProcess.cpu,
memory: masterProcess.memory,
uptime: masterProcess.elapsed
uptime: masterProcess.elapsed,
'thread.count': getThreadCount(pidToCheck)
}
}
agent.addMetrics(masterProcessMetric)
Expand All @@ -54,7 +96,8 @@ const addMetrics = function (agent, pidToCheck) {
fields: {
'cpu.percent': proc.cpu,
memory: proc.memory,
uptime: proc.elapsed
uptime: proc.elapsed,
'thread.count': getThreadCount(proc.id)
}
}
agent.addMetrics(childProcessMetric)
Expand Down
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ describe('SPM for Node.js tests', function () {
if (metric.measurement && metric.measurement.indexOf('process') > -1 &&
metric.fields.uptime &&
metric.fields.memory &&
metric.fields['cpu.percent']) {
metric.fields['cpu.percent'] &&
metric.fields['thread.count']) {
if (metric.tags.token !== config.tokens.infra) {
console.log(metric)
done(new Error(`No infra token set ${metric.tags.token} != ${config.tokens.infra}`))
errorReported = true
}
console.log(metric)
metricCounter = metricCounter + 1
}
if (metric.measurement && metric.measurement.indexOf('process') > -1 && metric.fields.count) {
Expand All @@ -202,7 +203,7 @@ describe('SPM for Node.js tests', function () {
metricCounter = metricCounter + 1
}

if (metricCounter > 1) {
if (metricCounter > 2) {
agent.removeListener('metric', checkMetrics)
agent.stop()
done()
Expand Down