Skip to content

Commit df69b5b

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
work in progress
1 parent 83d52e3 commit df69b5b

File tree

11 files changed

+352
-116
lines changed

11 files changed

+352
-116
lines changed

config/config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
'HOST': 'dev.stackify.com',
3+
'PORT': 443,
4+
'IDENTIFY_PATH': '/API/Metrics/IdentifyApp',
5+
'LOG_SAVE_PATH': '/API/Log/Save',
6+
'LICENSE_KEY': '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr',
7+
'MSG_LIMIT': 20, // number of messages
8+
'SCAN_TIMER': 30, // sec
9+
'REQUEST_TIMER': 30 * 1000, // sec
10+
'REQUEST_ATTEMPTS': 20, // number of attempts if API call isn't succesful
11+
'ERROR_FLOOD_LIMIT': 100, // limit of the same error message per minute
12+
'COOKIE_MASK': 'X-MASKED-X'
13+
};

index.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
var util = require('util'),
2-
access = require('./lib/access'),
3-
logger = require('./lib/logger'),
4-
api = require('./lib/api'),
5-
CONFIG = require('./lib/config'),
6-
sender = require('./sender');
1+
var api = require('./lib/api'),
2+
CONFIG = require('./config/config'),
3+
error = require('./lib/error'),
4+
exc = require('./lib/exception'),
5+
helpers = require('./lib/helpers'),
6+
logger = require('./lib/logger'),
7+
sender = require('./lib/sender');
78

8-
module.exports = {
9-
CONFIG: CONFIG,
9+
module.exports = function (options) {
1010

11-
start: function () {
12-
13-
},
11+
api.identifyApp(options);
12+
exc.exc();
1413

15-
log: logger.methods.log,
16-
debug: logger.methods.debug,
17-
info: logger.methods.info,
18-
warn: logger.methods.warn,
19-
error: logger.methods.error,
20-
postLogs: api.postLogs
21-
}
14+
return {
15+
storage: logger.storage,
16+
CONFIG: CONFIG,
17+
18+
log: logger.methods.log,
19+
debug: logger.methods.debug,
20+
info: logger.methods.info,
21+
warn: logger.methods.warn,
22+
error: logger.methods.error,
23+
24+
postLogs: api.postLogs,
25+
26+
checkError: error.checkError,
27+
28+
excCaught: exc.excCaught,
29+
excHandler: exc.exc,
30+
expressExcHandler: exc.expressExc
31+
};
32+
};

lib/api.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
11
var os = require('os'),
22
send = require('./sender'),
3-
access = require('.access'),
43
logger = require('./logger'),
5-
CONFIG = require('./config'),
4+
CONFIG = require('../config/config'),
65
pkginfo = require('pkginfo')(module, 'name'),
76

87
options = {
9-
host: CONFIG.HOST,
8+
hostname: CONFIG.HOST,
109
port: CONFIG.PORT,
1110
method: 'POST',
11+
secureProtocol: 'SSLv3_method',
1212
headers: {
13-
Content-Type: 'application/json',
14-
X-Stackify-Key: CONFIG.LICENSE_KEY,
15-
X-Stackify-PV: 'V1'
13+
'Content-Type': 'application/json',
14+
'X-Stackify-Key': CONFIG.LICENSE_KEY,
15+
'X-Stackify-PV': 'V1'
1616
}
17-
}
17+
};
1818

1919
module.exports = {
2020

21-
identifyApp: function identifyApp (licenseKey) {
22-
var options = options,
21+
identifyApp: function identifyApp(settings) {
22+
var opt = options,
2323
data = {
2424
"DeviceName": os.hostname(),
2525
"AppName": module.exports.name
2626
},
27-
callback = function(data) {
27+
callback = function (data) {
2828
CONFIG.APP_DETAILS = data.toJSON();
29+
CONFIG.LICENSE_KEY = settings.license_key;
2930
},
30-
fail = function() {
31-
setTimeout(function() {
32-
identifyApp(licenseKey)
33-
}, CONFIG.REQUEST_TIMER)
34-
}
35-
36-
options.path = CONFIG.IDENTIFY_PATH;
31+
fail = function () {
32+
setTimeout(function () {
33+
send(options, data, callback, fail);
34+
}, CONFIG.REQUEST_TIMER);
35+
};
36+
opt.path = CONFIG.IDENTIFY_PATH;
37+
38+
if (typeof (settings.license_key) === 'string') {
39+
opt.headers['X-Stackify-Key'] = settings.license_key;
40+
send(options, data, callback, fail);
41+
} else {
42+
throw new TypeError('License key is not defined or has a wrong format');
43+
}
3744

38-
send(options, data, callback, fail);
39-
};
45+
},
4046

4147
postLogs: function postLogs(messages, cb, fail) {
42-
var options = options,
43-
callback = cb || function(data) {
44-
logger.storage = logger.storage.slice(CONFIG.MSG_LIMIT)
45-
},
46-
fail = function() {
47-
setTimeout(function() {
48-
postLogs(messages, callback, fail)
49-
}, CONFIG.REQUEST_TIMER)
50-
},
48+
var opt = options,
5149
data = CONFIG.APP_DETAILS;
5250

5351
data.Msgs = messages;
54-
options.path = CONFIG.LOG_SAVE_PATH;
52+
opt.path = CONFIG.LOG_SAVE_PATH;
5553

56-
send(options, data, callback, fail);
57-
};
58-
}
54+
send(options, data, cb, fail);
55+
}
56+
};

lib/config.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/error.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
var url = require('url'),
2+
stackTrace = require('stack-trace'),
3+
helpers = require('./helpers'),
4+
CONFIG = require('../config/config'),
5+
6+
getCookies = function getCookies(req) {
7+
var key,
8+
keys,
9+
cookies = req.cookies,
10+
result = {};
11+
if (cookies) {
12+
if (Object.keys(cookies).length) {
13+
for (key in cookies) {
14+
if (cookies.hasOwnProperty(key)) {
15+
result[key] = CONFIG.COOKIE_MASK;
16+
}
17+
}
18+
}
19+
} else if (req.headers.cookie) {
20+
keys = req.headers.cookie.split('; ');
21+
keys.forEach(function (elem) {
22+
var parts = elem.split('=');
23+
result[parts[0]] = CONFIG.COOKIE_MASK;
24+
});
25+
}
26+
27+
return result;
28+
},
29+
30+
getPostData = function getPostData(req) {
31+
var result = {};
32+
if (req.body instanceof Object) {
33+
result = req.body;
34+
}
35+
return result;
36+
};
37+
38+
module.exports = {
39+
formatEx : function formatEx(err, req) {
40+
var trace = this.parseError(err),
41+
ex = {
42+
OccuredEpochMillis: Date.now(),
43+
Error: {
44+
Message: err.method,
45+
ErrorType: err.name,
46+
SourceMethod: trace[0],
47+
StackTrace: trace,
48+
InnerError: null
49+
},
50+
EnvironmentDetail: {
51+
AppName: CONFIG.APP_DETAILS.AppName,
52+
AppNameID: CONFIG.APP_DETAILS.AppNameID,
53+
EnvID: CONFIG.APP_DETAILS.EnvID,
54+
AppEnvID: CONFIG.APP_DETAILS.AppEnvID,
55+
AppLocation: process.env.PWD,
56+
},
57+
ServerVariables: process.env
58+
},
59+
headers = req.headers,
60+
qs,
61+
key;
62+
63+
if (req) {
64+
qs = url.parse(req.url, true);
65+
66+
if (req.headers.cookie) {
67+
headers = {};
68+
for (key in req.headers) {
69+
if (req.headers.hasOwnProperty(key)) {
70+
headers[key] = req.headers[key];
71+
}
72+
}
73+
delete headers.cookie;
74+
}
75+
ex.WebRequestDetail = {
76+
UserIPAddress : req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress ||
77+
req.connection.socket.remoteAddress,
78+
HttpMethod: req.protocol,
79+
RequestProtocol: req.method,
80+
RequestUrl: req.url,
81+
RequestUrlRoot: qs.pathname,
82+
ReferralUrl: req.headers.referer,
83+
Headers: headers,
84+
Cookies: getCookies(req),
85+
QueryString: qs.query,
86+
PostData: getPostData(req)
87+
};
88+
}
89+
90+
return ex;
91+
},
92+
93+
checkError : function checkError(obj) {
94+
var meta = obj.meta,
95+
metaIsValid = helpers.checkMeta(meta),
96+
err;
97+
98+
if (metaIsValid && metaIsValid[1] instanceof Error) {
99+
err = metaIsValid[1];
100+
obj.Ex = this.formatEx(err);
101+
delete obj.meta;
102+
}
103+
104+
return obj;
105+
}
106+
};

lib/exception.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var qs = require('querystring'),
2+
error = require('./error'),
3+
logger = require('./logger'),
4+
api = require('./api'),
5+
CONFIG = require('../config/config'),
6+
handler = function handler(err, req, cb) {
7+
var rec = {
8+
Msg: err.method + ': ' + err.name,
9+
Level: 'ERROR',
10+
EpochMs: new Date().toUTCString(),
11+
Ex: error.formatEx(err, req)
12+
},
13+
fail_counter = 0,
14+
fail = function () {
15+
fail_counter += 1;
16+
if (fail_counter < CONFIG.REQUEST_ATTEMPTS) {
17+
setTimeout(function () {
18+
api.postLogs(logger.storage, null, fail);
19+
}, CONFIG.REQUEST_TIMER);
20+
}
21+
};
22+
23+
if (CONFIG.APP_DETAILS) {
24+
logger.storage.push(rec);
25+
api.postLogs(logger.storage, cb, fail);
26+
}
27+
},
28+
excCaught = false;
29+
30+
module.exports = {
31+
excCaught: excCaught,
32+
exc : function exc(req) {
33+
return function() {
34+
var body = '';
35+
if (req) {
36+
req.on('data', function (chunk) {
37+
body += chunk;
38+
});
39+
req.on('end', function () {
40+
var json = qs.parse(body);
41+
req.body = json;
42+
});
43+
}
44+
45+
process.on('uncaughtException', function (err) {
46+
if (!excCaught) {
47+
excCaught = true;
48+
handler(err, req);
49+
}
50+
});
51+
};
52+
},
53+
54+
expressExc : function expressExc(err, req, res, next) {
55+
var cb = function () {
56+
next(err);
57+
};
58+
59+
if (!err) {
60+
return next();
61+
}
62+
63+
if (!excCaught) {
64+
excCaught = true;
65+
handler(err, req, cb);
66+
}
67+
68+
next(err);
69+
}
70+
};

lib/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports.checkMeta = function checkMeta(meta) {
2+
if (Object.prototype.toString.call(meta) === '[object Object]') {
3+
if (Object.keys(meta).length === 1) {
4+
try {
5+
JSON.stringify(meta);
6+
} catch (e) {
7+
return false;
8+
}
9+
return [Object.keys(meta)[0], meta[Object.keys(meta)[0]]];
10+
}
11+
}
12+
return false;
13+
};

0 commit comments

Comments
 (0)