@easyweb/logging 1.1.0 → 1.1.1
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 +12 -4
- package/dist/telemetry.d.ts +3 -1
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +82 -17
- package/dist/telemetry.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -133,10 +133,18 @@ the existing `requestId`.
|
|
|
133
133
|
It is inert unless configured: it starts only when `OTEL_EXPORTER_OTLP_ENDPOINT`
|
|
134
134
|
or `OTEL_TRACES_EXPORTER` is set and non-empty, and `OTEL_SDK_DISABLED=true`
|
|
135
135
|
always wins. The service name is `OTEL_SERVICE_NAME`, else `SERVICE_NAME`, else
|
|
136
|
-
`unknown-service`.
|
|
137
|
-
|
|
136
|
+
`unknown-service`. `OTEL_*` variables are read when the process starts, before
|
|
137
|
+
dotenv loads a service's `.env`, so setting them there has no effect: put them
|
|
138
|
+
on the command line or in the pod env. Exporter, endpoint and sampler come from the standard
|
|
139
|
+
`OTEL_*` variables. `/health`, `/livez` and `/api/heartbeat` are not traced. Only traces are exported (no OTLP logs or
|
|
140
|
+
metrics), query-string values are redacted in span attributes for the
|
|
141
|
+
instrumentation libraries' own default keys (`sig`, `Signature`, `AWSAccessKeyId`,
|
|
142
|
+
`X-Goog-Signature`, `X-Amz-Signature`, `X-Amz-Credential`, `X-Amz-Security-Token`)
|
|
143
|
+
plus `token`, `code`, `state` and `refresh_token`; keys are percent-decoded before
|
|
144
|
+
matching but matched exact-case, and a token in the URL path is not redacted. Redis statements carry the command name only, and `pg`,
|
|
145
|
+
`undici` and outgoing `http` make no span outside a request.
|
|
138
146
|
Note that `OTEL_TRACES_EXPORTER=none` registers no tracer provider, so spans are
|
|
139
147
|
non-recording and carry no `trace_id`.
|
|
140
148
|
|
|
141
|
-
`npm run check:telemetry` builds and proves the `trace_id` injection
|
|
142
|
-
`dist/`; it cannot be a jest test because jest bypasses the require hooks.
|
|
149
|
+
`npm run check:telemetry` builds and proves the `trace_id` injection and the
|
|
150
|
+
traces-only, redacted export against `dist/`; it cannot be a jest test because jest bypasses the require hooks.
|
package/dist/telemetry.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
1
|
+
import type { NodeSDK } from "@opentelemetry/sdk-node";
|
|
2
2
|
import type { Instrumentation } from "@opentelemetry/instrumentation";
|
|
3
3
|
export declare function shouldStartTelemetry(env: NodeJS.ProcessEnv): boolean;
|
|
4
4
|
export declare function telemetryServiceName(env: NodeJS.ProcessEnv): string;
|
|
5
5
|
export declare function isProbePath(url: string | undefined): boolean;
|
|
6
|
+
export declare const REDACTED_QUERY_PARAMS: string[];
|
|
7
|
+
export declare function redactQuery(query: string): string;
|
|
6
8
|
export declare function telemetryInstrumentations(): Instrumentation[];
|
|
7
9
|
export declare function createTelemetrySdk(env: NodeJS.ProcessEnv): NodeSDK | null;
|
|
8
10
|
//# sourceMappingURL=telemetry.d.ts.map
|
package/dist/telemetry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAUtE,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;AAMD,eAAO,MAAM,qBAAqB,UAGjC,CAAC;AAUF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CASjD;AAED,wBAAgB,yBAAyB,IAAI,eAAe,EAAE,CA+C7D;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,OAAO,GAAG,IAAI,CAazE"}
|
package/dist/telemetry.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REDACTED_QUERY_PARAMS = void 0;
|
|
3
4
|
exports.shouldStartTelemetry = shouldStartTelemetry;
|
|
4
5
|
exports.telemetryServiceName = telemetryServiceName;
|
|
5
6
|
exports.isProbePath = isProbePath;
|
|
7
|
+
exports.redactQuery = redactQuery;
|
|
6
8
|
exports.telemetryInstrumentations = telemetryInstrumentations;
|
|
7
9
|
exports.createTelemetrySdk = createTelemetrySdk;
|
|
8
|
-
|
|
9
|
-
|
|
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");
|
|
10
|
+
// The SDK and instrumentation packages are require()d lazily, after the gate:
|
|
11
|
+
// importing them costs ~100 ms and ~36 MB per process even when telemetry is off.
|
|
16
12
|
// Deliberately imports nothing from ./logger or ./index: this file must load
|
|
17
13
|
// before pino, http, pg, ... so the SDK can patch them as they are required.
|
|
18
14
|
const PROBE_PATHS = new Set(["/health", "/livez", "/api/heartbeat"]);
|
|
@@ -30,25 +26,94 @@ function isProbePath(url) {
|
|
|
30
26
|
const path = url.split("?")[0] ?? "";
|
|
31
27
|
return PROBE_PATHS.has(path);
|
|
32
28
|
}
|
|
29
|
+
// Superset of the installed instrumentation-http and instrumentation-undici default
|
|
30
|
+
// lists (passing the option REPLACES the library default, so they are repeated here;
|
|
31
|
+
// a test fails when an upgrade adds a key) plus our own. Matched exactly,
|
|
32
|
+
// case-sensitive, like the library matchers.
|
|
33
|
+
exports.REDACTED_QUERY_PARAMS = [
|
|
34
|
+
"sig", "Signature", "AWSAccessKeyId", "X-Goog-Signature", "X-Amz-Signature", "X-Amz-Credential", "X-Amz-Security-Token",
|
|
35
|
+
"token", "code", "state", "refresh_token",
|
|
36
|
+
];
|
|
37
|
+
function decodeKey(k) {
|
|
38
|
+
try {
|
|
39
|
+
return decodeURIComponent(k.replace(/\+/g, " "));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return k;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function redactQuery(query) {
|
|
46
|
+
const q = query.startsWith("?") ? query.slice(1) : query;
|
|
47
|
+
return q
|
|
48
|
+
.split("&")
|
|
49
|
+
.map((pair) => {
|
|
50
|
+
const [k = ""] = pair.split("=");
|
|
51
|
+
return exports.REDACTED_QUERY_PARAMS.includes(decodeKey(k)) ? `${k}=REDACTED` : pair;
|
|
52
|
+
})
|
|
53
|
+
.join("&");
|
|
54
|
+
}
|
|
33
55
|
function telemetryInstrumentations() {
|
|
56
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
57
|
+
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
|
|
58
|
+
const { ExpressInstrumentation } = require("@opentelemetry/instrumentation-express");
|
|
59
|
+
const { UndiciInstrumentation } = require("@opentelemetry/instrumentation-undici");
|
|
60
|
+
const { PgInstrumentation } = require("@opentelemetry/instrumentation-pg");
|
|
61
|
+
const { IORedisInstrumentation } = require("@opentelemetry/instrumentation-ioredis");
|
|
62
|
+
const { AmqplibInstrumentation } = require("@opentelemetry/instrumentation-amqplib");
|
|
63
|
+
const { PinoInstrumentation } = require("@opentelemetry/instrumentation-pino");
|
|
64
|
+
/* eslint-enable @typescript-eslint/no-require-imports */
|
|
34
65
|
return [
|
|
35
|
-
new
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
new
|
|
66
|
+
new HttpInstrumentation({
|
|
67
|
+
ignoreIncomingRequestHook: (req) => isProbePath(req.url),
|
|
68
|
+
requireParentforOutgoingSpans: true,
|
|
69
|
+
redactedQueryParamsServer: exports.REDACTED_QUERY_PARAMS,
|
|
70
|
+
redactedQueryParams: exports.REDACTED_QUERY_PARAMS,
|
|
71
|
+
}),
|
|
72
|
+
new ExpressInstrumentation(),
|
|
73
|
+
// undici has no redaction option; it records url.full and url.query with only
|
|
74
|
+
// its built-in list, so the request hook overwrites both.
|
|
75
|
+
new UndiciInstrumentation({
|
|
76
|
+
requireParentforSpans: true,
|
|
77
|
+
requestHook: (span, request) => {
|
|
78
|
+
const q = request.path.indexOf("?");
|
|
79
|
+
if (q < 0)
|
|
80
|
+
return;
|
|
81
|
+
const query = redactQuery(request.path.slice(q));
|
|
82
|
+
span.setAttribute("url.query", query);
|
|
83
|
+
span.setAttribute("url.full", `${request.origin}${request.path.slice(0, q)}?${query}`);
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
new PgInstrumentation({ requireParentSpan: true }),
|
|
87
|
+
// Command name only: arguments carry login emails, client IPs and BullMQ job data.
|
|
88
|
+
new IORedisInstrumentation({ dbStatementSerializer: (cmd) => cmd }),
|
|
89
|
+
new AmqplibInstrumentation(),
|
|
90
|
+
new PinoInstrumentation({
|
|
91
|
+
// Traces only: no OTLP log records from every pino line.
|
|
92
|
+
disableLogSending: true,
|
|
93
|
+
// Only sampled lines carry ids; an unsampled trace id is a dead link.
|
|
94
|
+
logHook: (span, record) => {
|
|
95
|
+
if ((span.spanContext().traceFlags & 1) === 0) {
|
|
96
|
+
delete record["trace_id"];
|
|
97
|
+
delete record["span_id"];
|
|
98
|
+
delete record["trace_flags"];
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
}),
|
|
42
102
|
];
|
|
43
103
|
}
|
|
44
104
|
function createTelemetrySdk(env) {
|
|
45
105
|
if (!shouldStartTelemetry(env))
|
|
46
106
|
return null;
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
108
|
+
const { NodeSDK: Sdk } = require("@opentelemetry/sdk-node");
|
|
47
109
|
// No traceExporter: OTEL_TRACES_EXPORTER, OTEL_EXPORTER_OTLP_* and
|
|
48
|
-
// OTEL_TRACES_SAMPLER* are read from the environment by the SDK.
|
|
49
|
-
|
|
110
|
+
// OTEL_TRACES_SAMPLER* are read from the environment by the SDK. The empty
|
|
111
|
+
// arrays stop the SDK defaulting logs and metrics exporters to otlp.
|
|
112
|
+
return new Sdk({
|
|
50
113
|
serviceName: telemetryServiceName(env),
|
|
51
114
|
instrumentations: telemetryInstrumentations(),
|
|
115
|
+
metricReaders: [],
|
|
116
|
+
logRecordProcessors: [],
|
|
52
117
|
});
|
|
53
118
|
}
|
|
54
119
|
//# sourceMappingURL=telemetry.js.map
|
package/dist/telemetry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":";;;AAWA,oDAGC;AAED,oDAEC;AAED,kCAIC;AAmBD,kCASC;AAED,8DA+CC;AAED,gDAaC;AAjHD,8EAA8E;AAC9E,kFAAkF;AAElF,6EAA6E;AAC7E,6EAA6E;AAE7E,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC;AAErE,SAAgB,oBA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AG,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5C,iEAAiE;IACjE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,yBAAyB,CAA6C,CAAC;IACxG,mEAAmE;IACnE,2EAA2E;IAC3E,qEAAqE;IACrE,OAAO,IAAI,GAAG,CAAC;QACb,WAAW,EAAE,oBAAoB,CAAC,GAAG,CAAC;QACtC,gBAAgB,EAAE,yBAAyB,EAAE;QAC7C,aAAa,EAAE,EAAE;QACjB,mBAAmB,EAAE,EAAE;KACxB,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easyweb/logging",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
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",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"test": "jest",
|
|
29
29
|
"typecheck": "tsc --noEmit",
|
|
30
30
|
"typecheck:test": "tsc -p tsconfig.test.json --noEmit",
|
|
31
|
-
"check:telemetry": "npm run build && node scripts/check-telemetry.js"
|
|
31
|
+
"check:telemetry": "npm run build && node scripts/check-telemetry.js && node scripts/check-telemetry-export.js"
|
|
32
32
|
},
|
|
33
33
|
"keywords": [
|
|
34
34
|
"microservices",
|