Skip to content

Commit 0bb1861

Browse files
committed
errors from logs: performance pass
1 parent b0915b5 commit 0bb1861

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

index.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,21 @@ exports.register = (server, options) => {
3838
},
3939
});
4040

41-
let catchLogErrors = false;
4241
let errorTags = ['error', 'fatal', 'fail'];
43-
if (opts.catchLogErrors) {
44-
catchLogErrors = true;
45-
if (Array.isArray(opts.catchLogErrors)) {
46-
errorTags = opts.catchLogErrors;
47-
}
42+
if (opts.catchLogErrors && Array.isArray(opts.catchLogErrors)) {
43+
errorTags = opts.catchLogErrors;
4844
}
4945

50-
function shouldIgnoreEvent(event) {
51-
const match = catchLogErrors &&
52-
event.error &&
53-
errorTags.reduce((cond, tag) => cond || event.tags.includes(tag), false);
54-
55-
return event.channel === 'app' && !match;
56-
}
46+
const channels = ['error'];
47+
// also listen for app events to get log messages
48+
if (opts.catchLogErrors) channels.push('app');
5749

5850
// get request errors to capture them with sentry
59-
server.events.on({ name: 'request', channels: ['error', 'app'] }, (request, event) => {
60-
if (shouldIgnoreEvent(event)) {
61-
return;
51+
server.events.on({ name: 'request', channels }, (request, event) => {
52+
// check for errors in request logs
53+
if (event.channel === 'app') {
54+
if (!event.error) return; // no error, just a log message
55+
if (event.tags.some(tag => errorTags.includes(tag)) === false) return; // no matching tag
6256
}
6357

6458
Sentry.withScope(scope => { // thus use a temp scope and re-assign it
@@ -92,23 +86,24 @@ exports.register = (server, options) => {
9286
});
9387
});
9488

95-
server.events.on('log', event => {
96-
if (shouldIgnoreEvent(event)) {
97-
return;
98-
}
89+
if (opts.catchLogErrors) {
90+
server.events.on({ name: 'log', channels: ['app'] }, event => {
91+
if (!event.error) return; // no error, just a log message
92+
if (event.tags.some(tag => errorTags.includes(tag)) === false) return; // no matching tag
9993

100-
Sentry.withScope(scope => {
101-
scope.addEventProcessor(sentryEvent => {
102-
sentryEvent.level = 'error';
94+
Sentry.withScope(scope => {
95+
scope.addEventProcessor(sentryEvent => {
96+
sentryEvent.level = 'error';
10397

104-
// some SDK identificator
105-
sentryEvent.sdk = { name: 'sentry.javascript.node.hapi', version };
106-
return sentryEvent;
107-
});
98+
// some SDK identificator
99+
sentryEvent.sdk = { name: 'sentry.javascript.node.hapi', version };
100+
return sentryEvent;
101+
});
108102

109-
Sentry.captureException(event.error);
103+
Sentry.captureException(event.error);
104+
});
110105
});
111-
});
106+
}
112107
};
113108

114109
exports.name = name;

0 commit comments

Comments
 (0)