Skip to content

Commit 65570b5

Browse files
authored
Add request duration in Sentry logs (getodk#723)
* add request duration in Sentry logs * configure scope instead of polluting request header * using sentry from the container
1 parent 0467401 commit 65570b5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/external/sentry.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to the terms contained in the LICENSE file.
99

1010
const { inspect } = require('util');
11-
const { isBlank, isPresent } = require('../util/util');
11+
const { isBlank, isPresent, noop } = require('../util/util');
1212

1313

1414
// Endpoints where query strings are sensitive and should be removed
@@ -91,6 +91,7 @@ const sanitizeEventRequest = (event) => {
9191
// filter out access tokens embedded in URLs
9292
event.request.url = filterTokenFromUrl(event.request.url);
9393
}
94+
9495
}
9596

9697
/* eslint-enable no-param-reassign */
@@ -110,7 +111,8 @@ const init = (config) => {
110111
process.stderr.write('attempted to log Sentry exception in development:\n');
111112
process.stderr.write(inspect(err));
112113
process.stderr.write('\n');
113-
}
114+
},
115+
configureScope: noop
114116
};
115117
}
116118

@@ -122,6 +124,10 @@ const init = (config) => {
122124
beforeSend(event, hint) {
123125
const sanitizedEvent = sanitizeEventRequest(event);
124126

127+
if (sanitizedEvent.extra && sanitizedEvent.extra.startTimestamp) { // just an extra safety check. this should be always true
128+
sanitizedEvent.extra.duration = Date.now() - sanitizedEvent.extra.startTimestamp;
129+
}
130+
125131
// only file the event if it is a bare exception or it is a true 500.x Problem.
126132
const error = hint.originalException;
127133
if (error == null) return sanitizedEvent; // we aren't sure why there isn't an exception; pass through.

lib/http/service.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// defined elsewhere into an actual Express service. The only thing it needs in
1212
// order to do this is a valid dependency injection context container.
1313

14-
1514
module.exports = (container) => {
1615
const service = require('express')();
1716

@@ -21,6 +20,13 @@ module.exports = (container) => {
2120
// apply the Sentry request hook.
2221
service.use(container.Sentry.Handlers.requestHandler());
2322

23+
service.use((req, res, next) => {
24+
container.Sentry.configureScope(scope => {
25+
scope.setExtra('startTimestamp', Date.now());
26+
});
27+
next();
28+
});
29+
2430
// automatically parse JSON if it is marked as such. otherwise, just pull the
2531
// plain-text body contents.
2632
const bodyParser = require('body-parser');

test/unit/external/sanitize-sentry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ const filteredTokenUrls = [
548548

549549
describe('external: sanitize-sentry', () => {
550550
it('removes sensitive data from request objects ', () => {
551+
551552
for (const [input, expectedOutput] of cases) {
552553
sanitizeEventRequest({ request: input }).should.eql({ request: expectedOutput });
553554
}

0 commit comments

Comments
 (0)