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
0 parents
commit ecce4ce
Showing
8 changed files
with
244 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 @@ | ||
node_modules |
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,130 @@ | ||
Stats Server for Node Applications | ||
================================== | ||
|
||
Create a /stats page http-server for a server application | ||
|
||
Usage | ||
----- | ||
|
||
``` js | ||
var stats_page = require('stats-page'); | ||
stats_page.start(port[, host]); | ||
``` | ||
|
||
Example | ||
------- | ||
|
||
To create a stats server for your application use the following code | ||
|
||
``` js | ||
var stats_page = require('stats-page'); | ||
stats_page.start(8745); | ||
``` | ||
|
||
This will fire up a stats server on port 8745 that you can query | ||
|
||
**NOTE:** this will listen on `0.0.0.0` by default (as does `http.createServer`), | ||
you can specify a second argument to the `start` method of `localhost` to restrict | ||
access. | ||
|
||
$ curl -sS localhost:8745/ | json | ||
|
||
``` json | ||
[ | ||
"process" | ||
] | ||
``` | ||
|
||
This returns the next route you can query | ||
|
||
$ curl -sS localhost:8745/process | json | ||
|
||
``` json | ||
[ | ||
"argv", | ||
"cwd", | ||
"env", | ||
"execPath", | ||
"features", | ||
"getgid", | ||
"getuid", | ||
"memoryUsage", | ||
"pid", | ||
"platform", | ||
"uptime", | ||
"uvCounters", | ||
"version", | ||
"versions" | ||
] | ||
``` | ||
|
||
Then drill in further to get some meaningful information | ||
|
||
$ curl -sS localhost:8745/process/argv | json | ||
|
||
``` json | ||
[ | ||
"node", | ||
"/Users/dave/dev/node-stats-page/examples/simple.js" | ||
] | ||
``` | ||
|
||
$ curl -sS localhost:8745/process/memoryUsage | json | ||
|
||
``` json | ||
{ | ||
"rss": 14368768, | ||
"heapTotal": 5343936, | ||
"heapUsed": 2799240 | ||
} | ||
``` | ||
|
||
$ curl -sS localhost:8745/process/uptime | json | ||
32 | ||
$ curl -sS localhost:8745/process/version | json | ||
v0.6.15 | ||
|
||
This provides a lot of information you can gather about your app while it | ||
is running. You can use this for alerting, graphing, etc. | ||
|
||
To disable the server for whatever reason, you simply invoke the `close` method | ||
|
||
``` js | ||
stats_page.close(); | ||
``` | ||
|
||
Customization | ||
------------- | ||
|
||
You will no doubt have information specific to your app that you would like to | ||
expose. You can add your own routes using the `add\_route` function | ||
|
||
``` js | ||
stats_page.add_route('/custom', function(cb) { | ||
cb(null, 'My Custom Route'); | ||
}); | ||
``` | ||
|
||
then when you query | ||
|
||
curl -sS localhost:8745/custom | json | ||
My Custom Route | ||
|
||
you will get your data | ||
|
||
The first argument is the url path, and the second argument is a function to run | ||
when the url is queried. The function takes one argument, a callback to run | ||
with a possible error, and data to send to the user (it will be JSON.stringifed | ||
for you). | ||
|
||
|
||
Install | ||
------- | ||
|
||
npm install stats-page | ||
|
||
|
||
License | ||
------- | ||
|
||
MIT Licensed |
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,5 @@ | ||
var stats_page = require('..'); | ||
|
||
stats_page.add_route('/custom', function(cb) { cb(null, 'My Custom Route'); }); | ||
|
||
stats_page.start(8745, 'localhost'); |
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,2 @@ | ||
var stats_page = require('..'); | ||
stats_page.start(8745, 'localhost'); |
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,50 @@ | ||
var http = require('http'), | ||
route = require('./routes'), | ||
custom_routes = {}, | ||
server; | ||
|
||
module.exports.start = start; | ||
module.exports.stop = stop; | ||
module.exports.add_route = add_route; | ||
|
||
/** | ||
* Start the webserver on a given host and port | ||
*/ | ||
function start(port, host) { | ||
port = port || 8745; | ||
if (!server) server = http.createServer(on_request).listen(port, host); | ||
} | ||
|
||
/** | ||
* Stop the webserver | ||
*/ | ||
function stop() { | ||
if (server) server.close(); | ||
server = undefined; | ||
} | ||
|
||
/** | ||
* Add a custom route | ||
*/ | ||
function add_route(url, func) { | ||
custom_routes[url] = func; | ||
} | ||
|
||
/** | ||
* Request callback for the server | ||
*/ | ||
function on_request(req, res) { | ||
var func = custom_routes[req.url] || route(req.url); | ||
|
||
if (typeof func !== 'function') | ||
return res.end(JSON.stringify({'error': 'Route not found'})); | ||
|
||
func(function(err, out) { | ||
if (err) { | ||
res.write(JSON.stringify({'error':err})); | ||
} else { | ||
res.write(JSON.stringify(out)); | ||
} | ||
res.end(); | ||
}); | ||
} |
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,18 @@ | ||
{ | ||
"name": "stats-page", | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
"description": "Create a /stats page http-server for a server application", | ||
"version": "0.0.0", | ||
"repository": { | ||
"url": "https://github.com/bahamas10/node-stats-page.git", | ||
"type": "git" | ||
}, | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"keywords": [ "stats", "statistics", "alerting", "alerts", "uptime" ] | ||
} |
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,17 @@ | ||
var routes = { | ||
'process': require('./process') | ||
}; | ||
|
||
|
||
module.exports = function(url) { | ||
var parts = url.split('/').slice(1), | ||
route = routes[parts[0]]; | ||
|
||
// Index page | ||
if (!parts[0]) return function(cb) { cb(null, Object.keys(routes)); }; | ||
|
||
// Not routable | ||
if (!route) return undefined; | ||
|
||
return route(parts.slice(1)); | ||
}; |
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,21 @@ | ||
var routes = { | ||
'argv' : function(cb) { cb(null, process.argv); }, | ||
'cwd' : function(cb) { cb(null, process.cwd()); }, | ||
'env' : function(cb) { cb(null, process.env); }, | ||
'execPath' : function(cb) { cb(null, process.execPath); }, | ||
'features' : function(cb) { cb(null, process.features); }, | ||
'getgid' : function(cb) { cb(null, process.getgid()); }, | ||
'getuid' : function(cb) { cb(null, process.getuid()); }, | ||
'memoryUsage' : function(cb) { cb(null, process.memoryUsage()); }, | ||
'pid' : function(cb) { cb(null, process.pid); }, | ||
'platform' : function(cb) { cb(null, process.platform); }, | ||
'uptime' : function(cb) { cb(null, process.uptime()); }, | ||
'uvCounters' : function(cb) { cb(null, process.uvCounters()); }, | ||
'version' : function(cb) { cb(null, process.version); }, | ||
'versions' : function(cb) { cb(null, process.versions); }, | ||
}; | ||
|
||
module.exports = function(parts) { | ||
if (!parts[0]) return function(cb) { cb(null, Object.keys(routes)); }; | ||
return routes[parts[0]]; | ||
}; |