observe-node 1.0.3 → 1.0.5

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 +91 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -41,36 +41,99 @@ function expressMiddleware(observe, opts = {}) {
41
41
  return _json(body);
42
42
  };
43
43
 
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) };
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") };
48
57
  return _send(body);
49
- };
58
+ }
59
+
60
+ // If Buffer, don't log full
61
+ if (Buffer.isBuffer(body)) {
62
+ capturedResponse = { truncated: true, bytes: body.length };
63
+ return _send(body);
64
+ }
65
+
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
+ }
71
+
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
+ const originalUrl = req.originalUrl || req.url || "";
84
+ const pathOnly = originalUrl.split("?")[0];
85
+ const queryString = originalUrl.includes("?") ? originalUrl.split("?")[1] : "";
86
+
87
+ // ✅ best effort matched route pattern
88
+ // baseUrl is mounted path, route.path is endpoint pattern (like "/:id")
89
+ const routePath = req.route?.path;
90
+ const matchedRoute =
91
+ routePath ? `${req.baseUrl || ""}${routePath}` : undefined;
92
+
93
+ // ✅ headers (safe subset)
94
+ const headers = {
95
+ "user-agent": req.headers["user-agent"],
96
+ "content-type": req.headers["content-type"],
97
+ "content-length": req.headers["content-length"],
98
+ "referer": req.headers["referer"],
99
+ };
100
+
101
+ // ✅ auth info (don’t log token)
102
+ const hasAuth = !!req.headers.authorization;
103
+
104
+ // ✅ body (only if parsed)
105
+ const requestBody = req.body ? redact(req.body) : undefined;
106
+
107
+ observe.emit({
108
+ event_name: "http.response",
109
+ level: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warn" : "info",
110
+ message: `${req.method} ${originalUrl} -> ${res.statusCode} (${duration_ms}ms)`,
111
+
112
+ http: {
113
+ method: req.method,
114
+ status: res.statusCode,
115
+ path: pathOnly,
116
+ original_url: originalUrl,
117
+ matched_route: matchedRoute, // ✅ tells exactly which API matched
118
+ duration_ms,
119
+ ip: req.ip,
120
+ },
121
+
122
+ request: {
123
+ headers,
124
+ has_auth: hasAuth,
125
+ query: req.query, // parsed query object
126
+ query_string: queryString, // raw query string
127
+ params: req.params, // may be empty (see note)
128
+ body: requestBody, // parsed body (if express.json ran before)
129
+ },
50
130
 
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
- });
131
+ response: {
132
+ status: res.statusCode,
133
+ ...capturedResponse,
134
+ },
135
+ });
136
+ });
74
137
 
75
138
  next();
76
139
  };