Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
feat(filters): Add a stacktrace filter.
Browse files Browse the repository at this point in the history
Still pretty basic, and needs tests. Addresses #1.
  • Loading branch information
lance committed Nov 9, 2016
1 parent e1fd6a3 commit f951ee3
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ci: test

test: lint
test: node_modules lint
npm $@

lint:
Expand Down
3 changes: 3 additions & 0 deletions example/stacktrace/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "semistandard"
}
13 changes: 13 additions & 0 deletions example/stacktrace/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

Copyright 2016 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
24 changes: 24 additions & 0 deletions example/stacktrace/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('clarify');

const LightBright = require('../../');
LightBright.addFilter(LightBright.builtins.stacktrace);
LightBright.enable();

function somethingAsync () {
setTimeout(
() => {
throw new Error('I broke');
}, 10);
}

function callsSomethingAsync () {
somethingAsync();
}

callsSomethingAsync();

process.on('uncaughtException', (e) => {
process._rawDebug('UncaughtException');
process._rawDebug(e);
});

30 changes: 30 additions & 0 deletions example/stacktrace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "stacktrace",
"version": "0.0.1",
"main": false,
"author": "Red Hat, Inc.",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git://github.com/bucharest-gold/lightbright.git"
},
"files": [
"package.json",
"LICENSE",
"index.js"
],
"bugs": {
"url": "https://github.com/bucharest-gold/lightbright/issues"
},
"homepage": "https://github.com/bucharest-gold/lightbright/examples/stacktrace",
"description": "Example app showing usage of the LightBright.builtins.stacktrace filter",
"keywords": [
"trace",
"stack",
"stacktrace",
"async"
],
"dependencies": {
"clarify": "~2.0.0"
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const LightBright = require('./lib/trace_hooks');
LightBright.builtins = {
timing: require('./lib/filters/timing')
timing: require('./lib/filters/timing'),
stacktrace: require('./lib/filters/stack-trace')
};

module.exports = exports = LightBright;
59 changes: 59 additions & 0 deletions lib/filters/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function FormatErrorString (error) {
try {
return Error.prototype.toString.call(error);
} catch (e) {
try {
return "<error: " + e + ">";
} catch (ee) {
return "<error>";
}
}
}

module.exports = function FormatStackTrace (error, frames) {
var lines = [];
lines.push(FormatErrorString(error));
for (var i = 0; i < frames.length; i++) {
var frame = frames[i];
var line;
try {
line = frame.toString();
} catch (e) {
try {
line = "<error: " + e + ">";
} catch (ee) {
// Any code that reaches this point is seriously nasty!
line = "<error>";
}
}
lines.push(" at " + line);
}
return lines.join("\n");
};
42 changes: 42 additions & 0 deletions lib/filters/stack-trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const chain = require('stack-chain');
const stackFormatter = require('./format.js');
const records = new WeakMap();
let currentStack = null;

Error.prepareStackTrace = function prepareStackTrace (err, stack) {
if (currentStack) stack.push.apply(stack, currentStack);
err.rawStack = stack;
return stackFormatter(err, stack);
};

function getCallSites (skip) {
const limit = Error.stackTraceLimit;

Error.stackTraceLimit = limit + skip;
const stack = chain.callSite({
extend: false,
filter: true,
slice: skip
});
Error.stackTraceLimit = limit;
return stack;
}

function tracer (record) {
switch (record.step) {
case 'init': {
records.set(record.handle, getCallSites(6));
break;
}
case 'pre': {
currentStack = records.get(record.handle);
break;
}
case 'post': {
currentStack = null;
break;
}
}
}

module.exports = exports = tracer;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
"type": "git",
"url": "https://github.com/bucharest-gold/lightbright.git"
},
"bugs": "https://github.com/bucharest-gold/lightbright/issues"
"bugs": "https://github.com/bucharest-gold/lightbright/issues",
"dependencies": {
"stack-chain": "~1.3.7"
}
}

0 comments on commit f951ee3

Please sign in to comment.