Skip to content

Commit

Permalink
added process tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Adnan Rahic committed Jan 13, 2021
1 parent 388c807 commit 591e9ee
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ function Agent (plugin) {
var eventEmitter = new events.EventEmitter()
var cluster = require('cluster')
var workerId = 0 + '-' + process.pid // 0 == Master, default
var config = require('./util/spmconfig.js')
require('./util/docker.js')
require('./util/kubernetes.js')
var config = require('./util/spmconfig')
require('./util/docker')
require('./util/kubernetes')
require('./util/process')

if (!cluster.isMaster) {
workerId = cluster.worker.id + '-' + process.pid
Expand Down Expand Up @@ -114,11 +115,19 @@ function Agent (plugin) {
if (!metric.tags['nodejs.worker.id']) {
metric.tags['nodejs.worker.id'] = workerId
}

const processTags = JSON.parse(process.env.processTags)
if (!metric.tags['process.pid']) {
metric.tags['process.pid'] = process.pid
metric.tags['process.pid'] = processTags.process.pid
}
if (!metric.tags['process.ppid']) {
metric.tags['process.ppid'] = process.ppid
metric.tags['process.ppid'] = processTags.process.ppid
}
if (!metric.tags['process.name']) {
metric.tags['process.name'] = processTags.process.name
}
if (!metric.tags['process.type']) {
metric.tags['process.type'] = processTags.process.type
}

// Set container tags only for K8s metrics if running in K8s
Expand Down
48 changes: 48 additions & 0 deletions lib/util/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const pidUsageTree = require('pidusage-tree')
const util = require('util')
const pidUsageTreePromise = util.promisify(pidUsageTree)
const pm2Enabled = process.env.NODE_APP_INSTANCE !== undefined
const isChildProcess = process.send !== undefined
const isMasterProcess = process.send === undefined

const setProcessTags = async ({ pidToCheck, type }) => {
const results = await pidUsageTreePromise(pidToCheck)
const processes = Object.keys(results)
.map(key => results[key])
.filter(proc => proc)

const resProc = processes.filter(proc => proc.pid === pidToCheck).pop()

const processTags = {
process: {
type,
name: 'node',
pid: resProc.pid,
ppip: resProc.ppid
}
}

process.env.processTags = JSON.stringify(processTags)
}

const setDefaultProcessTags = async () =>
isMasterProcess
? await setProcessTags({ pidToCheck: process.pid, type: 'master' })
: await setProcessTags({ pidToCheck: process.pid, type: 'child' })

const setPm2ProcessTags = async () =>
isChildProcess
? await setProcessTags({ pidToCheck: process.ppid, type: 'pm2' })
: null

;(async () => {
try {
if (pm2Enabled) {
await setPm2ProcessTags()
} else {
await setDefaultProcessTags()
}
} catch (error) {
console.log(error)
}
})()
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"kubernetes-client": "^9.0.0",
"moment": "^2.24.0",
"nedb": "^1.8.0",
"pidusage-tree": "^2.0.5",
"rc-yaml-2": "^1.0.2",
"request": "^2.88.0",
"winston": "^2.4.4"
Expand Down

0 comments on commit 591e9ee

Please sign in to comment.