observe-node 1.0.0 → 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 CHANGED
@@ -1,11 +1,20 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
7
- "files": ["src"],
8
- "keywords": ["observability","logging","metrics","prometheus","loki","grafana"],
7
+ "files": [
8
+ "src"
9
+ ],
10
+ "keywords": [
11
+ "observability",
12
+ "logging",
13
+ "metrics",
14
+ "prometheus",
15
+ "loki",
16
+ "grafana"
17
+ ],
9
18
  "license": "MIT",
10
19
  "scripts": {
11
20
  "dev": "nodemon example.js"
@@ -17,4 +26,4 @@
17
26
  "devDependencies": {
18
27
  "nodemon": "^3.1.14"
19
28
  }
20
- }
29
+ }
@@ -1,63 +1,78 @@
1
1
  // src/http/express.js
2
- const { runWithContext } = require("../utils/context");
3
- const { newId } = require("../utils/ids");
4
-
5
- function getRoutePath(req) {
6
- // Prefer express route pattern if available (best for metrics + grouping)
7
- if (req.route && req.route.path) {
8
- const base = req.baseUrl || "";
9
- return `${base}${req.route.path}`;
2
+ function safeJsonSizeBytes(obj) {
3
+ try {
4
+ return Buffer.byteLength(JSON.stringify(obj), "utf8");
5
+ } catch {
6
+ return null;
10
7
  }
11
-
12
- // Fallback: originalUrl without querystring
13
- const raw = req.originalUrl || req.url || "";
14
- return raw.split("?")[0] || raw;
15
8
  }
16
9
 
17
- function expressMiddleware(observe) {
18
- const config = observe.__config;
19
-
20
- return function observeExpress(req, res, next) {
21
- const requestId =
22
- req.headers["x-request-id"] ||
23
- req.headers["x-correlation-id"] ||
24
- newId();
10
+ function redact(obj, keys = ["password","token","authorization","jwt","otp","secret","accessToken","refreshToken"]) {
11
+ if (!obj || typeof obj !== "object") return obj;
12
+ if (Array.isArray(obj)) return obj.map(v => redact(v, keys));
13
+ const out = {};
14
+ for (const k of Object.keys(obj)) {
15
+ if (keys.includes(k)) out[k] = "[REDACTED]";
16
+ else out[k] = redact(obj[k], keys);
17
+ }
18
+ return out;
19
+ }
25
20
 
26
- // expose id to downstream services
27
- res.setHeader("x-request-id", String(requestId));
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
- const start = process.hrtime.bigint();
30
+ function expressMiddleware(observe, opts = {}) {
31
+ const maxResponseBytes = Number(opts.maxResponseBytes || 20_000);
30
32
 
31
- runWithContext({ request_id: String(requestId) }, () => {
32
- res.on("finish", () => {
33
- const end = process.hrtime.bigint();
34
- const durationMs = Number(end - start) / 1_000_000;
33
+ return function (req, res, next) {
34
+ const start = Date.now();
35
35
 
36
- const status = res.statusCode;
37
- 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
+ };
38
43
 
39
- const path = getRoutePath(req);
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);
49
+ };
40
50
 
41
- config.emitEvent({
42
- event_name: "http.request",
43
- level,
44
- message: "HTTP request completed",
45
- http: {
46
- method: req.method,
47
- path,
48
- status,
49
- duration_ms: Math.round(durationMs),
50
- },
51
- });
51
+ res.on("finish", () => {
52
+ const duration_ms = Date.now() - start;
52
53
 
53
- // metrics hook (if enabled)
54
- if (typeof config._observeHttpMetric === "function") {
55
- config._observeHttpMetric(req.method, path, status, durationMs);
56
- }
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
+ },
57
72
  });
58
-
59
- next();
60
73
  });
74
+
75
+ next();
61
76
  };
62
77
  }
63
78
 
@@ -1,30 +1,39 @@
1
1
  // src/http/expressError.js
2
2
  function createExpressErrorMiddleware(observe, opts = {}) {
3
- const {
4
- includeStack = true,
5
- } = opts;
3
+ const {
4
+ includeStack = true,
5
+ } = opts;
6
6
 
7
- // eslint-disable-next-line no-unused-vars
8
- return function observeExpressError(err, req, res, next) {
9
- const status = err.status || err.statusCode || 500;
7
+ // eslint-disable-next-line no-unused-vars
8
+ return function observeExpressError(err, req, res, next) {
9
+ const status = err.status || err.statusCode || 500;
10
10
 
11
- // your apps may set these:
12
- const code = err.code; // e.g. "AUTH-401" / "WH-INV-001"
11
+ // your apps may set these:
12
+ const code = err.code; // e.g. "AUTH-401" / "WH-INV-001"
13
13
 
14
- observe.emit({
15
- event_name: "error",
16
- level: "error",
17
- message: err.message || "Unhandled error",
18
- error: {
19
- name: err.name || "Error",
20
- code,
21
- status,
22
- stack: includeStack ? err.stack : undefined,
23
- },
24
- });
14
+ observe.emit({
15
+ event_name: "http.error",
16
+ level: "error",
17
+ message: `${req.method} ${req.originalUrl} failed: ${err.message}`,
18
+ http: {
19
+ method: req.method,
20
+ path: (req.originalUrl || req.url || "").split("?")[0],
21
+ },
22
+ request: {
23
+ query: req.query,
24
+ params: req.params,
25
+ body: req.body,
26
+ },
27
+ error: {
28
+ name: err.name || "Error",
29
+ code: err.code,
30
+ status,
31
+ stack: includeStack ? err.stack : undefined,
32
+ },
33
+ });
25
34
 
26
- next(err);
27
- };
35
+ next(err);
36
+ };
28
37
  }
29
38
 
30
39
  module.exports = { createExpressErrorMiddleware };
package/src/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  // src/index.js
2
2
  const start = require("./start");
3
3
  const { expressMiddleware } = require("./http/express");
4
+ const { createExpressErrorMiddleware } = require("./http/expressError");
4
5
 
5
6
  module.exports = {
6
7
  start,
7
8
  expressMiddleware,
9
+ createExpressErrorMiddleware,
8
10
  };