Monyt provides abstraction interfaces of monitoring and logging for Node.js Applications.
$ npm i -S monyt
monitor.js
import Monyt, {
RequestCountMetrics,
ErrorCountMetrics,
MemoryMetrics,
GarbageCollectionMetrics,
EventLoopLagMetrics,
GraphiteSender
} from 'monyt';
const interval = 60000; //default is 30000(ms)
const senders = [new GraphiteSender({
host: 'my.graphite.host.com',
port: '2003' //port of plaintext protocol
})];
const metricses = [
new RequestCountMetrics(),
new ErrorCountMetrics(),
new MemoryMetrics(),
new GarbageCollectionMetrics(),
new EventLoopLagMetrics()
];
const monitor = new Monyt({
interval,
prefix: `${application}.${hostname}.${clusterId}`, //This could be server hostname or application name or clusterId and etc.
senders,
metricses
});
export default monitor;
server.js
import Express from 'express';
import monitor from './monitor';
const logger = monitor.getLogger();
monitor.listen(results => {
results
.then(metricses=>logger.debug(metricses))
.catch(error=>logger.error(error));
});
const app = new Express();
app.use(monyt.middlewares());
app.use('/', (req, res, next) => {
logger.info('This is index.');
res.send('hello monyt!');
});
ProductBuyMetrics.js
import { Metrics } from 'monyt';
export default class ProductBuyMetrics extends Metrics {
constructor() {
super();
this.name = 'product.buy';
this.value = {}
}
buy(productId) {
this.value[productId] = this.value[productId] || 0;
this.value[productId] = this.value[productId] + 1;
}
}
MongoDBSender.js
import { Sender } from 'monyt';
export default class MongoDBSender extends Sender {
constructor(options = {}) {
super();
this.client = options.db.collection('metrics');
}
send(metrics) {
return new Promise((resolve, reject)=> {
this.client.insert(metrics, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
}
}
monitor.js
...
const productBuyMetrics = new ProductBuyMetrics()
const senders = [new MongoDBSender({db: mongodbClient});]
const metricses = [new ProductBuyMetrics()];
const monitor = new Monyt({
...
senders,
metricses,
...
});
export default monitor;
export productBuyMetrics;
your-app.js
import { productBuyMetrics } from './monitor';
app.post('/buy/:user/:productId', (req, res, next) => {
//...buying process...
productBuyMetrics.buy(req.params.productId);
res.send(buyResult);
});
This software is free to use under the Minkyu Cho. MIT license. See the LICENSE file for license text and copyright information.
Please don't hesitate to send a small pull-request or just leave anything you want as an issue.
- Fork it!
- Create your feature branch:
git checkout -b feature/my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin feature/my-new-feature
- Submit a pull request :D