Skip to content

Commit ab3927a

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
error governor fix
1 parent 3f1103c commit ab3927a

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lib/error.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ var url = require('url'),
44
helpers = require('./helpers'),
55
CONFIG = require('../config/config'),
66

7-
// hash that contains all the errors and their number logged during the current minute
7+
// object that contains all the errors and their numbers logged during the current minute
88
errorStorage = {};
99

1010
module.exports = {
1111
/**
12-
* Check for duplicated error messages. If the same error message logged more than configurated limit in one minute
12+
* Check for duplicate error messages. If the same error message logged more than configurated limit in one minute
1313
* don't push it to the queue
1414
*/
1515
checkErrorLimitMessage : function checkErrorLimitMessage(ex) {
@@ -34,7 +34,7 @@ module.exports = {
3434
errorStorage[min][key] = 1;
3535
}
3636

37-
return errorStorage[min][key] < CONFIG.MSG.MAX_DUP_ERROR_PER_MINUTE;
37+
return errorStorage[min][key] <= CONFIG.MSG.MAX_DUP_ERROR_PER_MINUTE;
3838
},
3939

4040
// getting source method and source method line of code
@@ -69,7 +69,7 @@ module.exports = {
6969
ServerVariables: process.env
7070
},
7171
href;
72-
72+
// add web request details if req object is passed
7373
if (req) {
7474
href = url.parse(helpers.getURL(req), true);
7575
ex.WebRequestDetail = {

lib/logger.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var api = require('./api'),
55

66
storage = [],
77

8-
// current delay
8+
// current delay, starting with one sec
99
delay = CONFIG.DELAY.ONE_SECOND_DELAY,
1010
sendingLogsInProgress = false,
1111
// delayed sendLogs call
@@ -48,10 +48,10 @@ var api = require('./api'),
4848
}
4949
}
5050
};
51-
// count the delay
51+
// re-count the delay
5252
if (length >= CONFIG.MSG.MAX_BATCH_SIZE) {
5353
delay = Math.max(Math.round(delay / 2.0), CONFIG.DELAY.ONE_SECOND_DELAY);
54-
} else {
54+
} else if (length < CONFIG.MSG.MIN_BATCH_SIZE) {
5555
delay = Math.min(Math.round(1.25 * delay), CONFIG.DELAY.FIVE_SECONDS_DELAY);
5656
}
5757

@@ -86,7 +86,8 @@ module.exports.methods = {
8686
if (meta.length) {
8787
if (level.toLowerCase() === 'error') {
8888
data = helpers.parseMeta(meta, true);
89-
if (data.ex) {
89+
// if duplicate errors cap per minute isn't exceeded and error object is passed, create an exception
90+
if (data.ex && error.checkErrorLimitMessage(data.ex)) {
9091
messageObject.Ex = error.formatEx(data.ex, req);
9192
}
9293
} else {
@@ -98,26 +99,27 @@ module.exports.methods = {
9899
}
99100
}
100101

101-
if (level.toLowerCase() === 'error' && !messageObject.Ex) {
102+
// if error object isn't passed with message & duplicate errors cap per minute isn't exceeded, create a string exception
103+
if (level.toLowerCase() === 'error' && !messageObject.Ex && error.checkErrorLimitMessage(data.ex)) {
102104
messageObject.Ex = error.formatEx(err, null, msg);
103105
}
104106

105-
if ((level.toLowerCase() === 'error' && error.checkErrorLimitMessage(messageObject.Ex)) || level.toLowerCase() !== 'error') {
106-
storage.push(messageObject);
107-
// remove the earliest message from the queue if message cap is exceeded
108-
if (storage.length === CONFIG.MSG.QUEUE_CAP) {
109-
storage.shift();
110-
}
107+
storage.push(messageObject);
108+
109+
// remove the earliest message from the queue if message cap is exceeded
110+
if (storage.length === CONFIG.MSG.QUEUE_CAP) {
111+
storage.shift();
111112
}
112113
},
113114

114-
/* start sending logs after IdentifyApp call is done
115-
*/
115+
// start sending logs after IdentifyApp call is done
116116
start: function start() {
117117
timeout = setTimeout(sendLogs, delay);
118118
},
119+
119120
// reset current delay schedule, push exception to the queue, send data and run the callback
120121
sendException: function sendException(err, req, cb) {
122+
// check if messages are being sent right now
121123
var check = function check() {
122124

123125
if (sendingLogsInProgress) {
@@ -131,7 +133,14 @@ module.exports.methods = {
131133
this.push('error', err.message, [err], req);
132134
check();
133135
},
136+
// drain the queue and send the messages before server closes
137+
drain : function drain() {
138+
if (storage.length) {
139+
api.methods.postLogsSync(storage);
140+
}
141+
},
134142

143+
// basic logging method
135144
log: function log(level, msg) {
136145
var meta = Array.prototype.slice.call(arguments, 2);
137146
var levels = ['error', 'debug', 'warn', 'info', 'trace'];
@@ -140,11 +149,10 @@ module.exports.methods = {
140149
if (levels.indexOf(level.toLowerCase()) < 0) {
141150
throw new TypeError(level + ' level doesn\'t exist');
142151
}
143-
// check the message
152+
// check the message itself
144153
if (typeof msg !== 'string') {
145154
throw new TypeError('Message must be a string');
146155
}
147-
148156
this.push(level, msg, meta);
149157
},
150158

0 commit comments

Comments
 (0)