Skip to content

Commit

Permalink
Merge pull request #26 from sematext/add_thread_count_metric
Browse files Browse the repository at this point in the history
add process.thread.count metric
  • Loading branch information
Adnan Rahić authored Jan 20, 2020
2 parents 404bfc1 + 4322d6b commit 9bcbfd1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
43 changes: 43 additions & 0 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 @@ -34,6 +75,7 @@ const addMetrics = function (agent, pidToCheck) {
'process.type': 'master'
},
fields: {
'thread.count': getThreadCount(pidToCheck),
'cpu.usage': masterProcess.cpu,
rss: masterProcess.memory,
uptime: masterProcess.elapsed
Expand All @@ -52,6 +94,7 @@ const addMetrics = function (agent, pidToCheck) {
'process.type': 'child'
},
fields: {
'thread.count': getThreadCount(proc.id),
'cpu.usage': proc.cpu,
rss: proc.memory,
uptime: proc.elapsed
Expand Down
9 changes: 4 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,24 @@ describe('SPM for Node.js tests', function () {
}
if (metric.measurement && metric.measurement.indexOf('process') > -1 &&
metric.fields.uptime &&
metric.fields.memory &&
metric.fields['cpu.usage']) {
metric.fields.rss &&
metric.fields['cpu.usage'] &&
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
}
metricCounter = metricCounter + 1
}
if (metric.measurement && metric.measurement.indexOf('process') > -1 && metric.fields.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
}
metricCounter = metricCounter + 1
}

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

0 comments on commit 9bcbfd1

Please sign in to comment.