@easyweb/logging 1.0.2 → 1.1.0

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/README.md CHANGED
@@ -115,3 +115,28 @@ operator-create refusal, and a Google registration — and they are the signup
115
115
  audit trail: `userId` alone cannot answer "which address did they sign up
116
116
  with". The exposure is real and bounded by log retention. See
117
117
  `docs/platform/ADR-0001`.
118
+
119
+ ## Tracing (OpenTelemetry)
120
+
121
+ `@easyweb/logging/register` starts the OpenTelemetry SDK on import. Make it the
122
+ **first** import of a service's entry file, before anything that loads `http`,
123
+ `express`, `pg`, `ioredis`, `amqplib`, `undici` or `pino`:
124
+
125
+ ```ts
126
+ import "@easyweb/logging/register";
127
+ ```
128
+
129
+ The instrumentations patch those modules as they are required, so a module
130
+ loaded earlier is never traced. Pino lines gain `trace_id` and `span_id` next to
131
+ the existing `requestId`.
132
+
133
+ It is inert unless configured: it starts only when `OTEL_EXPORTER_OTLP_ENDPOINT`
134
+ or `OTEL_TRACES_EXPORTER` is set and non-empty, and `OTEL_SDK_DISABLED=true`
135
+ always wins. The service name is `OTEL_SERVICE_NAME`, else `SERVICE_NAME`, else
136
+ `unknown-service`. Exporter, endpoint and sampler come from the standard
137
+ `OTEL_*` variables. `/health`, `/livez` and `/api/heartbeat` are not traced.
138
+ Note that `OTEL_TRACES_EXPORTER=none` registers no tracer provider, so spans are
139
+ non-recording and carry no `trace_id`.
140
+
141
+ `npm run check:telemetry` builds and proves the `trace_id` injection against
142
+ `dist/`; it cannot be a jest test because jest bypasses the require hooks.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=register.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const telemetry_1 = require("./telemetry");
4
+ (0, telemetry_1.createTelemetrySdk)(process.env)?.start();
5
+ // Must be the FIRST import of a service entry file. The instrumentations patch
6
+ // http, express, pg, ioredis, amqplib, undici and pino by hooking require, so
7
+ // any of those loaded before this line is never traced. Do not import ./logger
8
+ // or ./index here for the same reason.
9
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":";;AAAA,2CAAiD;AAEjD,IAAA,8BAAkB,EAAC,OAAO,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC;AAEzC,+EAA+E;AAC/E,8EAA8E;AAC9E,+EAA+E;AAC/E,uCAAuC"}
@@ -0,0 +1,8 @@
1
+ import { NodeSDK } from "@opentelemetry/sdk-node";
2
+ import type { Instrumentation } from "@opentelemetry/instrumentation";
3
+ export declare function shouldStartTelemetry(env: NodeJS.ProcessEnv): boolean;
4
+ export declare function telemetryServiceName(env: NodeJS.ProcessEnv): string;
5
+ export declare function isProbePath(url: string | undefined): boolean;
6
+ export declare function telemetryInstrumentations(): Instrumentation[];
7
+ export declare function createTelemetrySdk(env: NodeJS.ProcessEnv): NodeSDK | null;
8
+ //# sourceMappingURL=telemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AActE,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,OAAO,CAGpE;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,MAAM,CAEnE;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAI5D;AAED,wBAAgB,yBAAyB,IAAI,eAAe,EAAE,CAU7D;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,OAAO,GAAG,IAAI,CAQzE"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldStartTelemetry = shouldStartTelemetry;
4
+ exports.telemetryServiceName = telemetryServiceName;
5
+ exports.isProbePath = isProbePath;
6
+ exports.telemetryInstrumentations = telemetryInstrumentations;
7
+ exports.createTelemetrySdk = createTelemetrySdk;
8
+ const sdk_node_1 = require("@opentelemetry/sdk-node");
9
+ const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
10
+ const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
11
+ const instrumentation_undici_1 = require("@opentelemetry/instrumentation-undici");
12
+ const instrumentation_pg_1 = require("@opentelemetry/instrumentation-pg");
13
+ const instrumentation_ioredis_1 = require("@opentelemetry/instrumentation-ioredis");
14
+ const instrumentation_amqplib_1 = require("@opentelemetry/instrumentation-amqplib");
15
+ const instrumentation_pino_1 = require("@opentelemetry/instrumentation-pino");
16
+ // Deliberately imports nothing from ./logger or ./index: this file must load
17
+ // before pino, http, pg, ... so the SDK can patch them as they are required.
18
+ const PROBE_PATHS = new Set(["/health", "/livez", "/api/heartbeat"]);
19
+ function shouldStartTelemetry(env) {
20
+ if (env["OTEL_SDK_DISABLED"] === "true")
21
+ return false;
22
+ return Boolean(env["OTEL_EXPORTER_OTLP_ENDPOINT"] || env["OTEL_TRACES_EXPORTER"]);
23
+ }
24
+ function telemetryServiceName(env) {
25
+ return env["OTEL_SERVICE_NAME"] || env["SERVICE_NAME"] || "unknown-service";
26
+ }
27
+ function isProbePath(url) {
28
+ if (!url)
29
+ return false;
30
+ const path = url.split("?")[0] ?? "";
31
+ return PROBE_PATHS.has(path);
32
+ }
33
+ function telemetryInstrumentations() {
34
+ return [
35
+ new instrumentation_http_1.HttpInstrumentation({ ignoreIncomingRequestHook: (req) => isProbePath(req.url) }),
36
+ new instrumentation_express_1.ExpressInstrumentation(),
37
+ new instrumentation_undici_1.UndiciInstrumentation(),
38
+ new instrumentation_pg_1.PgInstrumentation(),
39
+ new instrumentation_ioredis_1.IORedisInstrumentation(),
40
+ new instrumentation_amqplib_1.AmqplibInstrumentation(),
41
+ new instrumentation_pino_1.PinoInstrumentation(),
42
+ ];
43
+ }
44
+ function createTelemetrySdk(env) {
45
+ if (!shouldStartTelemetry(env))
46
+ return null;
47
+ // No traceExporter: OTEL_TRACES_EXPORTER, OTEL_EXPORTER_OTLP_* and
48
+ // OTEL_TRACES_SAMPLER* are read from the environment by the SDK.
49
+ return new sdk_node_1.NodeSDK({
50
+ serviceName: telemetryServiceName(env),
51
+ instrumentations: telemetryInstrumentations(),
52
+ });
53
+ }
54
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":";;AAeA,oDAGC;AAED,oDAEC;AAED,kCAIC;AAED,8DAUC;AAED,gDAQC;AAlDD,sDAAkD;AAElD,8EAA0E;AAC1E,oFAAgF;AAChF,kFAA8E;AAC9E,0EAAsE;AACtE,oFAAgF;AAChF,oFAAgF;AAChF,8EAA0E;AAE1E,6EAA6E;AAC7E,6EAA6E;AAE7E,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC;AAErE,SAAgB,oBAAoB,CAAC,GAAsB;IACzD,IAAI,GAAG,CAAC,mBAAmB,CAAC,KAAK,MAAM;QAAE,OAAO,KAAK,CAAC;IACtD,OAAO,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,IAAI,GAAG,CAAC,sBAAsB,CAAC,CAAC,CAAC;AACpF,CAAC;AAED,SAAgB,oBAAoB,CAAC,GAAsB;IACzD,OAAO,GAAG,CAAC,mBAAmB,CAAC,IAAI,GAAG,CAAC,cAAc,CAAC,IAAI,iBAAiB,CAAC;AAC9E,CAAC;AAED,SAAgB,WAAW,CAAC,GAAuB;IACjD,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACrC,OAAO,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC/B,CAAC;AAED,SAAgB,yBAAyB;IACvC,OAAO;QACL,IAAI,0CAAmB,CAAC,EAAE,yBAAyB,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QACrF,IAAI,gDAAsB,EAAE;QAC5B,IAAI,8CAAqB,EAAE;QAC3B,IAAI,sCAAiB,EAAE;QACvB,IAAI,gDAAsB,EAAE;QAC5B,IAAI,gDAAsB,EAAE;QAC5B,IAAI,0CAAmB,EAAE;KAC1B,CAAC;AACJ,CAAC;AAED,SAAgB,kBAAkB,CAAC,GAAsB;IACvD,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5C,mEAAmE;IACnE,iEAAiE;IACjE,OAAO,IAAI,kBAAO,CAAC;QACjB,WAAW,EAAE,oBAAoB,CAAC,GAAG,CAAC;QACtC,gBAAgB,EAAE,yBAAyB,EAAE;KAC9C,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyweb/logging",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Shared structured logging for Easyweb microservices: one pino configuration, request-scoped context over AsyncLocalStorage, an HTTP access log, and the redaction every service needs before its logs leave the pod",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,6 +9,11 @@
9
9
  "require": "./dist/index.js",
10
10
  "import": "./dist/index.js",
11
11
  "types": "./dist/index.d.ts"
12
+ },
13
+ "./register": {
14
+ "require": "./dist/register.js",
15
+ "import": "./dist/register.js",
16
+ "types": "./dist/register.d.ts"
12
17
  }
13
18
  },
14
19
  "files": [
@@ -22,7 +27,8 @@
22
27
  "clean": "rimraf dist",
23
28
  "test": "jest",
24
29
  "typecheck": "tsc --noEmit",
25
- "typecheck:test": "tsc -p tsconfig.test.json --noEmit"
30
+ "typecheck:test": "tsc -p tsconfig.test.json --noEmit",
31
+ "check:telemetry": "npm run build && node scripts/check-telemetry.js"
26
32
  },
27
33
  "keywords": [
28
34
  "microservices",
@@ -55,9 +61,21 @@
55
61
  "typescript": "^6.0.3"
56
62
  },
57
63
  "engines": {
58
- "node": ">=18.0.0"
64
+ "node": ">=22.15.0"
59
65
  },
60
66
  "publishConfig": {
61
67
  "access": "public"
68
+ },
69
+ "dependencies": {
70
+ "@opentelemetry/api": "^1.10.0",
71
+ "@opentelemetry/instrumentation": "^0.301.2",
72
+ "@opentelemetry/instrumentation-amqplib": "^0.72.0",
73
+ "@opentelemetry/instrumentation-express": "^0.73.0",
74
+ "@opentelemetry/instrumentation-http": "^0.301.2",
75
+ "@opentelemetry/instrumentation-ioredis": "^0.73.0",
76
+ "@opentelemetry/instrumentation-pg": "^0.78.0",
77
+ "@opentelemetry/instrumentation-pino": "^0.71.0",
78
+ "@opentelemetry/instrumentation-undici": "^0.35.1",
79
+ "@opentelemetry/sdk-node": "^0.301.2"
62
80
  }
63
81
  }