express-status-monitor
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -0,23 +1,12 @@ | ||
/* eslint no-console: "off" */ | ||
const express = require('express'); | ||
const app = express(); | ||
const config = { | ||
path: '/', | ||
title: 'Express Status', | ||
spans: [{ | ||
interval: 1, | ||
retention: 60 | ||
}, { | ||
interval: 5, | ||
retention: 60 | ||
}, { | ||
interval: 15, | ||
retention: 60 | ||
}] | ||
} | ||
app.use(require('../index')({ path: '/' })); | ||
app.use(require('express-favicon-short-circuit')); | ||
app.use(require('../index')(config)); | ||
app.listen(3000, () => { | ||
console.log('Listening on http://0.0.0.0:3000'); | ||
console.log('listening on http://0.0.0.0:3000'); | ||
}); |
{ | ||
"name": "express-status-monitor-example", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Examples", | ||
@@ -17,6 +17,14 @@ "main": "index.js", | ||
"express": "^4.14.0", | ||
"express-favicon-short-circuit": "^1.1.0", | ||
"on-headers": "^1.0.1", | ||
"pidusage": "^1.0.4", | ||
"socket.io": "^1.4.8" | ||
}, | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/RafalWilinski/express-status-monitor/tree/master/examples" | ||
} | ||
} | ||
} |
89
index.js
@@ -1,86 +0,18 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
const onHeaders = require('on-headers'); | ||
const pidusage = require('pidusage'); | ||
const validate = require('./helpers/validate'); | ||
const gatherOsMetrics = require('./helpers/gather-os-metrics'); | ||
let io; | ||
const defaultConfig = { | ||
title: 'Express Status', | ||
path: '/status', | ||
spans: [{ | ||
interval: 1, | ||
retention: 60 | ||
}, { | ||
interval: 5, | ||
retention: 60 | ||
}, { | ||
interval: 15, | ||
retention: 60 | ||
}] | ||
}; | ||
const gatherOsMetrics = (io, span) => { | ||
const defaultResponse = { | ||
'2': 0, | ||
'3': 0, | ||
'4': 0, | ||
'5': 0, | ||
count: 0, | ||
mean: 0, | ||
timestamp: Date.now() | ||
}; | ||
pidusage.stat(process.pid, (err, stat) => { | ||
const last = span.responses[span.responses.length - 1]; | ||
// Convert from B to MB | ||
stat.memory = stat.memory / 1024 / 1024; | ||
stat.load = os.loadavg(); | ||
stat.timestamp = Date.now(); | ||
span.os.push(stat); | ||
if (!span.responses[0] || last.timestamp + (span.interval * 1000) < Date.now()) span.responses.push(defaultResponse); | ||
if (span.os.length >= span.retention) span.os.shift(); | ||
if (span.responses[0] && span.responses.length > span.retention) span.responses.shift(); | ||
sendMetrics(io, span); | ||
}); | ||
}; | ||
const sendMetrics = (io, span) => { | ||
io.emit('stats', { | ||
os: span.os[span.os.length - 2], | ||
responses: span.responses[span.responses.length - 2], | ||
interval: span.interval, | ||
retention: span.retention | ||
}); | ||
}; | ||
const middlewareWrapper = (config) => { | ||
if (config === null || config === undefined) { | ||
config = defaultConfig; | ||
} | ||
config = validate(config); | ||
if (config.path === undefined || !config.path instanceof String) { | ||
config.path = defaultConfig.path; | ||
} | ||
if (config.spans === undefined || !config.spans instanceof Array) { | ||
config.spans = defaultConfig.spans; | ||
} | ||
if (config.title === undefined || !config.title instanceof String) { | ||
config.title = 'Express Status'; | ||
} | ||
let renderedHtml; | ||
fs.readFile(path.join(__dirname, '/index.html'), function (err, html) { | ||
renderedHtml = html.toString() | ||
const renderedHtml = | ||
fs.readFileSync(path.join(__dirname, '/index.html')) | ||
.toString() | ||
.replace(/{{title}}/g, config.title) | ||
.replace(/{{script}}/g, fs.readFileSync(path.join(__dirname, '/app.js'))) | ||
.replace(/{{style}}/g, fs.readFileSync(path.join(__dirname, '/style.css'))); | ||
}); | ||
@@ -102,3 +34,4 @@ return (req, res, next) => { | ||
span.responses = []; | ||
setInterval(() => gatherOsMetrics(io, span), span.interval * 1000); | ||
const interval = setInterval(() => gatherOsMetrics(io, span), span.interval * 1000); | ||
interval.unref(); // don't keep node.js process up | ||
}); | ||
@@ -119,3 +52,3 @@ } | ||
if (last !== undefined && | ||
last.timestamp / 1000 + span.interval > Date.now() / 1000) { | ||
last.timestamp / 1000 + span.interval > Date.now() / 1000) { | ||
last[category]++; | ||
@@ -143,2 +76,2 @@ last.count++; | ||
module.exports = middlewareWrapper; | ||
module.exports = middlewareWrapper; |
{ | ||
"name": "express-status-monitor", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Realtime Monitoring for Express-based Node applications", | ||
@@ -13,2 +13,5 @@ "main": "index.js", | ||
], | ||
"engines" : { | ||
"node" : ">=4" | ||
}, | ||
"author": "Rafal Wilinski <raf.wilinski@gmail.com> (http://rwilinski.me)", | ||
@@ -25,2 +28,12 @@ "contributors": [ | ||
"url": "https://github.com/n1try/" | ||
}, | ||
{ | ||
"name": "Mattia Richetto", | ||
"email": "mattia.richetto@gmail.com", | ||
"url": "https://github.com/mattiaerre" | ||
}, | ||
{ | ||
"name": "Jiri Spac", | ||
"email": "capajj@gmail.com", | ||
"url": "https://github.com/capaj/" | ||
} | ||
@@ -37,3 +50,13 @@ ], | ||
"socket.io": "^1.4.8" | ||
}, | ||
"scripts": { | ||
"coverage": "istanbul cover _mocha test -- --recursive", | ||
"test": "mocha --recursive" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.0.2", | ||
"sinon": "^1.17.5" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
### Issues with HTTPS (including Heroku) has been resolved, module is working again | ||
# express-status-monitor | ||
@@ -19,5 +18,5 @@ Simple, self-hosted module based on Socket.io and Chart.js to report realtime server metrics for Express-based node servers. | ||
1. Go to `examples/` | ||
2. Run `npm install` | ||
3. Run server `node index.js` | ||
1. Go to `cd examples/` | ||
2. Run `npm i` | ||
3. Run server `npm start` | ||
4. Go to `http://0.0.0.0:3000` | ||
@@ -30,3 +29,3 @@ | ||
Default config: | ||
``` | ||
```javascript | ||
title: 'Express Status', // Default title | ||
@@ -47,4 +46,23 @@ path: '/status', | ||
## Securing endpoint | ||
Example using https://www.npmjs.com/package/connect-ensure-login | ||
```javascript | ||
const ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn() | ||
app.get('/status', ensureLoggedIn, require('express-status-monitor')()) | ||
``` | ||
Credits to [@mattiaerre](https://github.com/mattiaerre) | ||
## Tests and coverage | ||
In order to run test and coverage use the following npm commands: | ||
``` | ||
npm test | ||
npm run coverage | ||
``` | ||
## License | ||
[MIT License](https://opensource.org/licenses/MIT) © Rafal Wilinski |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22858
19
483
66
4