Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 1e08348

Browse files
author
Matt Loring
committed
Resume logging after log quota is hit
1 parent a26278e commit 1e08348

File tree

4 files changed

+78
-34
lines changed

4 files changed

+78
-34
lines changed

lib/debuglet.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ Debuglet.prototype.addBreakpoint_ = function(breakpoint, cb) {
334334
that.activeBreakpointMap_[breakpoint.id] = breakpoint;
335335

336336
if (breakpoint.action === 'LOG') {
337-
that.v8debug_.log(breakpoint, Debuglet.format,
337+
that.v8debug_.log(breakpoint,
338+
function(fmt, exprs) {
339+
console.log('LOGPOINT:', Debuglet.format(fmt, exprs));
340+
},
338341
function() {
339342
return that.completedBreakpointMap_[breakpoint.id];
340343
});

lib/v8debugapi.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,33 @@ module.exports.create = function(logger_, config_, fileStats_) {
206206
* @param {Breakpoint} breakpoint
207207
* @param {Function} callback
208208
*/
209-
log: function(breakpoint, format, shouldStop) {
209+
log: function(breakpoint, print, shouldStop) {
210210
var num = breakpoints[breakpoint.id].v8Breakpoint.number();
211+
var logsThisSecond = 0;
212+
var timesliceEnd = Date.now() + 1000;
211213
var listener = onBreakpointHit.bind(
212-
null, breakpoint,
213-
createLogpointHandler(breakpoint, format, shouldStop, listener));
214-
214+
null, breakpoint, function(err) {
215+
var currTime = Date.now();
216+
if (currTime > timesliceEnd) {
217+
logsThisSecond = 0;
218+
timesliceEnd = currTime + 1000;
219+
}
220+
print(breakpoint.logMessageFormat,
221+
breakpoint.evaluatedExpressions.map(JSON.stringify));
222+
logsThisSecond++;
223+
if (shouldStop()) {
224+
listeners[num].enabled = false;
225+
} else {
226+
if (logsThisSecond >= config.log.maxLogsPerSecond) {
227+
listeners[num].enabled = false;
228+
setTimeout(function() {
229+
if (!shouldStop()) {
230+
listeners[num].enabled = true;
231+
}
232+
}, config.log.logDelaySeconds * 1000);
233+
}
234+
}
235+
});
215236
listeners[num] = { enabled: true, listener: listener };
216237
},
217238

@@ -327,33 +348,6 @@ module.exports.create = function(logger_, config_, fileStats_) {
327348
setImmediate(function() { cb(null); }); // success.
328349
}
329350

330-
function createLogpointHandler(breakpoint, format, shouldStop, listener) {
331-
var logsThisSecond = 0;
332-
var timesliceEnd = Date.now() + 1000;
333-
var num = breakpoints[breakpoint.id].v8Breakpoint.number();
334-
return function(err) {
335-
var currTime = Date.now();
336-
if (currTime > timesliceEnd) {
337-
logsThisSecond = 0;
338-
timesliceEnd = currTime + 1000;
339-
}
340-
var message = format(breakpoint.logMessageFormat,
341-
breakpoint.evaluatedExpressions.map(JSON.stringify));
342-
logsThisSecond++;
343-
console.log('LOGPOINT:', message);
344-
if (shouldStop()) {
345-
listeners[num].enabled = false;
346-
} else {
347-
if (logsThisSecond > config.log.maxLogsPerSecond) {
348-
listeners[num].enabled = false;
349-
setTimeout(function() {
350-
listeners[num].enabled = true;
351-
}, config.log.logDelaySeconds * 1000);
352-
}
353-
}
354-
};
355-
}
356-
357351
function endsWith(str, suffix) {
358352
if (typeof String.prototype.endsWith === 'function') {
359353
return str.endsWith(suffix);

test/e2e/test-log-throttling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ if (cluster.isMaster) {
201201
runTest();
202202
} else {
203203
var agent = require('../..');
204-
agent.private_.config_.log.maxLogsPerSecond = 1;
204+
agent.private_.config_.log.maxLogsPerSecond = 2;
205205
agent.private_.config_.log.logDelaySeconds = 5;
206206
setTimeout(process.send.bind(process, agent), 7000);
207207
setInterval(fib.bind(null, 12), 500);

test/test-v8debugapi.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,53 @@ describe('v8debugapi', function() {
386386
});
387387
});
388388

389+
describe('log', function() {
390+
var oldLPS;
391+
var oldDS;
392+
393+
before(function() {
394+
oldLPS = config.log.maxLogsPerSecond;
395+
oldDS = config.log.logDelaySeconds;
396+
config.log.maxLogsPerSecond = 1;
397+
config.log.logDelaySeconds = 1;
398+
});
399+
400+
after(function() {
401+
config.log.maxLogsPerSecond = oldLPS;
402+
config.log.logDelaySeconds = oldDS;
403+
assert(stateIsClean(api));
404+
});
405+
406+
it('should throttle correctly', function(done) {
407+
var completed = false;
408+
var bp = {
409+
id: breakpointInFoo.id,
410+
location: breakpointInFoo.location,
411+
action: 'LOG',
412+
logMessageFormat: 'cat'
413+
};
414+
api.set(bp, function(err) {
415+
var transcript = '';
416+
var runCount = 0;
417+
assert.ifError(err);
418+
api.log(bp, function(fmt) { transcript += fmt; },
419+
function() { return completed; });
420+
var interval = setInterval(function() {
421+
foo(1);
422+
runCount++;
423+
}, 100);
424+
setTimeout(function() {
425+
completed = true;
426+
assert.equal(transcript, 'catcat');
427+
assert(runCount > 12);
428+
clearInterval(interval);
429+
api.clear(bp);
430+
done();
431+
}, 1500);
432+
});
433+
});
434+
});
435+
389436
describe('set and wait', function() {
390437

391438
it('should be possible to wait on a breakpoint', function(done) {
@@ -431,7 +478,7 @@ describe('v8debugapi', function() {
431478
var bp = {
432479
id: breakpointInFoo.id,
433480
action: 'LOG',
434-
log_message_format: 'Hello World',
481+
logMessageFormat: 'Hello World',
435482
location: breakpointInFoo.location
436483
};
437484
api.set(bp, function(err) {

0 commit comments

Comments
 (0)