observe-node 1.0.2 → 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 +1 -1
- package/src/http/express.js +57 -64
package/package.json
CHANGED
package/src/http/express.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// src/http/express.js
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
function safeJsonSizeBytes(obj) {
|
|
3
|
+
try {
|
|
4
|
+
return Buffer.byteLength(JSON.stringify(obj), "utf8");
|
|
5
|
+
} catch {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
5
9
|
|
|
6
10
|
function redact(obj, keys = ["password","token","authorization","jwt","otp","secret","accessToken","refreshToken"]) {
|
|
7
11
|
if (!obj || typeof obj !== "object") return obj;
|
|
@@ -14,73 +18,62 @@ function redact(obj, keys = ["password","token","authorization","jwt","otp","sec
|
|
|
14
18
|
return out;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Fallback: originalUrl without querystring
|
|
26
|
-
const raw = req.originalUrl || req.url || "";
|
|
27
|
-
return raw.split("?")[0] || raw;
|
|
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) {
|
|
31
|
-
|
|
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();
|
|
38
|
-
|
|
39
|
-
// expose id to downstream services
|
|
40
|
-
res.setHeader("x-request-id", String(requestId));
|
|
30
|
+
function expressMiddleware(observe, opts = {}) {
|
|
31
|
+
const maxResponseBytes = Number(opts.maxResponseBytes || 20_000);
|
|
41
32
|
|
|
42
|
-
|
|
33
|
+
return function (req, res, next) {
|
|
34
|
+
const start = Date.now();
|
|
43
35
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
});
|
|
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
|
+
};
|
|
80
43
|
|
|
81
|
-
|
|
82
|
-
|
|
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);
|
|
83
49
|
};
|
|
50
|
+
|
|
51
|
+
res.on("finish", () => {
|
|
52
|
+
const duration_ms = Date.now() - start;
|
|
53
|
+
|
|
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
|
+
},
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
next();
|
|
76
|
+
};
|
|
84
77
|
}
|
|
85
78
|
|
|
86
79
|
module.exports = { expressMiddleware };
|