Skip to content

Commit

Permalink
Add Node.js monitoring template
Browse files Browse the repository at this point in the history
  • Loading branch information
devmirek committed Mar 24, 2021
1 parent cb86c53 commit 45753b9
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 0 deletions.
35 changes: 35 additions & 0 deletions node_js/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** InfluxDB URL */
const INFLUX_URL = process.env.INFLUX_URL || 'http://localhost:8086'
/** InfluxDB authorization token */
const INFLUX_TOKEN = process.env.INFLUX_TOKEN || 'my-token'
/** Organization within InfluxDB */
const INFLUX_ORG = process.env.INFLUX_ORG || 'my-org'
/** InfluxDB bucket */
const INFLUX_BUCKET = 'iot_center'

// Defaults when on boarding a fresh new InfluxDB instance
/** InfluxDB user */
const onboarding_username = 'my-user'
/** InfluxDB password */
const onboarding_password = 'my-password'

/** recommended interval for client's to refresh configuration in seconds */
const configuration_refresh = 3600

function logEnvironment() {
console.log(`INFLUX_URL=${INFLUX_URL}`)
console.log(`INFLUX_TOKEN=${INFLUX_TOKEN ? '***' : ''}`)
console.log(`INFLUX_ORG=${INFLUX_ORG}`)
console.log(`INFLUX_BUCKET=${INFLUX_BUCKET}`)
}

module.exports = {
INFLUX_URL,
INFLUX_TOKEN,
INFLUX_ORG,
onboarding_username,
onboarding_password,
configuration_refresh,
INFLUX_BUCKET,
logEnvironment,
}
75 changes: 75 additions & 0 deletions node_js/monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const {InfluxDB, Point} = require('@influxdata/influxdb-client')
const {
INFLUX_URL: url,
INFLUX_TOKEN: token,
INFLUX_ORG: org,
INFLUX_BUCKET: bucket,
} = require('./env')
const responseTime = require('response-time')

// create Influx Write API to report application monitoring data
const writeAPI = new InfluxDB({url, token}).getWriteApi(org, bucket, 'ns', {
defaultTags: {
service: 'iot_center',
host: require('os').hostname(),
},
})
// write node resource/cpu/memory usage
function writeProcessUsage() {
function createPoint(measurement, usage) {
const point = new Point(measurement)
for (const key of Object.keys(usage)) {
point.floatField(key, usage[key])
}
return point
}

// https://nodejs.org/api/process.html#process_process_memoryusage
writeAPI.writePoint(createPoint('node_memory_usage', process.memoryUsage()))
// https://nodejs.org/api/process.html#process_process_cpuusage_previousvalue
writeAPI.writePoint(createPoint('node_cpu_usage', process.cpuUsage()))
// https://nodejs.org/api/process.html#process_process_resourceusage
writeAPI.writePoint(
createPoint('node_resource_usage', process.resourceUsage())
)
}
// write process usage now and then every 10 seconds
writeProcessUsage()
const nodeUsageTimer = setInterval(writeProcessUsage, 10_000).unref()

// on shutdown
// - clear reporting of node usage
// - flush unwritten points and cancel retries
async function onShutdown() {
clearInterval(nodeUsageTimer)
try {
await writeAPI.close()
} catch (error) {
console.error('ERROR: Application monitoring', error)
}
// eslint-disable-next-line no-process-exit
process.exit(0)
}
process.on('SIGINT', onShutdown)
process.on('SIGTERM', onShutdown)

// export a monitoring function for express.js response time monitoring
module.exports = function (app) {
app.use(
responseTime((req, res, time) => {
// print out request basics
console.info(
`${req.method} ${req.path} ${res.statusCode} ${
Math.round(time * 100) / 100
}ms`
)
// write response time to InfluxDB
const point = new Point('express_http_server')
.tag('uri', req.path)
.tag('method', req.method)
.tag('status', String(res.statusCode))
.floatField('response_time', time)
writeAPI.writePoint(point)
})
)
}
Binary file added node_js/node_dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 45753b9

Please sign in to comment.