Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Merge 1e70134 into 123bdab
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Dec 29, 2016
2 parents 123bdab + 1e70134 commit 08684b7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="1.1.0-0"></a>
# 1.1.0-0 (2016-12-29)


### Bug Fixes

* whoops, had typoe in JSON ([869d461](https://github.com/npm/micro-monitor/commit/869d461))


### Features

* allow the key on an arbitrary object to be monitored ([e5c8754](https://github.com/npm/micro-monitor/commit/e5c8754))
* pull [@ceejbot](https://github.com/ceejbot)'s code into a module ([b0c57aa](https://github.com/npm/micro-monitor/commit/b0c57aa))
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ HTTP interface.
Simply initialize the monitor when your application starts up:

```js
const monitor = require('micro-monitor')
const Monitor = require('micro-monitor')

monitor(9999, () => {
let monitor = Monitor(9999, () => {
// do something now that monitoring is running
})
```
Expand Down Expand Up @@ -43,6 +43,17 @@ monitor(9999, () => {
* `http://0.0.0.1:9999/_monitor/ping` is also available and will respond with
status `200` and the text `OK`.

## Customizing Status Information

You can customize the status information returned, by configuring
`micro-monitor` to monitor a key on an arbitrary object:

* **monitor.monitorKey(obj, key)**: the status object returned by
`/_monitor/status` will be populated with a `key` equal to the
value of the given `key` on `obj`.
* **monitor.stopMonitoringKey(obj, key)**: stop monitoring the
key on an object.

## License

ISC
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const restify = require('restify')
const exec = require('child_process').exec

const monitoredObjects = []

module.exports = function startMonitor (port, callback) {
const monitor = restify.createServer({name: 'monitor'})
let commitHash = ''
Expand All @@ -18,6 +20,9 @@ module.exports = function startMonitor (port, callback) {
cmdline: process.argv,
git: commitHash
}
monitoredObjects.forEach((mo) => {
status[mo.key] = mo.obj[mo.key]
})
response.json(200, status)
next()
})
Expand All @@ -26,4 +31,19 @@ module.exports = function startMonitor (port, callback) {
if (!err && stdout) commitHash = stdout.trim()
monitor.listen(port, callback)
})

return {
monitorKey: (obj, key) => {
monitoredObjects.push({
key: key,
obj: obj
})
},
stopMonitoringKey: (obj, key) => {
const element = monitoredObjects.find((e) => {
return e.key === key && e.obj === obj
})
monitoredObjects.splice(monitoredObjects.indexOf(element), 1)
}
}
}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "micro-monitor",
"version": "1.0.0",
"version": "1.1.0-0",
"description": "add a monitoring endpoint to your node background services",
"main": "index.js",
"scripts": {
"pretest": "standard",
"test": "nyc mocha test.js",
"coverage": "nyc --reporter=text-lcov mocha | coveralls"
"coverage": "nyc --reporter=text-lcov mocha | coveralls",
"release": "standard-version"
},
"repository": {
"type": "git",
Expand All @@ -28,7 +29,8 @@
"mocha": "^3.2.0",
"nyc": "^10.0.0",
"request": "^2.79.0",
"standard": "^8.6.0"
"standard": "^8.6.0",
"standard-version": "^4.0.0"
},
"dependencies": {
"restify": "^4.3.0"
Expand Down
60 changes: 58 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

require('chai').should()

const monitor = require('./')
const expect = require('chai').expect
const Monitor = require('./')
const request = require('request')

describe('micro-monitor', () => {
before((done) => monitor(9999, done))
let monitor = null
before((done) => { monitor = Monitor(9999, done) })

it('responds with status information', (done) => {
request.get({
Expand All @@ -32,4 +34,58 @@ describe('micro-monitor', () => {
return done()
})
})

describe('monitorKey', () => {
it('allows an arbitrary key on an object to be monitored', (done) => {
const obj = {batman: 'rich but grumpy'}
monitor.monitorKey(obj, 'batman')

request.get({
url: 'http://127.0.0.1:9999/_monitor/status',
json: true
}, (err, res, response) => {
if (err) return done(err)
monitor.stopMonitoringKey(obj, 'batman')
res.statusCode.should.equal(200)
response.batman.should.equal('rich but grumpy')
return done()
})
})

it('should report new value if original object is updated', (done) => {
const obj = {batman: 'rich but grumpy'}
monitor.monitorKey(obj, 'batman')
obj.batman = 'Caped Crusader'

request.get({
url: 'http://127.0.0.1:9999/_monitor/status',
json: true
}, (err, res, response) => {
if (err) return done(err)
monitor.stopMonitoringKey(obj, 'batman')
res.statusCode.should.equal(200)
response.batman.should.equal('Caped Crusader')
return done()
})
})
})

describe('stopMonitoringKey', () => {
it('stops monitoring a given key on an object', (done) => {
const obj = {batman: 'rich but grumpy'}
monitor.monitorKey(obj, 'batman')
monitor.stopMonitoringKey(obj, 'batman')

request.get({
url: 'http://127.0.0.1:9999/_monitor/status',
json: true
}, (err, res, response) => {
if (err) return done(err)
monitor.stopMonitoringKey(obj, 'batman')
res.statusCode.should.equal(200)
expect(response.batman).to.equal(undefined)
return done()
})
})
})
})

0 comments on commit 08684b7

Please sign in to comment.