Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.

Commit 8605e4c

Browse files
committed
Opt-in hooks and signal by default
This changes the default action on require('nodereport') to opt-in to the exception and fatal-error hooks, and to enable the signal handler. So 'node -r nodereport application.js' will get all the nodereport functionality without any application code changes. For users programming to the API, a separate api-only entry point, require('nodereport/api') is provided. This does not enable the hooks and signal unless and until .setEvents() is called. PR-URL: #33 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 67050f5 commit 8605e4c

9 files changed

Lines changed: 58 additions & 17 deletions

File tree

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*
22
!src/**
33
!binding.gyp
4+
!index.js

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@ Supports Node.js v4, v6 and v7 on Linux, MacOS, Windows and AIX.
1515

1616
```bash
1717
npm install nodereport
18+
node -r nodereport app.js
1819
```
20+
A NodeReport will be triggered automatically on unhandled exceptions and fatal
21+
error events (for example out of memory errors), and can also be triggered
22+
by sending a USR2 signal to a Node.js process (Linux/MacOS only).
1923

20-
This will allow a NodeReport to be triggered via an API
21-
call from a JavaScript application.
24+
A NodeReport can also be triggered via an API call from a JavaScript
25+
application.
2226

2327
```js
2428
var nodereport = require('nodereport');
2529
nodereport.triggerReport();
2630
```
31+
The API can be used without adding the automatic exception and fatal error
32+
hooks and the signal handler, as follows:
33+
34+
```js
35+
var nodereport = require('nodereport/api');
36+
nodereport.triggerReport();
37+
```
2738

2839
Content of the NodeReport consists of a header section containing the event
2940
type, date, time, PID and Node version, sections containing JavaScript and
@@ -50,19 +61,12 @@ can be specified as a parameter on the `triggerReport()` call.
5061
nodereport.triggerReport("myReportName");
5162
```
5263

53-
A NodeReport can also be triggered automatically on unhandled exceptions, fatal
54-
error events (for example out of memory errors), and signals (Linux/MacOS only).
55-
Triggering on these events can be enabled using the following API call:
56-
57-
```js
58-
nodereport.setEvents("exception+fatalerror+signal+apicall");
59-
```
60-
6164
## Configuration
6265

6366
Additional configuration is available using the following APIs:
6467

6568
```js
69+
nodereport.setEvents("exception+fatalerror+signal+apicall");
6670
nodereport.setSignal("SIGUSR2|SIGQUIT");
6771
nodereport.setFileName("stdout|stderr|<filename>");
6872
nodereport.setDirectory("<full path>");

binding.gyp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"targets": [
33
{
4-
"target_name": "nodereport",
4+
"target_name": "api",
55
"sources": [ "src/node_report.cc", "src/module.cc" ],
66
"include_dirs": [ '<!(node -e "require(\'nan\')")' ],
77
"conditions": [
@@ -27,11 +27,11 @@
2727
{
2828
"target_name": "install",
2929
"type":"none",
30-
"dependencies" : [ "nodereport" ],
30+
"dependencies" : [ "api" ],
3131
"copies": [
3232
{
3333
"destination": "<(module_root_dir)",
34-
"files": ["<(module_root_dir)/build/Release/nodereport.node"]
34+
"files": ["<(module_root_dir)/build/Release/api.node"]
3535
}]
3636
},
3737
],

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Main module entry point for nodereport
2+
3+
const api = require('./api');
4+
5+
// NODEREPORT_EVENTS env var overrides the defaults
6+
const options = process.env.NODEREPORT_EVENTS || 'exception+fatalerror+signal+apicall';
7+
api.setEvents(options);
8+
9+
exports.triggerReport = api.triggerReport;
10+
exports.setEvents = api.setEvents;
11+
exports.setCoreDump = api.setCoreDump;
12+
exports.setSignal = api.setSignal;
13+
exports.setFileName = api.setFileName;
14+
exports.setDirectory = api.setDirectory;
15+
exports.setVerbose = api.setVerbose;

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "nodereport",
3-
"main": "nodereport.node",
43
"version": "1.0.7",
54
"description": "Diagnostic NodeReport",
65
"homepage": "https://github.com/nodejs/nodereport#readme",

test/test-api-nohooks.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
// Testcase to produce NodeReport via API call, using the no-hooks/no-signal
4+
// interface - i.e. require('nodereport/api')
5+
if (process.argv[2] === 'child') {
6+
const nodereport = require('../api');
7+
nodereport.triggerReport();
8+
} else {
9+
const common = require('./common.js');
10+
const spawn = require('child_process').spawn;
11+
const tap = require('tap');
12+
13+
const child = spawn(process.execPath, [__filename, 'child']);
14+
child.on('exit', (code) => {
15+
tap.plan(3);
16+
tap.equal(code, 0, 'Process exited cleanly');
17+
const reports = common.findReports(child.pid);
18+
tap.equal(reports.length, 1, 'Found reports ' + reports);
19+
const report = reports[0];
20+
common.validate(tap, report, {pid: child.pid});
21+
});
22+
}

test/test-exception.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Testcase to produce NodeReport on uncaught exception
44
if (process.argv[2] === 'child') {
5-
require('../').setEvents('exception');
5+
require('../');
66

77
function myException(request, response) {
88
const m = '*** exception.js: testcase exception thrown from myException()';

test/test-fatal-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Testcase to produce NodeReport on fatal error (javascript heap OOM)
44
if (process.argv[2] === 'child') {
5-
require('../').setEvents('fatalerror');
5+
require('../');
66

77
const list = [];
88
while (true) {

test/test-signal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Testcase to produce NodeReport on signal interrupting a js busy-loop,
44
// showing it is interruptible.
55
if (process.argv[2] === 'child') {
6-
require('../').setEvents('signal');
6+
require('../');
77

88
// Exit on loss of parent process
99
process.on('disconnect', () => process.exit(2));

0 commit comments

Comments
 (0)