tabletcommand-logger 2.0.5 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/build/bunyan-logger.js +6 -2
- package/build/bunyan-logger.js.map +1 -1
- package/build/middleware/metrics.js +25 -19
- package/build/middleware/metrics.js.map +1 -1
- package/build/middleware/status-logger.js +7 -3
- package/build/middleware/status-logger.js.map +1 -1
- package/build/test/middleware-metrics.js +35 -19
- package/build/test/middleware-metrics.js.map +1 -1
- package/definitions/bunyan-logger.d.ts +1 -1
- package/definitions/bunyan-logger.d.ts.map +1 -1
- package/definitions/middleware/metrics.d.ts +1 -0
- package/definitions/middleware/metrics.d.ts.map +1 -1
- package/definitions/middleware/status-logger.d.ts.map +1 -1
- package/package.json +21 -19
- package/src/bunyan-logger.ts +10 -3
- package/src/middleware/metrics.ts +24 -17
- package/src/middleware/status-logger.ts +15 -10
- package/src/test/middleware-metrics.ts +36 -18
package/README.md
ADDED
package/build/bunyan-logger.js
CHANGED
|
@@ -4,7 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const bunyan_1 = __importDefault(require("bunyan"));
|
|
7
|
-
function logger(name, filePath, logToConsole) {
|
|
7
|
+
function logger(name, filePath, logToConsole, logToFile) {
|
|
8
|
+
const loggingEnabled = logToConsole === true || logToFile === true;
|
|
9
|
+
if (!loggingEnabled) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
8
12
|
const streams = [];
|
|
9
13
|
if (logToConsole) {
|
|
10
14
|
const cfg = {
|
|
@@ -13,7 +17,7 @@ function logger(name, filePath, logToConsole) {
|
|
|
13
17
|
};
|
|
14
18
|
streams.push(cfg);
|
|
15
19
|
}
|
|
16
|
-
|
|
20
|
+
if (logToFile) {
|
|
17
21
|
const cfg = {
|
|
18
22
|
type: "rotating-file",
|
|
19
23
|
path: filePath,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bunyan-logger.js","sourceRoot":"","sources":["../src/bunyan-logger.ts"],"names":[],"mappings":";;;;;AAAA,oDAAwC;AAExC,SAAwB,MAAM,CAAC,IAAY,EAAE,QAAgB,EAAE,YAAqB;
|
|
1
|
+
{"version":3,"file":"bunyan-logger.js","sourceRoot":"","sources":["../src/bunyan-logger.ts"],"names":[],"mappings":";;;;;AAAA,oDAAwC;AAExC,SAAwB,MAAM,CAAC,IAAY,EAAE,QAAgB,EAAE,YAAqB,EAAE,SAAkB;IACtG,MAAM,cAAc,GAAG,YAAY,KAAK,IAAI,IAAI,SAAS,KAAK,IAAI,CAAC;IACnE,IAAI,CAAC,cAAc,EAAE;QACnB,OAAO,IAAI,CAAC;KACb;IAED,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,IAAI,YAAY,EAAE;QAChB,MAAM,GAAG,GAAW;YAClB,KAAK,EAAE,OAAO;YACd,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACnB;IACD,IAAI,SAAS,EAAE;QACb,MAAM,GAAG,GAAW;YAClB,IAAI,EAAE,eAAe;YACrB,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,IAAI;YACZ,KAAK,EAAE,EAAE;SACV,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACnB;IAED,MAAM,YAAY,GAAG,gBAAM,CAAC,YAAY,CAAC;QACvC,IAAI;QACJ,OAAO;KACR,CAAC,CAAC;IAEH,gCAAgC;IAChC,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;QACzB,YAAY,CAAC,iBAAiB,EAAE,CAAC;IACnC,CAAC,CAAC,CAAC;IAEH,OAAO,YAAY,CAAC;AACtB,CAAC;AAnCD,yBAmCC"}
|
|
@@ -8,11 +8,11 @@ const url_1 = require("url");
|
|
|
8
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
9
9
|
const debug_1 = __importDefault(require("debug"));
|
|
10
10
|
const express_statsd_1 = __importDefault(require("express-statsd"));
|
|
11
|
-
const monitorRequest = express_statsd_1.default();
|
|
11
|
+
const monitorRequest = (0, express_statsd_1.default)();
|
|
12
12
|
// Add an express-statsd key that looks like http.post.api.hello.world for a HTTP POST to /api/hello/world URL
|
|
13
13
|
// See https://github.com/uber/express-statsd
|
|
14
14
|
function metrics(filter) {
|
|
15
|
-
const debug = debug_1.default("tabletcommand-logger:middleware/metrics");
|
|
15
|
+
const debug = (0, debug_1.default)("tabletcommand-logger:middleware/metrics");
|
|
16
16
|
function defaultFilter(pathIn) {
|
|
17
17
|
const uuidRegex = /[-a-f\d]{36}/i;
|
|
18
18
|
const mongoIdRegex = /[a-f\d]{24}/i;
|
|
@@ -31,33 +31,39 @@ function metrics(filter) {
|
|
|
31
31
|
}
|
|
32
32
|
return path;
|
|
33
33
|
}
|
|
34
|
+
function setStatsDKey(req, hostname, env) {
|
|
35
|
+
let method = req.method || "unknown_method";
|
|
36
|
+
method = method.toLowerCase();
|
|
37
|
+
const defaultBase = "http://localhost";
|
|
38
|
+
const urlItem = new url_1.URL(req.url, defaultBase);
|
|
39
|
+
let path = urlItem.pathname.toLowerCase();
|
|
40
|
+
path = path.replace(/\//g, " ").trim().replace(/\s/g, ".");
|
|
41
|
+
let filterFunc = defaultFilter;
|
|
42
|
+
if (lodash_1.default.isFunction(filter)) {
|
|
43
|
+
filterFunc = filter;
|
|
44
|
+
}
|
|
45
|
+
const filteredPath = filterFunc(path);
|
|
46
|
+
req.statsdKey = [
|
|
47
|
+
hostname,
|
|
48
|
+
env,
|
|
49
|
+
"http",
|
|
50
|
+
method,
|
|
51
|
+
filteredPath
|
|
52
|
+
].join(".");
|
|
53
|
+
debug(`req.statsdKey: ${req.statsdKey}`);
|
|
54
|
+
}
|
|
34
55
|
function statsd() {
|
|
35
56
|
return function statsdFunc(req, res, next) {
|
|
36
57
|
const hostname = process.env.NODE_STATSD_PREFIX || os_1.default.hostname();
|
|
37
58
|
const env = process.env.NODE_ENV || "production";
|
|
38
|
-
|
|
39
|
-
method = method.toLowerCase();
|
|
40
|
-
let path = (new url_1.URL(req.url)).pathname.toLowerCase();
|
|
41
|
-
path = path.replace(/\//g, " ").trim().replace(/\s/g, ".");
|
|
42
|
-
let filterFunc = defaultFilter;
|
|
43
|
-
if (lodash_1.default.isFunction(filter)) {
|
|
44
|
-
filterFunc = filter;
|
|
45
|
-
}
|
|
46
|
-
const filteredPath = filterFunc(path);
|
|
47
|
-
req.statsdKey = [
|
|
48
|
-
hostname,
|
|
49
|
-
env,
|
|
50
|
-
"http",
|
|
51
|
-
method,
|
|
52
|
-
filteredPath
|
|
53
|
-
].join(".");
|
|
54
|
-
debug(`req.statsdKey: ${req.statsdKey}`);
|
|
59
|
+
setStatsDKey(req, hostname, env);
|
|
55
60
|
monitorRequest(req, res);
|
|
56
61
|
return next();
|
|
57
62
|
};
|
|
58
63
|
}
|
|
59
64
|
return {
|
|
60
65
|
defaultFilter,
|
|
66
|
+
setStatsDKey,
|
|
61
67
|
statsd,
|
|
62
68
|
};
|
|
63
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/middleware/metrics.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,6BAA0B;AAE1B,oDAAuB;AACvB,kDAAiC;AACjC,oEAA2C;AAK3C,MAAM,cAAc,GAAG,wBAAa,
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/middleware/metrics.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,6BAA0B;AAE1B,oDAAuB;AACvB,kDAAiC;AACjC,oEAA2C;AAK3C,MAAM,cAAc,GAAG,IAAA,wBAAa,GAAE,CAAC;AAEvC,8GAA8G;AAC9G,6CAA6C;AAC7C,SAAwB,OAAO,CAAC,MAAuB;IACrD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KAAK,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC3D,IAAI,UAAU,GAAG,aAAa,CAAC;QAC/B,IAAI,gBAAC,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;YACxB,UAAU,GAAG,MAAM,CAAC;SACrB;QACD,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;QACtC,GAAG,CAAC,SAAS,GAAG;YACd,QAAQ;YACR,GAAG;YACH,MAAM;YACN,MAAM;YACN,YAAY;SACb,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACZ,KAAK,CAAC,kBAAkB,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED,SAAS,MAAM;QACb,OAAO,SAAS,UAAU,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;YACxE,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,YAAE,CAAC,QAAQ,EAAE,CAAC;YACjE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,YAAY,CAAC;YACjD,YAAY,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;YACjC,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;YACzB,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC;IACJ,CAAC;IAED,OAAO;QACL,aAAa;QACb,YAAY;QACZ,MAAM;KACP,CAAC;AACJ,CAAC;AA3DD,0BA2DC"}
|
|
@@ -14,14 +14,17 @@ function shouldIgnore(req, res) {
|
|
|
14
14
|
}
|
|
15
15
|
return false;
|
|
16
16
|
}
|
|
17
|
+
function requestDuration(endTime, startTime) {
|
|
18
|
+
if (!lodash_1.default.isObject(startTime) || !lodash_1.default.isDate(startTime)) {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
return endTime.valueOf() - startTime.valueOf();
|
|
22
|
+
}
|
|
17
23
|
function statusLogger(logger) {
|
|
18
24
|
return function requestLogger(req, res, next) {
|
|
19
25
|
if (!lodash_1.default.isObject(req._startTime)) {
|
|
20
26
|
req._startTime = new Date();
|
|
21
27
|
}
|
|
22
|
-
if (lodash_1.default.isObject(req._startTime) && req._startTime instanceof Date) {
|
|
23
|
-
res.responseTime = new Date().valueOf() - req._startTime.valueOf();
|
|
24
|
-
}
|
|
25
28
|
res.bodyCopy = null;
|
|
26
29
|
// Save a copy of the response body
|
|
27
30
|
const oldJSON = res.json.bind(res);
|
|
@@ -39,6 +42,7 @@ function statusLogger(logger) {
|
|
|
39
42
|
res.bodyCopy = body;
|
|
40
43
|
}
|
|
41
44
|
oldJSON(body);
|
|
45
|
+
res.responseTime = requestDuration(new Date(), req._startTime);
|
|
42
46
|
return res;
|
|
43
47
|
};
|
|
44
48
|
function logRequest() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status-logger.js","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":";;;;;AAMA,oDAAuB;AAEvB,SAAS,YAAY,CAAC,GAAY,EAAE,GAAa;IAC/C,IAAI,gBAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,EAAE;QAC7C,OAAO,IAAI,CAAC;KACb;IAED,MAAM,SAAS,GAAY,gBAAC,CAAC,GAAG,CAAC,GAAG,EAAE,oBAAoB,CAAC,CAAC;IAC5D,IAAI,gBAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;QACvD,OAAO,IAAI,CAAC;KACb;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,
|
|
1
|
+
{"version":3,"file":"status-logger.js","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":";;;;;AAMA,oDAAuB;AAEvB,SAAS,YAAY,CAAC,GAAY,EAAE,GAAa;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,CAAC,EAAE;oBACV,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC;iBACrB;aACF;iBAAM;gBACL,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC;aACrB;YACD,OAAO,CAAC,IAAI,CAAC,CAAC;YACd,GAAG,CAAC,YAAY,GAAG,eAAe,CAAC,IAAI,IAAI,EAAE,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;YAC/D,OAAO,GAAG,CAAC;QACb,CAAC,CAAC;QAEF,SAAS,UAAU;YACjB,GAAG,CAAC,cAAc,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;YACzC,GAAG,CAAC,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAExC,IAAI,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE;gBAC1B,OAAO;aACR;YAED,MAAM,QAAQ,GAAG,gBAAC,CAAC,IAAI,CAAC,GAAG,EAAE;gBAC3B,MAAM;gBACN,eAAe;gBACf,SAAS;gBACT,aAAa;gBACb,QAAQ;gBACR,aAAa;gBACb,MAAM;gBACN,OAAO;aACR,CAAC,CAAC;YAEH,MAAM,QAAQ,GAAG,gBAAC,CAAC,IAAI,CAAC,GAAG,EAAE;gBAC3B,UAAU;gBACV,cAAc;gBACd,YAAY;aACb,CAAC,CAAC;YAEH,MAAM,CAAC,IAAI,CAAC;gBACV,GAAG,EAAE,QAAQ;gBACb,GAAG,EAAE,QAAQ;aACd,CAAC,CAAC;QACL,CAAC;QAED,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QAC7B,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;QAE5B,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AA/DD,+BA+DC"}
|
|
@@ -5,27 +5,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const chai_1 = require("chai");
|
|
7
7
|
const metrics_1 = __importDefault(require("../middleware/metrics"));
|
|
8
|
-
const metrics = metrics_1.default();
|
|
8
|
+
const metrics = (0, metrics_1.default)();
|
|
9
9
|
describe("Metrics Middleware", function () {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
context("Default Filter", () => {
|
|
11
|
+
it("removes uuid from end of paths", function () {
|
|
12
|
+
const path = "api.user.daafe605-feac-add0-ad0e-89023d48deab";
|
|
13
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
14
|
+
chai_1.assert.equal(cleanPath, "api.user");
|
|
15
|
+
});
|
|
16
|
+
it("removes uuid from middle of paths", function () {
|
|
17
|
+
const path = "api.user.daafe605-feac-add0-ad0e-89023d48deab.info";
|
|
18
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
19
|
+
chai_1.assert.equal(cleanPath, "api.user.info");
|
|
20
|
+
});
|
|
21
|
+
it("removes mongo id from middle of paths", function () {
|
|
22
|
+
const path = "api.online.515a41a3e0387575cc939002.status";
|
|
23
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
24
|
+
chai_1.assert.equal(cleanPath, "api.online.status");
|
|
25
|
+
});
|
|
26
|
+
it("skips paths without uuid", function () {
|
|
27
|
+
const path = "api.user.profile";
|
|
28
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
29
|
+
chai_1.assert.equal(cleanPath, "api.user.profile");
|
|
30
|
+
});
|
|
14
31
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
chai_1.assert.equal(cleanPath, "api.user.profile");
|
|
32
|
+
context("setStatsDKey", () => {
|
|
33
|
+
it("Parses URL without a set base hostname", () => {
|
|
34
|
+
const sampleUrl = "/api/v2/cad-vehicle-status?apikey=somekey";
|
|
35
|
+
const req = {
|
|
36
|
+
method: "POST",
|
|
37
|
+
url: sampleUrl,
|
|
38
|
+
};
|
|
39
|
+
const hostname = "a";
|
|
40
|
+
const env = "b";
|
|
41
|
+
metrics.setStatsDKey(req, hostname, env);
|
|
42
|
+
const expectedStatsdKey = "a.b.http.post.api.v2.cad-vehicle-status";
|
|
43
|
+
chai_1.assert.equal(req.statsdKey, expectedStatsdKey);
|
|
44
|
+
});
|
|
29
45
|
});
|
|
30
46
|
});
|
|
31
47
|
//# sourceMappingURL=middleware-metrics.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware-metrics.js","sourceRoot":"","sources":["../../src/test/middleware-metrics.ts"],"names":[],"mappings":";;;;;AAAA,+BAA8B;
|
|
1
|
+
{"version":3,"file":"middleware-metrics.js","sourceRoot":"","sources":["../../src/test/middleware-metrics.ts"],"names":[],"mappings":";;;;;AAAA,+BAA8B;AAE9B,oEAAkD;AAClD,MAAM,OAAO,GAAG,IAAA,iBAAa,GAAE,CAAC;AAEhC,QAAQ,CAAC,oBAAoB,EAAE;IAC7B,OAAO,CAAC,gBAAgB,EAAE,GAAG,EAAE;QAC7B,EAAE,CAAC,gCAAgC,EAAE;YACnC,MAAM,IAAI,GAAG,+CAA+C,CAAC;YAC7D,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YAC9C,aAAM,CAAC,KAAK,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,mCAAmC,EAAE;YACtC,MAAM,IAAI,GAAG,oDAAoD,CAAC;YAClE,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YAC9C,aAAM,CAAC,KAAK,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,uCAAuC,EAAE;YAC1C,MAAM,IAAI,GAAG,4CAA4C,CAAC;YAC1D,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YAC9C,aAAM,CAAC,KAAK,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAC;QAC/C,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,0BAA0B,EAAE;YAC7B,MAAM,IAAI,GAAG,kBAAkB,CAAC;YAChC,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YAC9C,aAAM,CAAC,KAAK,CAAC,SAAS,EAAE,kBAAkB,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,cAAc,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;YAChD,MAAM,SAAS,GAAG,2CAA2C,CAAC;YAC9D,MAAM,GAAG,GAAqB;gBAC5B,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,SAAS;aACf,CAAC;YACF,MAAM,QAAQ,GAAG,GAAG,CAAC;YACrB,MAAM,GAAG,GAAG,GAAG,CAAC;YAChB,OAAO,CAAC,YAAY,CAAC,GAAc,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;YACpD,MAAM,iBAAiB,GAAG,yCAAyC,CAAC;YACpE,aAAM,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import bunyan from "bunyan";
|
|
2
|
-
export default function logger(name: string, filePath: string, logToConsole: boolean): bunyan;
|
|
2
|
+
export default function logger(name: string, filePath: string, logToConsole: boolean, logToFile: boolean): bunyan | null;
|
|
3
3
|
export declare type LoggerModule = ReturnType<typeof logger>;
|
|
4
4
|
//# sourceMappingURL=bunyan-logger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bunyan-logger.d.ts","sourceRoot":"","sources":["../src/bunyan-logger.ts"],"names":[],"mappings":"AAAA,OAAO,MAAkB,MAAM,QAAQ,CAAC;AAExC,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"bunyan-logger.d.ts","sourceRoot":"","sources":["../src/bunyan-logger.ts"],"names":[],"mappings":"AAAA,OAAO,MAAkB,MAAM,QAAQ,CAAC;AAExC,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAmCvH;AAED,oBAAY,YAAY,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC"}
|
|
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from "express";
|
|
|
2
2
|
declare type FilterFunction = (parameter: string) => string;
|
|
3
3
|
export default function metrics(filter?: FilterFunction): {
|
|
4
4
|
defaultFilter: (pathIn: string) => string;
|
|
5
|
+
setStatsDKey: (req: Request, hostname: string, env: string) => void;
|
|
5
6
|
statsd: () => (req: Request, res: Response, next: NextFunction) => void;
|
|
6
7
|
};
|
|
7
8
|
export declare type MetricsModule = ReturnType<typeof metrics>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/middleware/metrics.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1D,aAAK,cAAc,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC;AAMpD,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,MAAM,CAAC,EAAE,cAAc;4BAGtB,MAAM,KAAG,MAAM;
|
|
1
|
+
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/middleware/metrics.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1D,aAAK,cAAc,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC;AAMpD,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,MAAM,CAAC,EAAE,cAAc;4BAGtB,MAAM,KAAG,MAAM;wBAmBnB,OAAO,YAAY,MAAM,OAAO,MAAM;wBAuB/B,OAAO,OAAO,QAAQ,QAAQ,YAAY;EAc7E;AAED,oBAAY,aAAa,GAAG,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status-logger.d.ts","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"status-logger.d.ts","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;AAwBjB,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,EAAE,MAAM,SACd,OAAO,OAAO,QAAQ,QAAQ,YAAY,UA8D9E;AAED,oBAAY,kBAAkB,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tabletcommand-logger",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Tablet Command Logging Configuration",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "gulp ts",
|
|
@@ -17,36 +17,38 @@
|
|
|
17
17
|
"types": "definitions/index.d.ts",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"bunyan": "^1.8.15",
|
|
20
|
-
"debug": "^4.3.
|
|
20
|
+
"debug": "^4.3.4",
|
|
21
21
|
"express-statsd": "^0.3.0",
|
|
22
22
|
"lodash": "^4.17.21"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/bunyan": "^1.8.
|
|
26
|
-
"@types/chai": "^4.
|
|
27
|
-
"@types/debug": "4.1.
|
|
28
|
-
"@types/express": "^4.17.
|
|
25
|
+
"@types/bunyan": "^1.8.8",
|
|
26
|
+
"@types/chai": "^4.3.3",
|
|
27
|
+
"@types/debug": "4.1.7",
|
|
28
|
+
"@types/express": "^4.17.13",
|
|
29
|
+
"@types/express-serve-static-core": "^4.17.30",
|
|
29
30
|
"@types/gulp-copy": "0.0.4",
|
|
30
|
-
"@types/lodash": "^4.14.
|
|
31
|
-
"@types/mocha": "^
|
|
32
|
-
"@types/node": "^
|
|
33
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
34
|
-
"@typescript-eslint/parser": "^
|
|
35
|
-
"chai": "^4.3.
|
|
36
|
-
"del": "^6.
|
|
37
|
-
"eslint": "^
|
|
38
|
-
"eslint-config-standard": "^
|
|
39
|
-
"eslint-plugin-import": "^2.
|
|
31
|
+
"@types/lodash": "^4.14.184",
|
|
32
|
+
"@types/mocha": "^9.1.1",
|
|
33
|
+
"@types/node": "^18.7.15",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^5.36.2",
|
|
35
|
+
"@typescript-eslint/parser": "^5.36.2",
|
|
36
|
+
"chai": "^4.3.6",
|
|
37
|
+
"del": "^6.1.1",
|
|
38
|
+
"eslint": "^8.23.0",
|
|
39
|
+
"eslint-config-standard": "^17.0.0",
|
|
40
|
+
"eslint-plugin-import": "^2.26.0",
|
|
40
41
|
"eslint-plugin-mocha-only": "0.0.3",
|
|
41
42
|
"eslint-plugin-node": "^11.1.0",
|
|
42
|
-
"eslint-plugin-promise": "^
|
|
43
|
-
"eslint-plugin-security": "^1.
|
|
43
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
44
|
+
"eslint-plugin-security": "^1.5.0",
|
|
44
45
|
"eslint-plugin-standard": "^4.1.0",
|
|
45
46
|
"gulp": "^4.0.2",
|
|
47
|
+
"gulp-cli": "^2.3.0",
|
|
46
48
|
"gulp-copy": "^4.0.1",
|
|
47
49
|
"gulp-eslint": "^6.0.0",
|
|
48
50
|
"gulp-mocha": "^8.0.0",
|
|
49
51
|
"gulp-shell": "^0.8.0",
|
|
50
|
-
"mocha": "^
|
|
52
|
+
"mocha": "^10.0.0"
|
|
51
53
|
}
|
|
52
54
|
}
|
package/src/bunyan-logger.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import bunyan, { Stream } from "bunyan";
|
|
2
2
|
|
|
3
|
-
export default function logger(name: string, filePath: string, logToConsole: boolean): bunyan {
|
|
3
|
+
export default function logger(name: string, filePath: string, logToConsole: boolean, logToFile: boolean): bunyan | null {
|
|
4
|
+
const loggingEnabled = logToConsole === true || logToFile === true;
|
|
5
|
+
if (!loggingEnabled) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
const streams: Stream[] = [];
|
|
5
10
|
if (logToConsole) {
|
|
6
11
|
const cfg: Stream = {
|
|
@@ -8,7 +13,8 @@ export default function logger(name: string, filePath: string, logToConsole: boo
|
|
|
8
13
|
stream: process.stdout,
|
|
9
14
|
};
|
|
10
15
|
streams.push(cfg);
|
|
11
|
-
}
|
|
16
|
+
}
|
|
17
|
+
if (logToFile) {
|
|
12
18
|
const cfg: Stream = {
|
|
13
19
|
type: "rotating-file",
|
|
14
20
|
path: filePath,
|
|
@@ -17,11 +23,12 @@ export default function logger(name: string, filePath: string, logToConsole: boo
|
|
|
17
23
|
};
|
|
18
24
|
streams.push(cfg);
|
|
19
25
|
}
|
|
26
|
+
|
|
20
27
|
const loggerClient = bunyan.createLogger({
|
|
21
28
|
name,
|
|
22
29
|
streams,
|
|
23
30
|
});
|
|
24
|
-
|
|
31
|
+
|
|
25
32
|
// Reopen file streams on signal
|
|
26
33
|
process.on("SIGUSR2", () => {
|
|
27
34
|
loggerClient.reopenFileStreams();
|
|
@@ -34,27 +34,33 @@ export default function metrics(filter?: FilterFunction) {
|
|
|
34
34
|
return path;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
function setStatsDKey(req: Request, hostname: string, env: string) {
|
|
38
|
+
let method = req.method || "unknown_method";
|
|
39
|
+
method = method.toLowerCase();
|
|
40
|
+
const defaultBase = "http://localhost";
|
|
41
|
+
const urlItem = new URL(req.url, defaultBase);
|
|
42
|
+
let path = urlItem.pathname.toLowerCase();
|
|
43
|
+
path = path.replace(/\//g, " ").trim().replace(/\s/g, ".");
|
|
44
|
+
let filterFunc = defaultFilter;
|
|
45
|
+
if (_.isFunction(filter)) {
|
|
46
|
+
filterFunc = filter;
|
|
47
|
+
}
|
|
48
|
+
const filteredPath = filterFunc(path);
|
|
49
|
+
req.statsdKey = [
|
|
50
|
+
hostname,
|
|
51
|
+
env,
|
|
52
|
+
"http",
|
|
53
|
+
method,
|
|
54
|
+
filteredPath
|
|
55
|
+
].join(".");
|
|
56
|
+
debug(`req.statsdKey: ${req.statsdKey}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
37
59
|
function statsd() {
|
|
38
60
|
return function statsdFunc(req: Request, res: Response, next: NextFunction) {
|
|
39
61
|
const hostname = process.env.NODE_STATSD_PREFIX || os.hostname();
|
|
40
62
|
const env = process.env.NODE_ENV || "production";
|
|
41
|
-
|
|
42
|
-
method = method.toLowerCase();
|
|
43
|
-
let path = (new URL(req.url)).pathname.toLowerCase();
|
|
44
|
-
path = path.replace(/\//g, " ").trim().replace(/\s/g, ".");
|
|
45
|
-
let filterFunc = defaultFilter;
|
|
46
|
-
if (_.isFunction(filter)) {
|
|
47
|
-
filterFunc = filter;
|
|
48
|
-
}
|
|
49
|
-
const filteredPath = filterFunc(path);
|
|
50
|
-
req.statsdKey = [
|
|
51
|
-
hostname,
|
|
52
|
-
env,
|
|
53
|
-
"http",
|
|
54
|
-
method,
|
|
55
|
-
filteredPath
|
|
56
|
-
].join(".");
|
|
57
|
-
debug(`req.statsdKey: ${req.statsdKey}`);
|
|
63
|
+
setStatsDKey(req, hostname, env);
|
|
58
64
|
monitorRequest(req, res);
|
|
59
65
|
return next();
|
|
60
66
|
};
|
|
@@ -62,6 +68,7 @@ export default function metrics(filter?: FilterFunction) {
|
|
|
62
68
|
|
|
63
69
|
return {
|
|
64
70
|
defaultFilter,
|
|
71
|
+
setStatsDKey,
|
|
65
72
|
statsd,
|
|
66
73
|
};
|
|
67
74
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import bunyan from "bunyan";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
3
|
NextFunction,
|
|
4
|
-
Request,
|
|
5
|
-
Response,
|
|
4
|
+
Request,
|
|
5
|
+
Response,
|
|
6
6
|
} from "express";
|
|
7
7
|
import _ from "lodash";
|
|
8
8
|
|
|
@@ -19,33 +19,38 @@ function shouldIgnore(req: Request, res: Response): boolean {
|
|
|
19
19
|
return false;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
function requestDuration(endTime: Date, startTime?: Date): number {
|
|
23
|
+
if (!_.isObject(startTime) || !_.isDate(startTime)) {
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return endTime.valueOf() - startTime.valueOf();
|
|
28
|
+
}
|
|
29
|
+
|
|
22
30
|
export default function statusLogger(logger: bunyan) {
|
|
23
31
|
return function requestLogger(req: Request, res: Response, next: NextFunction) {
|
|
24
32
|
if (!_.isObject(req._startTime)) {
|
|
25
33
|
req._startTime = new Date();
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
if (_.isObject(req._startTime) && req._startTime instanceof Date) {
|
|
29
|
-
res.responseTime = new Date().valueOf() - req._startTime.valueOf();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
36
|
res.bodyCopy = null;
|
|
33
37
|
|
|
34
38
|
// Save a copy of the response body
|
|
35
39
|
const oldJSON = res.json.bind(res);
|
|
36
40
|
|
|
37
41
|
// Used when using res.json(body);
|
|
38
|
-
res.json = function newJson(body: unknown|string): Response {
|
|
42
|
+
res.json = function newJson(body: unknown | string): Response {
|
|
39
43
|
if (_.isString(body)) {
|
|
40
44
|
try {
|
|
41
45
|
res.bodyCopy = JSON.parse(body);
|
|
42
46
|
} catch (e) {
|
|
43
47
|
res.bodyCopy = body;
|
|
44
|
-
}
|
|
48
|
+
}
|
|
45
49
|
} else {
|
|
46
50
|
res.bodyCopy = body;
|
|
47
51
|
}
|
|
48
52
|
oldJSON(body);
|
|
53
|
+
res.responseTime = requestDuration(new Date(), req._startTime);
|
|
49
54
|
return res;
|
|
50
55
|
};
|
|
51
56
|
|
|
@@ -82,7 +87,7 @@ export default function statusLogger(logger: bunyan) {
|
|
|
82
87
|
|
|
83
88
|
res.on("finish", logRequest);
|
|
84
89
|
res.on("close", logRequest);
|
|
85
|
-
|
|
90
|
+
|
|
86
91
|
return next();
|
|
87
92
|
};
|
|
88
93
|
}
|
|
@@ -1,29 +1,47 @@
|
|
|
1
1
|
import { assert } from "chai";
|
|
2
|
+
import { Request } from "express";
|
|
2
3
|
import MetricsModule from "../middleware/metrics";
|
|
3
4
|
const metrics = MetricsModule();
|
|
4
5
|
|
|
5
6
|
describe("Metrics Middleware", function() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
context("Default Filter", () => {
|
|
8
|
+
it("removes uuid from end of paths", function() {
|
|
9
|
+
const path = "api.user.daafe605-feac-add0-ad0e-89023d48deab";
|
|
10
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
11
|
+
assert.equal(cleanPath, "api.user");
|
|
12
|
+
});
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
it("removes uuid from middle of paths", function() {
|
|
15
|
+
const path = "api.user.daafe605-feac-add0-ad0e-89023d48deab.info";
|
|
16
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
17
|
+
assert.equal(cleanPath, "api.user.info");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("removes mongo id from middle of paths", function() {
|
|
21
|
+
const path = "api.online.515a41a3e0387575cc939002.status";
|
|
22
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
23
|
+
assert.equal(cleanPath, "api.online.status");
|
|
24
|
+
});
|
|
17
25
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
it("skips paths without uuid", function() {
|
|
27
|
+
const path = "api.user.profile";
|
|
28
|
+
const cleanPath = metrics.defaultFilter(path);
|
|
29
|
+
assert.equal(cleanPath, "api.user.profile");
|
|
30
|
+
});
|
|
22
31
|
});
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
context("setStatsDKey", () => {
|
|
34
|
+
it("Parses URL without a set base hostname", () => {
|
|
35
|
+
const sampleUrl = "/api/v2/cad-vehicle-status?apikey=somekey";
|
|
36
|
+
const req: Partial<Request> = {
|
|
37
|
+
method: "POST",
|
|
38
|
+
url: sampleUrl,
|
|
39
|
+
};
|
|
40
|
+
const hostname = "a";
|
|
41
|
+
const env = "b";
|
|
42
|
+
metrics.setStatsDKey(req as Request, hostname, env);
|
|
43
|
+
const expectedStatsdKey = "a.b.http.post.api.v2.cad-vehicle-status";
|
|
44
|
+
assert.equal(req.statsdKey, expectedStatsdKey);
|
|
45
|
+
});
|
|
28
46
|
});
|
|
29
47
|
});
|