observe-node 1.0.4 → 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 +57 -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.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",
@@ -77,29 +77,63 @@ res.send = (body) => {
77
77
  }
78
78
  };
79
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
- });
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
+ },
130
+
131
+ response: {
132
+ status: res.statusCode,
133
+ ...capturedResponse,
134
+ },
135
+ });
136
+ });
103
137
 
104
138
  next();
105
139
  };