Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: audit timers of same name should accumulate (#1435) #1443

Merged
merged 1 commit into from Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plugins/audit.js
Expand Up @@ -86,7 +86,7 @@ function auditLogger(opts) {
(req.timers || []).forEach(function (time) {
var t = time.time;
var _t = Math.floor((1000000 * t[0]) + (t[1] / 1000));
timers[time.name] = _t;
timers[time.name] = (timers[time.name] || 0) + _t;
});
return ({
// account for native and queryParser plugin usage
Expand Down
55 changes: 55 additions & 0 deletions test/plugins/audit.test.js
Expand Up @@ -277,6 +277,61 @@ describe('audit logger', function () {
});


it('restify-GH-1435 should accumulate log handler timers', function (done) {
// Dirty hack to capture the log record using a ring buffer.
var ringbuffer = new bunyan.RingBuffer({ limit: 1 });

SERVER.once('after', restify.plugins.auditLogger({
log: bunyan.createLogger({
name: 'audit',
streams:[ {
level: 'info',
type: 'raw',
stream: ringbuffer
}]
}),
event: 'after'
}));

SERVER.get('/audit', function aTestHandler(req, res, next) {
req.startHandlerTimer('audit-acc');

setTimeout(function () {
req.endHandlerTimer('audit-acc');
// Very brief timing for same name
req.startHandlerTimer('audit-acc');
req.endHandlerTimer('audit-acc');
res.send('');
return (next());
}, 1100);
// this really should be 1000 but make it 1100 so that the tests
// don't sporadically fail due to timing issues.
});

CLIENT.get('/audit', function (err, req, res) {
assert.ifError(err);

var record = ringbuffer.records && ringbuffer.records[0];

// check timers
assert.ok(record, 'no log records');
assert.equal(
ringbuffer.records.length, 1,
'should only have 1 log record'
);
assert.ok(
record.req.timers.aTestHandler > 1000000,
'atestHandler should be > 1000000'
);
assert.ok(
record.req.timers['aTestHandler-audit-acc'] > 1000000,
'aTestHandler-audit-acc should be > 1000000'
);
done();
});
});


it('restify-GH-812 audit logger has query params string', function (done) {

// Dirty hack to capture the log record using a ring buffer.
Expand Down