Skip to content

Commit

Permalink
feat: add debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
jmtavares committed Sep 7, 2018
1 parent acb9796 commit 212b46e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You can find here an usage example of the Statful Relay. In the following exampl

```json
{
"debug": false,
"listeners": {
"statful": {
"port": 2013,
Expand Down Expand Up @@ -124,11 +125,20 @@ Shows a small help for the collector.

### Configuration

In the configuration file you can find three main sections: `listeners, statfulClient and bunyan`.
In the configuration file you can find three main sections: `debug, listeners, statfulClient and bunyan`.

**Debug**
If enabled, Statful-Relay will expose an Http Server on port 9000 to download heapdumps.

| Route | Description | Type |
|:---|:---|
| /heapdump | Download heapdump. | `gzip` |
| /memory | Shows the memory in usage. | `string` |


**Listeners**

At the momment we only support one kind of listener called `statful`. However, here are the reference for listeners.
At the moment we only support one kind of listener called `statful`. However, here are the reference for listeners.

| Option | Description | Type | Default | Required |
|:---|:---|:---|:---|:---|
Expand Down
32 changes: 32 additions & 0 deletions lib/relay.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var StatfulListener = require('./listeners/statful');
var Logger = require('./logger');
var Utils = require('./utils');
var http = require('http');
var profiler = require('v8-profiler');
var zlib = require('zlib');

/**
* Constructor for the Statful Relay
Expand All @@ -22,8 +25,37 @@ Relay.prototype.start = function () {
this.statfulListener = new StatfulListener(this.config.listeners.statful, this.statful);
this.logger.info('Statful relay will start. All configured listeners will be created.');
this.statfulListener.start();

if (this.config.debug) {
http.createServer((req, res) => {
switch (req.url) {
case '/heapdump':
downloadHeapdump().then(data => {
res.write(data);
res.end();
});
break;
case '/memory':
var used = process.memoryUsage().heapUsed / 1024 / 1024;
res.write(used + 'MB');
res.end();
break;
}
}).listen(9000);
}
};

function downloadHeapdump () {
return new Promise(resolve => {
var snapshot1 = profiler.takeSnapshot();
snapshot1.export((error, result) => {
zlib.gzip(result, (err, buffer) => {
resolve(buffer);
});
});
});
}

/**
* Stops the Statful Relay
*/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "git",
"url": "https://github.com/statful/statful-relay.git"
},
"version": "1.1.5",
"version": "1.1.6",
"dependencies": {
"bluebird": "3.4.6",
"bunyan": "1.8.4",
Expand All @@ -22,6 +22,7 @@
"lodash": "4.16.6",
"pm2": "2.0.19",
"statful-client": "~5.0.0",
"v8-profiler": "^5.7.0",
"yargs": "6.3.0"
},
"engines": {
Expand Down

0 comments on commit 212b46e

Please sign in to comment.