observe-node 1.0.4 → 1.0.6

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 +69 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -18,6 +18,16 @@ function redact(obj, keys = ["password","token","authorization","jwt","otp","sec
18
18
  return out;
19
19
  }
20
20
 
21
+ function pickRequestBody(body, maxBytes = 20_000) {
22
+ if (!body || typeof body !== "object") return undefined;
23
+
24
+ const redacted = redact(body);
25
+ const bytes = safeJsonSizeBytes(redacted);
26
+ if (bytes == null) return { truncated: true, bytes: null };
27
+ if (bytes > maxBytes) return { truncated: true, bytes };
28
+ return { body: redacted, truncated: false, bytes };
29
+ }
30
+
21
31
  function pickResponseBody(body, maxBytes = 20_000) {
22
32
  // limit response size (avoid huge payloads / images)
23
33
  const redacted = redact(body);
@@ -29,6 +39,7 @@ function pickResponseBody(body, maxBytes = 20_000) {
29
39
 
30
40
  function expressMiddleware(observe, opts = {}) {
31
41
  const maxResponseBytes = Number(opts.maxResponseBytes || 20_000);
42
+ const maxRequestBytes = Number(opts.maxRequestBytes || 20_000);
32
43
 
33
44
  return function (req, res, next) {
34
45
  const start = Date.now();
@@ -77,29 +88,64 @@ res.send = (body) => {
77
88
  }
78
89
  };
79
90
 
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
- });
91
+ res.on("finish", () => {
92
+ const duration_ms = Date.now() - start;
93
+
94
+ const originalUrl = req.originalUrl || req.url || "";
95
+ const pathOnly = originalUrl.split("?")[0];
96
+ const queryString = originalUrl.includes("?") ? originalUrl.split("?")[1] : "";
97
+
98
+ // ✅ best effort matched route pattern
99
+ // baseUrl is mounted path, route.path is endpoint pattern (like "/:id")
100
+ const routePath = req.route?.path;
101
+ const matchedRoute =
102
+ routePath ? `${req.baseUrl || ""}${routePath}` : undefined;
103
+
104
+ // ✅ headers (safe subset)
105
+ const headers = {
106
+ "user-agent": req.headers["user-agent"],
107
+ "content-type": req.headers["content-type"],
108
+ "content-length": req.headers["content-length"],
109
+ "referer": req.headers["referer"],
110
+ };
111
+
112
+ // ✅ auth info (don’t log token)
113
+ const hasAuth = !!req.headers.authorization;
114
+
115
+ // ✅ body (only if parsed)
116
+ const requestBody = req.body ? redact(req.body) : undefined;
117
+
118
+ observe.emit({
119
+ event_name: "http.response",
120
+ level: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warn" : "info",
121
+ message: `${req.method} ${originalUrl} -> ${res.statusCode} (${duration_ms}ms)`,
122
+
123
+ http: {
124
+ method: req.method,
125
+ status: res.statusCode,
126
+ path: pathOnly,
127
+ original_url: originalUrl,
128
+ matched_route: matchedRoute, // ✅ tells exactly which API matched
129
+ duration_ms,
130
+ ip: req.ip,
131
+ },
132
+
133
+ request: {
134
+ headers,
135
+ has_auth: hasAuth,
136
+ query: req.query, // parsed query object
137
+ query_string: queryString, // raw query string
138
+ params: req.params, // may be empty (see note)
139
+ body: requestBody, // parsed body (if express.json ran before)
140
+ bodyPayload: capturedRequest,
141
+ },
142
+
143
+ response: {
144
+ status: res.statusCode,
145
+ ...capturedResponse,
146
+ },
147
+ });
148
+ });
103
149
 
104
150
  next();
105
151
  };