observe-node 1.0.0 → 1.0.2
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/package.json +13 -4
- package/src/http/express.js +72 -50
- package/src/http/expressError.js +30 -21
- package/src/index.js +2 -0
package/package.json
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "observe-node",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
7
|
-
"files": [
|
|
8
|
-
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"observability",
|
|
12
|
+
"logging",
|
|
13
|
+
"metrics",
|
|
14
|
+
"prometheus",
|
|
15
|
+
"loki",
|
|
16
|
+
"grafana"
|
|
17
|
+
],
|
|
9
18
|
"license": "MIT",
|
|
10
19
|
"scripts": {
|
|
11
20
|
"dev": "nodemon example.js"
|
|
@@ -17,4 +26,4 @@
|
|
|
17
26
|
"devDependencies": {
|
|
18
27
|
"nodemon": "^3.1.14"
|
|
19
28
|
}
|
|
20
|
-
}
|
|
29
|
+
}
|
package/src/http/express.js
CHANGED
|
@@ -2,63 +2,85 @@
|
|
|
2
2
|
const { runWithContext } = require("../utils/context");
|
|
3
3
|
const { newId } = require("../utils/ids");
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
|
|
6
|
+
function redact(obj, keys = ["password","token","authorization","jwt","otp","secret","accessToken","refreshToken"]) {
|
|
7
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
8
|
+
if (Array.isArray(obj)) return obj.map(v => redact(v, keys));
|
|
9
|
+
const out = {};
|
|
10
|
+
for (const k of Object.keys(obj)) {
|
|
11
|
+
if (keys.includes(k)) out[k] = "[REDACTED]";
|
|
12
|
+
else out[k] = redact(obj[k], keys);
|
|
10
13
|
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
function getRoutePath(req) {
|
|
19
|
+
// Prefer express route pattern if available (best for metrics + grouping)
|
|
20
|
+
if (req.route && req.route.path) {
|
|
21
|
+
const base = req.baseUrl || "";
|
|
22
|
+
return `${base}${req.route.path}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Fallback: originalUrl without querystring
|
|
26
|
+
const raw = req.originalUrl || req.url || "";
|
|
27
|
+
return raw.split("?")[0] || raw;
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
function expressMiddleware(observe) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// expose id to downstream services
|
|
27
|
-
res.setHeader("x-request-id", String(requestId));
|
|
28
|
-
|
|
29
|
-
const start = process.hrtime.bigint();
|
|
30
|
-
|
|
31
|
-
runWithContext({ request_id: String(requestId) }, () => {
|
|
32
|
-
res.on("finish", () => {
|
|
33
|
-
const end = process.hrtime.bigint();
|
|
34
|
-
const durationMs = Number(end - start) / 1_000_000;
|
|
35
|
-
|
|
36
|
-
const status = res.statusCode;
|
|
37
|
-
const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
|
|
38
|
-
|
|
39
|
-
const path = getRoutePath(req);
|
|
40
|
-
|
|
41
|
-
config.emitEvent({
|
|
42
|
-
event_name: "http.request",
|
|
43
|
-
level,
|
|
44
|
-
message: "HTTP request completed",
|
|
45
|
-
http: {
|
|
46
|
-
method: req.method,
|
|
47
|
-
path,
|
|
48
|
-
status,
|
|
49
|
-
duration_ms: Math.round(durationMs),
|
|
50
|
-
},
|
|
51
|
-
});
|
|
31
|
+
const config = observe.__config;
|
|
32
|
+
|
|
33
|
+
return function observeExpress(req, res, next) {
|
|
34
|
+
const requestId =
|
|
35
|
+
req.headers["x-request-id"] ||
|
|
36
|
+
req.headers["x-correlation-id"] ||
|
|
37
|
+
newId();
|
|
52
38
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
config._observeHttpMetric(req.method, path, status, durationMs);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
39
|
+
// expose id to downstream services
|
|
40
|
+
res.setHeader("x-request-id", String(requestId));
|
|
58
41
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
const start = process.hrtime.bigint();
|
|
43
|
+
|
|
44
|
+
runWithContext({ request_id: String(requestId) }, () => {
|
|
45
|
+
res.on("finish", () => {
|
|
46
|
+
const end = process.hrtime.bigint();
|
|
47
|
+
const durationMs = Number(end - start) / 1_000_000;
|
|
48
|
+
|
|
49
|
+
const status = res.statusCode;
|
|
50
|
+
const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
|
|
51
|
+
|
|
52
|
+
const path = getRoutePath(req);
|
|
53
|
+
|
|
54
|
+
config.emitEvent({
|
|
55
|
+
event_name: "http.request",
|
|
56
|
+
level,
|
|
57
|
+
message: `${req.method} ${path} -> ${status} (${Math.round(durationMs)}ms)`,
|
|
58
|
+
http: {
|
|
59
|
+
method: req.method,
|
|
60
|
+
path,
|
|
61
|
+
original_url: (req.originalUrl || req.url || "").split("?")[0],
|
|
62
|
+
status,
|
|
63
|
+
duration_ms: Math.round(durationMs),
|
|
64
|
+
ip: req.ip,
|
|
65
|
+
user_agent: req.headers["user-agent"],
|
|
66
|
+
},
|
|
67
|
+
request: {
|
|
68
|
+
query: req.query,
|
|
69
|
+
params: req.params,
|
|
70
|
+
// ⚠️ body is optional (see below)
|
|
71
|
+
body: redact(req.body),
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// metrics hook (if enabled)
|
|
76
|
+
if (typeof config._observeHttpMetric === "function") {
|
|
77
|
+
config._observeHttpMetric(req.method, path, status, durationMs);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
next();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
62
84
|
}
|
|
63
85
|
|
|
64
86
|
module.exports = { expressMiddleware };
|
package/src/http/expressError.js
CHANGED
|
@@ -1,30 +1,39 @@
|
|
|
1
1
|
// src/http/expressError.js
|
|
2
2
|
function createExpressErrorMiddleware(observe, opts = {}) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const {
|
|
4
|
+
includeStack = true,
|
|
5
|
+
} = opts;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
// eslint-disable-next-line no-unused-vars
|
|
8
|
+
return function observeExpressError(err, req, res, next) {
|
|
9
|
+
const status = err.status || err.statusCode || 500;
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
// your apps may set these:
|
|
12
|
+
const code = err.code; // e.g. "AUTH-401" / "WH-INV-001"
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
observe.emit({
|
|
15
|
+
event_name: "http.error",
|
|
16
|
+
level: "error",
|
|
17
|
+
message: `${req.method} ${req.originalUrl} failed: ${err.message}`,
|
|
18
|
+
http: {
|
|
19
|
+
method: req.method,
|
|
20
|
+
path: (req.originalUrl || req.url || "").split("?")[0],
|
|
21
|
+
},
|
|
22
|
+
request: {
|
|
23
|
+
query: req.query,
|
|
24
|
+
params: req.params,
|
|
25
|
+
body: req.body,
|
|
26
|
+
},
|
|
27
|
+
error: {
|
|
28
|
+
name: err.name || "Error",
|
|
29
|
+
code: err.code,
|
|
30
|
+
status,
|
|
31
|
+
stack: includeStack ? err.stack : undefined,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
next(err);
|
|
36
|
+
};
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
module.exports = { createExpressErrorMiddleware };
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// src/index.js
|
|
2
2
|
const start = require("./start");
|
|
3
3
|
const { expressMiddleware } = require("./http/express");
|
|
4
|
+
const { createExpressErrorMiddleware } = require("./http/expressError");
|
|
4
5
|
|
|
5
6
|
module.exports = {
|
|
6
7
|
start,
|
|
7
8
|
expressMiddleware,
|
|
9
|
+
createExpressErrorMiddleware,
|
|
8
10
|
};
|