forked from influxdata/community-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
551 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.