Skip to content

Commit a5c7ca5

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
minor fixes + comments added
1 parent c0586f9 commit a5c7ca5

File tree

8 files changed

+103
-31
lines changed

8 files changed

+103
-31
lines changed

config/config.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ module.exports = {
44
'PORT': 443,
55
'IDENTIFY_PATH': '/API/Metrics/IdentifyApp',
66
'LOG_SAVE_PATH': '/API/Log/Save',
7-
'APIKEY': '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr',
8-
'MSG_LIMIT': 20, // number of messages
7+
'MSG_LIMIT': 100, // number of messages per batch
8+
'MSG_CAP': 10000, // overall messages cap in the queue
99
'SCAN_TIMER': 30 * 1000,
1010
'REQUEST_TIMER': 5 * 1000,
1111
'REQUEST_ATTEMPTS': 5, // number of attempts if API call isn't succesful
12-
'ERROR_FLOOD_LIMIT': 100, // limit of the same error message per minute
12+
/**
13+
* Number of instances of a unique error that are allowed to be sent in one minute
14+
*/
15+
'MAX_DUP_ERROR_PER_MINUTE': 5,
16+
/**
17+
* Cookie value
18+
*/
1319
'COOKIE_MASK': 'X-MASKED-X'
1420
};

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = {
1010

1111
start: function(options) {
1212
api.methods.identifyApp(options);
13-
/* exc.exc();
14-
*/ },
13+
exc.exc();
14+
},
1515

1616
log: logger.methods.log,
1717
trace: logger.methods.trace,

lib/api.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ var os = require('os'),
66
CONFIG = require('../config/config'),
77
pkginfo = require('pkginfo')(module, 'name'),
88

9-
appname = module.exports.name,
9+
appname = module.exports.name, //name of the app from package.json
1010

11-
options = function options(path, body) {
12-
return {
11+
//getting all the headers for requests
12+
options = function options(path, body, proxy) {
13+
var result = {
1314
url: CONFIG.PROTOCOL + '://' + CONFIG.HOST + path,
1415
method: 'POST',
1516
json: true,
@@ -19,7 +20,15 @@ var os = require('os'),
1920
'X-Stackify-PV': 'V1'
2021
},
2122
body: body
23+
};
24+
25+
if (proxy) {
26+
result.proxy = proxy;
27+
CONFIG.PROXY = CONFIG.PROXY || proxy;
2228
}
29+
30+
return result;
31+
2332
};
2433

2534
module.exports.methods = {
@@ -31,7 +40,7 @@ module.exports.methods = {
3140
ConfiguredAppName: appname,
3241
AppLocaton: process.env.PWD
3342
},
34-
opt = options(CONFIG.IDENTIFY_PATH, body),
43+
opt = options(CONFIG.IDENTIFY_PATH, body, settings ? settings.proxy : undefined),
3544
fail_counter = 0,
3645
callback = function (data) {
3746
console.log('successfully identified');
@@ -54,17 +63,18 @@ module.exports.methods = {
5463
};
5564

5665
console.log('Identifying...');
57-
58-
if (settings.proxy) {
59-
CONFIG.PROXY = opt.proxy = settings.proxy
60-
};
66+
67+
// check that settings object is correct
68+
if (!settings) {
69+
throw new TypeError('Settings are not provided');
70+
}
6171

6272
if (typeof (settings.apiKey) === 'string' && typeof (settings.env) === 'string') {
6373
opt.headers['X-Stackify-Key'] = settings.apiKey;
6474
CONFIG.APPNAME = appname;
6575
send(opt, callback, fail);
6676
} else {
67-
throw new TypeError('You have to pass apiKey and env parameters');
77+
throw new TypeError('You have to pass API key and env parameters');
6878
}
6979

7080
},
@@ -84,12 +94,7 @@ module.exports.methods = {
8494
Platform: 'Node.js',
8595
Msgs : messages
8696
},
87-
opt = options(CONFIG.LOG_SAVE_PATH, body);
88-
89-
if (CONFIG.PROXY) {
90-
opt.proxy = CONFIG.PROXY;
91-
};
92-
97+
opt = options(CONFIG.LOG_SAVE_PATH, body, CONFIG.PROXY);
9398

9499
send(opt, cb, fail);
95100
}

lib/error.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
var url = require('url'),
22
util = require('util'),
3+
34
stackTrace = require('stack-trace'),
5+
46
helpers = require('./helpers'),
57
CONFIG = require('../config/config'),
68

9+
// extracting cookies from request object
710
getCookies = function getCookies(req) {
811
var key,
912
keys,
@@ -27,14 +30,16 @@ var url = require('url'),
2730

2831
return result;
2932
},
30-
33+
// extracting post data
3134
getPostData = function getPostData(req) {
3235
var result = {};
3336
if (req.body instanceof Object) {
3437
result = req.body;
3538
}
3639
return result;
3740
},
41+
42+
//getting the trace of an exception
3843
getTrace = function getTrace(err) {
3944
var trace = stackTrace.parse(err),
4045
result = [];
@@ -52,6 +57,7 @@ var url = require('url'),
5257
});
5358
return result;
5459
},
60+
// correctly parsing the URL
5561
getURL = function getURL(req) {
5662
var href = (req.connection.encrypted ? "https://" : "http://") + req.headers.host + req.url;
5763
return href;
@@ -66,6 +72,8 @@ module.exports = {
6672
SrcLine: trace[0].LineNum
6773
}
6874
},
75+
76+
// create the exception details object
6977
formatEx : function formatEx(err, req, msg) {
7078
var trace = getTrace(err),
7179
ex = {

lib/exception.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
var qs = require('querystring'),
22
util = require('util'),
3+
34
error = require('./error'),
45
logger = require('./logger'),
56
api = require('./api'),
67
CONFIG = require('../config/config'),
8+
9+
10+
// function that sends all the messages in the queue to Stackify when exception is caught
711
handler = function handler(err, req, cb) {
812
var rec = {
913
Msg: err.name + ': ' + err.message,
@@ -12,6 +16,8 @@ var qs = require('querystring'),
1216
Ex: error.formatEx(err, req)
1317
},
1418
fail_counter = 0,
19+
20+
//retry the request if it failed
1521
fail = function () {
1622
fail_counter += 1;
1723
if (fail_counter < CONFIG.REQUEST_ATTEMPTS) {
@@ -29,9 +35,12 @@ var qs = require('querystring'),
2935
console.log('not identified yet');
3036
}
3137
},
38+
39+
// flag used for not catching the same exception twice (inside and outside of the createServer method)
3240
excCaught = false;
3341

3442
module.exports = {
43+
// Pure Node apps exception catching
3544
exc : function exc(req, res) {
3645
return (function () {
3746
var body = '';
@@ -55,7 +64,7 @@ module.exports = {
5564
});
5665
}());
5766
},
58-
67+
// Express error handling middleware
5968
expressExc : function expressExc(err, req, res, next) {
6069
console.log('err');
6170

lib/logger.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,32 @@ var api = require('./api'),
66

77
storage = [],
88

9-
// flag that indicates if logs are being sent properly. if false then new batches of messages won't be sent.
9+
// flag that indicates if logs are being sent right now. if false then new batches of messages won't be sent.
1010
flag = false,
1111
// deferred checking of logs
1212
timeout = 0,
1313
//number of failed attempts of sending logs
1414
fail_counter = 0,
15+
// hash that contains all the errors and their number logged during the current minute
16+
error_storage = {},
1517

18+
// automatically send all the messages from the queue after some timeout
1619
checkLogs = function () {
1720
var length = storage.length;
1821
if (flag && length) {
1922
sendLogs(length);
2023
}
2124
console.log('logs checked');
2225
},
23-
26+
// handler for sending logs
2427
sendLogs = function (len) {
2528
var self = this,
2629
length = len || CONFIG.MSG_LIMIT,
2730
data = storage.slice(0, length),
31+
32+
/* if request is succesful remove messages from the queue, change the timeout for the next checkLogs call,
33+
switch the flag, send another batch (if there are enough messages in the queue)
34+
*/
2835
success = function (response) {
2936
fail_counter = 0;
3037

@@ -43,6 +50,8 @@ var api = require('./api'),
4350
timeout = setTimeout(checkLogs, CONFIG.SCAN_TIMER);
4451
}
4552
},
53+
54+
// retry the attempt after some timeout
4655
fail = function () {
4756
flag = false;
4857
fail_counter += 1;
@@ -56,11 +65,37 @@ var api = require('./api'),
5665
api.methods.postLogs(data, success, fail);
5766
flag = false;
5867
}
68+
},
69+
/**
70+
Check for duplicated error messages
71+
*/
72+
checkErrorLimitMessage = function checkErrorLimitMessage (ex) {
73+
var min = new Date().getMinutes(),
74+
key = ex.Error.Message + ex.Error.ErrorType + ex.Error.SourceMethod;
75+
76+
if (!ex) {
77+
return true;
78+
}
79+
80+
if (error_storage[min]) {
81+
if (error_storage[min][key]) {
82+
error_storage[min][key] += 1
83+
} else {
84+
error_storage[min][key] = 1;
85+
}
86+
} else {
87+
errorStorage = {};
88+
error_storage[min] = {};
89+
error_storage[min][key] = 1;
90+
}
91+
92+
return (error_storage[min][key] < CONFIG.MAX_DUP_ERROR_PER_MINUTE) ? true : false;
5993
};
6094

6195
module.exports.storage = storage;
6296

6397
module.exports.methods = {
98+
// create the message object, push it to the queue and execute sendLogs call if messages cap is exceeded
6499
push : function push(level, msg, meta) {
65100
var err = new Error(),
66101
getStack = error.getStackTraceItem(err),
@@ -92,15 +127,23 @@ module.exports.methods = {
92127
rec.Ex = error.formatEx(err, null, msg);
93128
}
94129

95-
storage.push(rec);
96-
console.log('logged');
97-
console.log(storage.length);
130+
if ((level.toLowerCase() === 'error' && checkErrorLimitMessage(rec.Ex)) || level.toLowerCase() !== 'error') {
131+
storage.push(rec);
132+
console.log('logged');
133+
134+
if (storage.length === CONFIG.MSG_CAP) {
135+
storage.shift();
136+
}
137+
}
98138

99139
if (flag && storage.length >= CONFIG.MSG_LIMIT && CONFIG.APIKEY) {
100140
sendLogs();
101141
}
102142
},
103143

144+
/* check the queue after IdentifyApp call is done, switch the flag, set the timeout for checking the queue,
145+
set environment details for already logged messages
146+
*/
104147
init: function init() {
105148
console.log('flag switched');
106149
flag = true;
@@ -123,7 +166,7 @@ module.exports.methods = {
123166
sendLogs();
124167
}
125168
},
126-
169+
127170
log: function log(level, msg, meta) {
128171
var levels = ['error', 'debug', 'warn', 'info', 'trace'];
129172

lib/sender.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Low level function for sending http/https requests
77
*/
88
var http = require('http'),
99
util = require('util'),
10+
1011
request = require('request');
1112

1213
module.exports = function sender(options, cb, fail) {

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var http = require('http');
22
var util = require('util');
33
var stackify = require('./index');
4-
stackify.start({apiKey: '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr', env: 'dev'/*, proxy: 'http://74.50.126.249:8089'*/});
4+
stackify.start({apiKey: '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr', env: 'dev', proxy: 'http://89.22.50.155:3128'});
55
var stack = require('stack-trace');
66
var pkginfo = require('pkginfo')(module, 'name');
77
var express = require("express");
@@ -17,8 +17,8 @@ var foo = function foo() {
1717
foo();
1818

1919
setInterval(function () {
20-
stackify.info('test');
21-
}, 3500);
20+
stackify.error('test');
21+
}, 1500);
2222

2323
app.use(bodyParser.urlencoded({ extended: false }));
2424

0 commit comments

Comments
 (0)