-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stacktrace): collect data from errors
Wrap `process._fatalException` function Add unwrapping functions Refactor app and collector to use config Cover error tracing w/ tests Cleanup tests to use test-setup
- Loading branch information
Gábor Döbrei
committed
Jul 24, 2015
1 parent
81fa6f8
commit a1f564f
Showing
10 changed files
with
192 additions
and
26 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
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
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
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
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
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
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 getNamespace = require('continuation-local-storage').getNamespace; | ||
var microtime = require('microtime'); | ||
|
||
function wrapRequest (original, collector) { | ||
var session = getNamespace('trace'); | ||
|
||
return session.bind(function (stackTrace) { | ||
collector.onCrash({ | ||
id: session.get('request-id'), | ||
time: microtime.now(), | ||
stackTrace: stackTrace.stack | ||
}); | ||
return original.apply(this, arguments); | ||
}); | ||
} | ||
|
||
module.exports = wrapRequest; |
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,73 @@ | ||
var fs = require('fs'); | ||
var http = require('http'); | ||
var spawn = require('child_process').spawn; | ||
var path = require('path'); | ||
|
||
var extend = require('lodash/object/extend'); | ||
var expect = require('chai').expect; | ||
|
||
var spawned; | ||
|
||
function startFailingServer (kill) { | ||
var env = extend({}, process.env); | ||
env.TRACE_CONFIG_PATH = path.join(__dirname, '../../test/mocks', 'failingServer.config.js'); | ||
|
||
spawned = spawn('node', ['--harmony', 'test/mocks/failingServer.js'], { | ||
env: env | ||
}); | ||
|
||
if (kill) { | ||
spawned.kill(); | ||
} | ||
} | ||
|
||
describe('The stacktrace wrapper module', function () { | ||
this.timeout(5000); | ||
|
||
before(function () { | ||
startFailingServer(); | ||
}); | ||
|
||
after(function () { | ||
spawned.kill(); | ||
}); | ||
|
||
it('writes stacktrace into log file', function (done) { | ||
setTimeout(function () { | ||
http | ||
.get('http://localhost:15124') | ||
.on('error', function (err) { | ||
// the server shuts down => TCP error | ||
expect(err.code).to.be.eq('ECONNRESET'); | ||
|
||
var files = fs.readdirSync(path.join(__dirname, '../../', 'test/mocks')); | ||
|
||
var logFiles = files.filter(function (file) { | ||
return file.indexOf('trace_') > -1; | ||
}); | ||
|
||
var LOGFILE_PATH = path.join(__dirname, '../../', 'test/mocks', logFiles[0]); | ||
|
||
fs.readFile(LOGFILE_PATH, 'utf-8', function (err, raw) { | ||
expect(err).to.be.not.ok; | ||
expect(raw).to.be.ok; | ||
|
||
var stacktrace = JSON.parse(raw.slice(0, -2)).events[0]; | ||
|
||
// it's a stacktrace | ||
expect(stacktrace.type).to.be.eq('st'); | ||
|
||
// the original error from the failing server | ||
var errMsg = 'Error: Very error'; | ||
expect(stacktrace.data.trace.slice(0, errMsg.length)).to.be.eq(errMsg); | ||
done(); | ||
}); | ||
}); | ||
}, 800); | ||
}); | ||
|
||
it('reads the previous log files', function () { | ||
startFailingServer(true); | ||
}); | ||
|
||
}); |
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 config = {}; | ||
|
||
config.appName = '#fail'; | ||
|
||
function FailingServerTestReporter () { | ||
this.send = function (data, cb) { | ||
cb(null); | ||
}; | ||
|
||
this.getService = function () { | ||
return config.appName; | ||
}; | ||
} | ||
|
||
config.reporter = new FailingServerTestReporter(); | ||
|
||
module.exports = config; |
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,24 @@ | ||
var path = require('path'); | ||
|
||
var express = require('express'); | ||
var collectorConfig = require('../../lib/config'); | ||
|
||
// override log file path | ||
collectorConfig.logFilePath = path.join(__dirname, '/'); | ||
|
||
// use trace collector | ||
require('../../lib'); | ||
|
||
var failingServer = express(); | ||
|
||
failingServer.get('/', function () { | ||
setTimeout(function () { | ||
throw new Error('Very error'); | ||
}, 0); | ||
}); | ||
|
||
failingServer.listen(15124, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); |