observe-node 1.0.2 → 1.0.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/http/express.js +84 -62
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -1,7 +1,11 @@
1
1
  // src/http/express.js
2
- const { runWithContext } = require("../utils/context");
3
- const { newId } = require("../utils/ids");
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,91 @@ function redact(obj, keys = ["password","token","authorization","jwt","otp","sec
14
18
  return out;
15
19
  }
16
20
 
17
-
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;
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
- 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();
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
- const start = process.hrtime.bigint();
33
+ return function (req, res, next) {
34
+ const start = Date.now();
43
35
 
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";
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
+ };
51
43
 
52
- const path = getRoutePath(req);
44
+ const _send = res.send.bind(res);
45
+ res.send = (body) => {
46
+ try {
47
+ // If body is a JSON string, parse it and store full object
48
+ if (typeof body === "string") {
49
+ const trimmed = body.trim();
50
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
51
+ const parsed = JSON.parse(trimmed);
52
+ capturedResponse = pickResponseBody(parsed, maxResponseBytes);
53
+ return _send(body);
54
+ }
55
+ // non-json string => don't log full
56
+ capturedResponse = { truncated: true, bytes: Buffer.byteLength(body, "utf8") };
57
+ return _send(body);
58
+ }
53
59
 
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
- });
60
+ // If Buffer, don't log full
61
+ if (Buffer.isBuffer(body)) {
62
+ capturedResponse = { truncated: true, bytes: body.length };
63
+ return _send(body);
64
+ }
74
65
 
75
- // metrics hook (if enabled)
76
- if (typeof config._observeHttpMetric === "function") {
77
- config._observeHttpMetric(req.method, path, status, durationMs);
78
- }
79
- });
66
+ // If object (sometimes people do res.send({}))
67
+ if (typeof body === "object" && body !== null) {
68
+ capturedResponse = pickResponseBody(body, maxResponseBytes);
69
+ return _send(body);
70
+ }
80
71
 
81
- next();
82
- });
83
- };
72
+ capturedResponse = { truncated: true, bytes: null };
73
+ return _send(body);
74
+ } catch (e) {
75
+ capturedResponse = { truncated: true, bytes: null, parse_error: true };
76
+ return _send(body);
77
+ }
78
+ };
79
+
80
+ res.on("finish", () => {
81
+ const duration_ms = Date.now() - start;
82
+
83
+ observe.emit({
84
+ event_name: "http.response",
85
+ level: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warn" : "info",
86
+ message: `${req.method} ${req.originalUrl} -> ${res.statusCode} (${duration_ms}ms)`,
87
+ http: {
88
+ method: req.method,
89
+ path: (req.originalUrl || req.url || "").split("?")[0],
90
+ status: res.statusCode,
91
+ duration_ms,
92
+ },
93
+ request: {
94
+ query: req.query,
95
+ params: req.params,
96
+ },
97
+ response: {
98
+ status: res.statusCode,
99
+ ...capturedResponse, // { body?, truncated, bytes }
100
+ },
101
+ });
102
+ });
103
+
104
+ next();
105
+ };
84
106
  }
85
107
 
86
108
  module.exports = { expressMiddleware };