Skip to content

Commit 1721c19

Browse files
committed
Merge pull request #41 from AnyFetch/opbeat
Opbeat
2 parents 1fe185c + b46a709 commit 1721c19

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

lib/helpers/hydrater.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var rarity = require('rarity');
1414
var util = require('util');
1515

1616
var lib = require('../index.js');
17-
1817
var HydrationError = lib.HydrationError;
18+
var logError = require('../utils').logError;
1919

2020

2121

@@ -52,6 +52,12 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
5252
if(!cleaner.called) {
5353
cleaner.called = true;
5454
if(err) {
55+
var extra = JSON.parse(JSON.stringify(task));
56+
extra.changes = changes;
57+
extra.stdout = stdout;
58+
extra.stderr = stderr;
59+
60+
logError(err, extra);
5561
child.reset();
5662
}
5763
else {
@@ -170,7 +176,7 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
170176
cb(null, changes);
171177
},
172178
function patchDocument(changes, cb) {
173-
// Returning null means we won't complete the hdyration, and are waiting for something else.
179+
// Returning null means we won't complete the hydration, and are waiting for something else.
174180
if(changes === null) {
175181
logger("Skipped task: " + ((task.file_path) ? task.file_path : task.document.id));
176182
return cb();
@@ -187,8 +193,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
187193
}
188194
], function handleErrors(err, changes, res) {
189195
async.waterfall([
190-
function logError(cb) {
196+
function logErrors(cb) {
191197
if(err) {
198+
var extra = JSON.parse(JSON.stringify(task));
199+
extra.changes = changes;
200+
logError(err, extra);
192201
errLogger("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id), err.toString());
193202
}
194203

@@ -214,7 +223,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
214223
}
215224
}
216225
], function(internalErr) {
226+
/* istanbul ignore next */
217227
if(internalErr) {
228+
var extra = JSON.parse(JSON.stringify(task));
229+
extra.changes = changes;
230+
logError(internalErr, extra);
218231
errLogger("INTERNAL ERR", internalErr);
219232
}
220233
done(err || internalErr, changes);

lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var restify = require('restify');
55
var yaqs = require('yaqs');
66
var Childs = require('./helpers/Childs');
77

8+
var utils = require('./utils.js');
89
/**
910
* Create a new hydration server.
1011
* This server will use `config.hydrater_function` as its main function, to turn a file into metadata.
@@ -23,6 +24,14 @@ module.exports.createServer = function(config) {
2324
config.logger = config.logger || console.log;
2425
config.errLogger = config.errLogger || console.error;
2526

27+
utils.logError.config = config;
28+
29+
/* istanbul ignore next */
30+
if(config.opbeat && config.opbeat.secretToken) {
31+
var opbeat = require('opbeat');
32+
utils.logError.opbeat = opbeat(config.opbeat);
33+
}
34+
2635
var concurrency = config.concurrency || 1;
2736
var tasksPerProcess = config.tasksPerProcess || 100;
2837

lib/utils.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
3+
/* istanbul ignore next */
4+
module.exports.logError = function logError(err, req, extra) {
5+
// No logging on test or if err is undefined
6+
if(process.env.NODE_ENV === "test" || !err) {
7+
return;
8+
}
9+
10+
if(!extra) {
11+
extra = req;
12+
req = null;
13+
}
14+
15+
delete err.domain;
16+
delete err.domainThrown;
17+
18+
if(err.__alreadyLogged) {
19+
console.warn("Skipping an error already sent to Opbeat: ", err.toString());
20+
return;
21+
}
22+
23+
if(!extra) {
24+
extra = {};
25+
}
26+
27+
if(module.exports.logError.config) {
28+
extra.hydrater = module.exports.logError.config.hydraterUrl;
29+
}
30+
31+
if(module.exports.logError.opbeat) {
32+
var meta = {
33+
extra: extra
34+
};
35+
36+
if(req) {
37+
meta.request = req;
38+
39+
if(req.token) {
40+
meta.user = {
41+
is_authenticated: true,
42+
id: req.token.anyfetchToken,
43+
username: req.token.accountName,
44+
email: req.token.accountName
45+
};
46+
}
47+
}
48+
49+
module.exports.logError.opbeat.captureError(err, meta);
50+
}
51+
else {
52+
var all = {
53+
details: err.toString(),
54+
err: err,
55+
extra: extra
56+
};
57+
58+
try {
59+
all = JSON.stringify(all);
60+
}
61+
catch(e) {
62+
// Converting circular structure to JSON.
63+
// We can't do anything, let's log the raw object.
64+
}
65+
66+
console.warn("LOG-ERROR-DETAILS", all);
67+
}
68+
69+
err.__alreadyLogged = true;
70+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"dependencies": {
2020
"async": "^0.9.0",
21+
"opbeat": "^1.0.5",
2122
"rarity": "^2.1.1",
2223
"redis": "^0.12.1",
2324
"restify": "^2.8.3",

0 commit comments

Comments
 (0)