observe-node 1.0.0 → 1.0.3
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 +62 -47
- 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.3",
|
|
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
|
@@ -1,63 +1,78 @@
|
|
|
1
1
|
// src/http/express.js
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (req.route && req.route.path) {
|
|
8
|
-
const base = req.baseUrl || "";
|
|
9
|
-
return `${base}${req.route.path}`;
|
|
2
|
+
function safeJsonSizeBytes(obj) {
|
|
3
|
+
try {
|
|
4
|
+
return Buffer.byteLength(JSON.stringify(obj), "utf8");
|
|
5
|
+
} catch {
|
|
6
|
+
return null;
|
|
10
7
|
}
|
|
11
|
-
|
|
12
|
-
// Fallback: originalUrl without querystring
|
|
13
|
-
const raw = req.originalUrl || req.url || "";
|
|
14
|
-
return raw.split("?")[0] || raw;
|
|
15
8
|
}
|
|
16
9
|
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
10
|
+
function redact(obj, keys = ["password","token","authorization","jwt","otp","secret","accessToken","refreshToken"]) {
|
|
11
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
12
|
+
if (Array.isArray(obj)) return obj.map(v => redact(v, keys));
|
|
13
|
+
const out = {};
|
|
14
|
+
for (const k of Object.keys(obj)) {
|
|
15
|
+
if (keys.includes(k)) out[k] = "[REDACTED]";
|
|
16
|
+
else out[k] = redact(obj[k], keys);
|
|
17
|
+
}
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
25
20
|
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
function pickResponseBody(body, maxBytes = 20_000) {
|
|
22
|
+
// limit response size (avoid huge payloads / images)
|
|
23
|
+
const redacted = redact(body);
|
|
24
|
+
const bytes = safeJsonSizeBytes(redacted);
|
|
25
|
+
if (bytes == null) return { truncated: true, bytes: null };
|
|
26
|
+
if (bytes > maxBytes) return { truncated: true, bytes };
|
|
27
|
+
return { body: redacted, truncated: false, bytes };
|
|
28
|
+
}
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
function expressMiddleware(observe, opts = {}) {
|
|
31
|
+
const maxResponseBytes = Number(opts.maxResponseBytes || 20_000);
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const end = process.hrtime.bigint();
|
|
34
|
-
const durationMs = Number(end - start) / 1_000_000;
|
|
33
|
+
return function (req, res, next) {
|
|
34
|
+
const start = Date.now();
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
// ✅ capture response body
|
|
37
|
+
let capturedResponse;
|
|
38
|
+
const _json = res.json.bind(res);
|
|
39
|
+
res.json = (body) => {
|
|
40
|
+
capturedResponse = pickResponseBody(body, maxResponseBytes);
|
|
41
|
+
return _json(body);
|
|
42
|
+
};
|
|
38
43
|
|
|
39
|
-
|
|
44
|
+
const _send = res.send.bind(res);
|
|
45
|
+
res.send = (body) => {
|
|
46
|
+
// if body is string/buffer, avoid logging full
|
|
47
|
+
capturedResponse = { truncated: true, bytes: Buffer.isBuffer(body) ? body.length : (typeof body === "string" ? Buffer.byteLength(body) : null) };
|
|
48
|
+
return _send(body);
|
|
49
|
+
};
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
});
|
|
51
|
+
res.on("finish", () => {
|
|
52
|
+
const duration_ms = Date.now() - start;
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
54
|
+
observe.emit({
|
|
55
|
+
event_name: "http.response",
|
|
56
|
+
level: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warn" : "info",
|
|
57
|
+
message: `${req.method} ${req.originalUrl} -> ${res.statusCode} (${duration_ms}ms)`,
|
|
58
|
+
http: {
|
|
59
|
+
method: req.method,
|
|
60
|
+
path: (req.originalUrl || req.url || "").split("?")[0],
|
|
61
|
+
status: res.statusCode,
|
|
62
|
+
duration_ms,
|
|
63
|
+
},
|
|
64
|
+
request: {
|
|
65
|
+
query: req.query,
|
|
66
|
+
params: req.params,
|
|
67
|
+
},
|
|
68
|
+
response: {
|
|
69
|
+
status: res.statusCode,
|
|
70
|
+
...capturedResponse, // { body?, truncated, bytes }
|
|
71
|
+
},
|
|
57
72
|
});
|
|
58
|
-
|
|
59
|
-
next();
|
|
60
73
|
});
|
|
74
|
+
|
|
75
|
+
next();
|
|
61
76
|
};
|
|
62
77
|
}
|
|
63
78
|
|
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
|
};
|