@bymax-one/nest-telemetry 1.0.2 → 1.0.4
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/CHANGELOG.md +96 -0
- package/README.md +80 -57
- package/dist/nest/index.cjs +47 -0
- package/dist/nest/index.d.cts +32 -3
- package/dist/nest/index.d.ts +32 -3
- package/dist/nest/index.mjs +46 -1
- package/dist/register/index.mjs +136 -17
- package/dist/sdk/index.cjs +138 -18
- package/dist/sdk/index.d.cts +24 -6
- package/dist/sdk/index.d.ts +24 -6
- package/dist/sdk/index.mjs +138 -18
- package/package.json +2 -1
package/dist/nest/index.mjs
CHANGED
|
@@ -11,6 +11,17 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
11
11
|
};
|
|
12
12
|
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
13
13
|
|
|
14
|
+
// src/sdk/warning-sink.ts
|
|
15
|
+
var SINK_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/warning-sink");
|
|
16
|
+
function registerWarningSink(sink) {
|
|
17
|
+
Reflect.set(globalThis, SINK_KEY, sink);
|
|
18
|
+
return () => {
|
|
19
|
+
if (Reflect.get(globalThis, SINK_KEY) === sink) {
|
|
20
|
+
Reflect.deleteProperty(globalThis, SINK_KEY);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
// src/sdk/handle.ts
|
|
15
26
|
var HANDLE_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/handle");
|
|
16
27
|
var PENDING_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/pending-start");
|
|
@@ -28,6 +39,8 @@ async function shutdownTelemetry() {
|
|
|
28
39
|
|
|
29
40
|
// src/nest/telemetry-shutdown.hook.ts
|
|
30
41
|
var TELEMETRY_FLUSH_FAILED_KEY = "TRACING_FLUSH_FAILED";
|
|
42
|
+
var TELEMETRY_SHUTDOWN_TIMEOUT_KEY = "TRACING_SHUTDOWN_TIMEOUT";
|
|
43
|
+
var TELEMETRY_EXPORT_FAILED_KEY = "TRACING_EXPORT_FAILED";
|
|
31
44
|
var TELEMETRY_LOGGER = /* @__PURE__ */ Symbol("TELEMETRY_LOGGER");
|
|
32
45
|
var TelemetryShutdownHook = class {
|
|
33
46
|
/**
|
|
@@ -36,6 +49,15 @@ var TelemetryShutdownHook = class {
|
|
|
36
49
|
constructor(moduleRef) {
|
|
37
50
|
this.moduleRef = moduleRef;
|
|
38
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Routes the library's two runtime warnings to the structured logger the application bound, when
|
|
54
|
+
* there is one. Without it they stay process warnings.
|
|
55
|
+
*/
|
|
56
|
+
onModuleInit() {
|
|
57
|
+
if (this.structuredLogger() !== void 0) {
|
|
58
|
+
this.releaseSink = registerWarningSink((warning) => this.route(warning));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
39
61
|
/**
|
|
40
62
|
* Flushes and stops the SDK, if one was started.
|
|
41
63
|
*
|
|
@@ -52,7 +74,30 @@ var TelemetryShutdownHook = class {
|
|
|
52
74
|
await shutdownTelemetry();
|
|
53
75
|
} catch (error) {
|
|
54
76
|
this.report(error);
|
|
77
|
+
} finally {
|
|
78
|
+
this.releaseSink?.();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Writes a warning to the structured logger under its key.
|
|
83
|
+
*
|
|
84
|
+
* @param warning - The warning.
|
|
85
|
+
* @returns Whether the logger took it; a warning with no key or no method on the logger is left
|
|
86
|
+
* to the process warning.
|
|
87
|
+
*/
|
|
88
|
+
route(warning) {
|
|
89
|
+
const logger = this.structuredLogger();
|
|
90
|
+
if (warning.code === "BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT" && typeof logger?.errorStructured === "function") {
|
|
91
|
+
logger.errorStructured(TELEMETRY_SHUTDOWN_TIMEOUT_KEY, new Error(warning.text));
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (warning.code === "BYMAX_TELEMETRY_EXPORT_FAILED" && typeof logger?.warnStructured === "function") {
|
|
95
|
+
logger.warnStructured(TELEMETRY_EXPORT_FAILED_KEY, warning.text, void 0, {
|
|
96
|
+
...warning.fields
|
|
97
|
+
});
|
|
98
|
+
return true;
|
|
55
99
|
}
|
|
100
|
+
return false;
|
|
56
101
|
}
|
|
57
102
|
/**
|
|
58
103
|
* Reports a failed flush: through the structured logger when the application provides a usable
|
|
@@ -106,4 +151,4 @@ TelemetryModule = __decorateClass([
|
|
|
106
151
|
Module({ providers: [TelemetryShutdownHook] })
|
|
107
152
|
], TelemetryModule);
|
|
108
153
|
|
|
109
|
-
export { TELEMETRY_FLUSH_FAILED_KEY, TELEMETRY_LOGGER, TelemetryModule, TelemetryShutdownHook };
|
|
154
|
+
export { TELEMETRY_EXPORT_FAILED_KEY, TELEMETRY_FLUSH_FAILED_KEY, TELEMETRY_LOGGER, TELEMETRY_SHUTDOWN_TIMEOUT_KEY, TelemetryModule, TelemetryShutdownHook };
|
package/dist/register/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { register } from 'module';
|
|
|
3
3
|
// src/register/boot.ts
|
|
4
4
|
|
|
5
5
|
// src/sdk/environment.ts
|
|
6
|
+
var MAX_TIMER_MS = 2147483647;
|
|
6
7
|
function readExporterList(raw) {
|
|
7
8
|
return (raw ?? "").split(",").map((name) => name.trim()).filter((name) => name !== "");
|
|
8
9
|
}
|
|
@@ -10,13 +11,30 @@ function readPositiveNumber(raw) {
|
|
|
10
11
|
const value = Number(raw);
|
|
11
12
|
return Number.isFinite(value) && value > 0 ? value : void 0;
|
|
12
13
|
}
|
|
14
|
+
function readTimerMillis(name, raw, oversized) {
|
|
15
|
+
const value = readPositiveNumber(raw);
|
|
16
|
+
if (value !== void 0 && value > MAX_TIMER_MS) {
|
|
17
|
+
oversized.push(name);
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
13
22
|
function readTelemetryEnvironment(source = process.env) {
|
|
14
23
|
const tuning = {};
|
|
15
24
|
const maxQueueSize = readPositiveNumber(source["OTEL_BSP_MAX_QUEUE_SIZE"]);
|
|
16
25
|
if (maxQueueSize !== void 0) tuning.maxQueueSize = maxQueueSize;
|
|
17
|
-
const
|
|
26
|
+
const oversizedTimers = [];
|
|
27
|
+
const scheduledDelayMillis = readTimerMillis(
|
|
28
|
+
"OTEL_BSP_SCHEDULE_DELAY",
|
|
29
|
+
source["OTEL_BSP_SCHEDULE_DELAY"],
|
|
30
|
+
oversizedTimers
|
|
31
|
+
);
|
|
18
32
|
if (scheduledDelayMillis !== void 0) tuning.scheduledDelayMillis = scheduledDelayMillis;
|
|
19
|
-
const exportTimeoutMillis =
|
|
33
|
+
const exportTimeoutMillis = readTimerMillis(
|
|
34
|
+
"OTEL_BSP_EXPORT_TIMEOUT",
|
|
35
|
+
source["OTEL_BSP_EXPORT_TIMEOUT"],
|
|
36
|
+
oversizedTimers
|
|
37
|
+
);
|
|
20
38
|
if (exportTimeoutMillis !== void 0) tuning.exportTimeoutMillis = exportTimeoutMillis;
|
|
21
39
|
const maxExportBatchSize = readPositiveNumber(source["OTEL_BSP_MAX_EXPORT_BATCH_SIZE"]);
|
|
22
40
|
if (maxExportBatchSize !== void 0) tuning.maxExportBatchSize = maxExportBatchSize;
|
|
@@ -28,6 +46,7 @@ function readTelemetryEnvironment(source = process.env) {
|
|
|
28
46
|
metricsExporters: readExporterList(source["OTEL_METRICS_EXPORTER"]),
|
|
29
47
|
logsExporters: readExporterList(source["OTEL_LOGS_EXPORTER"]),
|
|
30
48
|
batchSpanProcessor: tuning,
|
|
49
|
+
oversizedTimers,
|
|
31
50
|
resourceDetectors: detectors === void 0 || detectors.trim() === "" ? void 0 : readExporterList(detectors)
|
|
32
51
|
};
|
|
33
52
|
}
|
|
@@ -93,6 +112,8 @@ var HTTP_INSTRUMENTATION = "@opentelemetry/instrumentation-http";
|
|
|
93
112
|
var IOREDIS_INSTRUMENTATION = "@opentelemetry/instrumentation-ioredis";
|
|
94
113
|
var REDIS_INSTRUMENTATION = "@opentelemetry/instrumentation-redis";
|
|
95
114
|
var PINO_INSTRUMENTATION = "@opentelemetry/instrumentation-pino";
|
|
115
|
+
var WINSTON_INSTRUMENTATION = "@opentelemetry/instrumentation-winston";
|
|
116
|
+
var BUNYAN_INSTRUMENTATION = "@opentelemetry/instrumentation-bunyan";
|
|
96
117
|
var LOG_EXPORTERS = /* @__PURE__ */ new Set(["otlp", "console"]);
|
|
97
118
|
var UNKNOWN_SERVICE_NAME = "unknown_service:node";
|
|
98
119
|
var WARNING_OPTIONS = {
|
|
@@ -128,10 +149,13 @@ function buildInstrumentationConfig(options, signals) {
|
|
|
128
149
|
if (config !== void 0) overrides[name] = { ...config };
|
|
129
150
|
}
|
|
130
151
|
const redactRedis = options.unsafe?.disableRedisArgumentRedaction !== true;
|
|
152
|
+
const disableLogSending = !(instrumentations.sendLogs ?? signals.sendLogs);
|
|
131
153
|
const library = {
|
|
132
154
|
[FS_INSTRUMENTATION]: { enabled: instrumentations.fs === true },
|
|
133
155
|
[HTTP_INSTRUMENTATION]: {},
|
|
134
|
-
[PINO_INSTRUMENTATION]: { disableLogSending
|
|
156
|
+
[PINO_INSTRUMENTATION]: { disableLogSending },
|
|
157
|
+
[WINSTON_INSTRUMENTATION]: { disableLogSending },
|
|
158
|
+
[BUNYAN_INSTRUMENTATION]: { disableLogSending }
|
|
135
159
|
};
|
|
136
160
|
const merged = {};
|
|
137
161
|
for (const name of /* @__PURE__ */ new Set([...Object.keys(library), ...Object.keys(overrides)])) {
|
|
@@ -172,7 +196,13 @@ function warnAboutUnsafeOptions(options) {
|
|
|
172
196
|
}
|
|
173
197
|
if (options.unsafe?.exportProcessCommandArgs === true) {
|
|
174
198
|
process.emitWarning(
|
|
175
|
-
"Process command-line redaction is disabled: process.command_args and process.
|
|
199
|
+
"Process command-line redaction is disabled: process.command_args, process.command_line and process.command are exported in the resource, and command-line arguments can carry secrets.",
|
|
200
|
+
WARNING_OPTIONS
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
if (options.unsafe?.exportHostId === true) {
|
|
204
|
+
process.emitWarning(
|
|
205
|
+
"Host id redaction is disabled: host.id, the machine identifier, is exported in the resource, and it identifies the machine to every collector and backend.",
|
|
176
206
|
WARNING_OPTIONS
|
|
177
207
|
);
|
|
178
208
|
}
|
|
@@ -183,6 +213,14 @@ function warnAboutUnsafeOptions(options) {
|
|
|
183
213
|
);
|
|
184
214
|
}
|
|
185
215
|
}
|
|
216
|
+
function warnAboutOversizedTimers(env) {
|
|
217
|
+
for (const name of env.oversizedTimers) {
|
|
218
|
+
process.emitWarning(
|
|
219
|
+
`${name} is above 2147483647 ms, which a timer cannot hold, so the default applies. Set a value up to 2147483647.`,
|
|
220
|
+
{ type: "Warning", code: "BYMAX_TELEMETRY_BSP_VALUE_IGNORED" }
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
186
224
|
function warnAboutIgnoredOptions(options) {
|
|
187
225
|
const stop = options.enabled === false;
|
|
188
226
|
if (stop || Object.values(options).some((v) => v !== void 0)) {
|
|
@@ -193,6 +231,50 @@ function warnAboutIgnoredOptions(options) {
|
|
|
193
231
|
}
|
|
194
232
|
}
|
|
195
233
|
|
|
234
|
+
// src/sdk/warning-sink.ts
|
|
235
|
+
var SINK_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/warning-sink");
|
|
236
|
+
function reportWarning(warning) {
|
|
237
|
+
try {
|
|
238
|
+
const sink = Reflect.get(globalThis, SINK_KEY);
|
|
239
|
+
if (sink?.(warning) === true) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
} catch {
|
|
243
|
+
}
|
|
244
|
+
process.emitWarning(warning.text, { type: "Warning", code: warning.code });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/sdk/export-failure.ts
|
|
248
|
+
var WINDOW_MS = 6e4;
|
|
249
|
+
var SUCCESS = 0;
|
|
250
|
+
var CODE_TOKEN = /^[A-Z0-9_]{1,40}$/;
|
|
251
|
+
function codeOf(error) {
|
|
252
|
+
const code = String(Reflect.get(Object(error), "code"));
|
|
253
|
+
return CODE_TOKEN.test(code) ? code : "UNKNOWN";
|
|
254
|
+
}
|
|
255
|
+
function warnOnExportFailure(exporter, kind, clock = () => performance.now()) {
|
|
256
|
+
const original = exporter.export.bind(exporter);
|
|
257
|
+
let lastWarning = Number.NEGATIVE_INFINITY;
|
|
258
|
+
exporter.export = (spans, done) => {
|
|
259
|
+
original(spans, (result) => {
|
|
260
|
+
if (result.code !== SUCCESS && clock() - lastWarning >= WINDOW_MS) {
|
|
261
|
+
lastWarning = clock();
|
|
262
|
+
try {
|
|
263
|
+
const code = codeOf(result.error);
|
|
264
|
+
reportWarning({
|
|
265
|
+
code: "BYMAX_TELEMETRY_EXPORT_FAILED",
|
|
266
|
+
text: `Exporting spans failed (${kind}, code ${code}), so the batch was dropped. This warning repeats at most once a minute. Check that the collector is reachable.`,
|
|
267
|
+
fields: { kind, code }
|
|
268
|
+
});
|
|
269
|
+
} catch {
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
done(result);
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
return exporter;
|
|
276
|
+
}
|
|
277
|
+
|
|
196
278
|
// src/sdk/handle.ts
|
|
197
279
|
var HANDLE_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/handle");
|
|
198
280
|
var PENDING_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/pending-start");
|
|
@@ -216,7 +298,12 @@ function setPendingStart(start) {
|
|
|
216
298
|
function clearPendingStart() {
|
|
217
299
|
delete store[PENDING_KEY];
|
|
218
300
|
}
|
|
219
|
-
|
|
301
|
+
var DEFAULT_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
302
|
+
var MAX_SHUTDOWN_TIMEOUT_MS = 12e4;
|
|
303
|
+
function shutdownDeadline(requested) {
|
|
304
|
+
return requested !== void 0 && Number.isFinite(requested) && requested > 0 && requested <= MAX_SHUTDOWN_TIMEOUT_MS ? requested : DEFAULT_SHUTDOWN_TIMEOUT_MS;
|
|
305
|
+
}
|
|
306
|
+
function createHandle(stopSdk, shutdownTimeoutMs) {
|
|
220
307
|
let flush;
|
|
221
308
|
let finished = false;
|
|
222
309
|
const handle = {
|
|
@@ -228,12 +315,31 @@ function createHandle(stopSdk) {
|
|
|
228
315
|
if (finished) {
|
|
229
316
|
return Promise.resolve();
|
|
230
317
|
}
|
|
231
|
-
flush ??=
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
Reflect.
|
|
236
|
-
|
|
318
|
+
flush ??= new Promise((resolve, reject) => {
|
|
319
|
+
const deadline = shutdownDeadline(shutdownTimeoutMs);
|
|
320
|
+
const finish = () => {
|
|
321
|
+
finished = true;
|
|
322
|
+
Reflect.set(globalThis, STOPPED_KEY, true);
|
|
323
|
+
if (currentHandle() === handle) {
|
|
324
|
+
Reflect.deleteProperty(globalThis, HANDLE_KEY);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
const timer = setTimeout(() => {
|
|
328
|
+
reportWarning({
|
|
329
|
+
code: "BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT",
|
|
330
|
+
text: `The telemetry flush did not finish within ${deadline} ms, so shutdown stopped waiting for it. Spans still queued may be lost. Raise shutdownTimeoutMs or check that the collector answers.`,
|
|
331
|
+
fields: { timeoutMs: deadline }
|
|
332
|
+
});
|
|
333
|
+
finish();
|
|
334
|
+
resolve();
|
|
335
|
+
}, deadline);
|
|
336
|
+
timer.unref();
|
|
337
|
+
new Promise((started) => {
|
|
338
|
+
started(stopSdk());
|
|
339
|
+
}).finally(() => {
|
|
340
|
+
clearTimeout(timer);
|
|
341
|
+
finish();
|
|
342
|
+
}).then(resolve, reject);
|
|
237
343
|
});
|
|
238
344
|
return flush;
|
|
239
345
|
}
|
|
@@ -242,7 +348,12 @@ function createHandle(stopSdk) {
|
|
|
242
348
|
}
|
|
243
349
|
|
|
244
350
|
// src/sdk/resource-detectors.ts
|
|
245
|
-
var COMMAND_LINE_ATTRIBUTES = [
|
|
351
|
+
var COMMAND_LINE_ATTRIBUTES = [
|
|
352
|
+
"process.command_args",
|
|
353
|
+
"process.command_line",
|
|
354
|
+
"process.command"
|
|
355
|
+
];
|
|
356
|
+
var HOST_ID_ATTRIBUTES = ["host.id"];
|
|
246
357
|
var NAMED = /* @__PURE__ */ new Map([
|
|
247
358
|
["process", ["process"]],
|
|
248
359
|
["host", ["host", "os"]],
|
|
@@ -274,8 +385,12 @@ function withoutAttributes(detector, names) {
|
|
|
274
385
|
}
|
|
275
386
|
};
|
|
276
387
|
}
|
|
277
|
-
function selectResourceDetectors(selection, detectors,
|
|
278
|
-
const available =
|
|
388
|
+
function selectResourceDetectors(selection, detectors, keeps) {
|
|
389
|
+
const available = {
|
|
390
|
+
...detectors,
|
|
391
|
+
process: keeps.keepCommandLine ? detectors.process : withoutAttributes(detectors.process, COMMAND_LINE_ATTRIBUTES),
|
|
392
|
+
host: keeps.keepHostId ? detectors.host : withoutAttributes(detectors.host, HOST_ID_ATTRIBUTES)
|
|
393
|
+
};
|
|
279
394
|
if (selection?.includes("none") === true && !selection.includes("all")) {
|
|
280
395
|
return [];
|
|
281
396
|
}
|
|
@@ -350,13 +465,16 @@ function buildSdk(packages, options, env) {
|
|
|
350
465
|
process: resources.processDetector,
|
|
351
466
|
serviceinstance: resources.serviceInstanceIdDetector
|
|
352
467
|
},
|
|
353
|
-
|
|
468
|
+
{
|
|
469
|
+
keepCommandLine: options.unsafe?.exportProcessCommandArgs === true,
|
|
470
|
+
keepHostId: options.unsafe?.exportHostId === true
|
|
471
|
+
}
|
|
354
472
|
),
|
|
355
473
|
spanProcessors: [
|
|
356
474
|
// Ordered before the batch processor, so each span is redacted in place before it is exported.
|
|
357
475
|
...options.unsafe?.disableUrlRedaction === true ? [] : [createUrlQueryRedactor()],
|
|
358
476
|
new sdkTrace.BatchSpanProcessor({
|
|
359
|
-
exporter: new exporter.OTLPTraceExporter(),
|
|
477
|
+
exporter: warnOnExportFailure(new exporter.OTLPTraceExporter(), "otlp-http traces"),
|
|
360
478
|
...env.batchSpanProcessor
|
|
361
479
|
})
|
|
362
480
|
],
|
|
@@ -368,9 +486,10 @@ function buildSdk(packages, options, env) {
|
|
|
368
486
|
async function boot(options, env) {
|
|
369
487
|
warnAboutUnsafeOptions(options);
|
|
370
488
|
warnAboutMissingServiceName(options, env);
|
|
489
|
+
warnAboutOversizedTimers(env);
|
|
371
490
|
const sdk = buildSdk(await loadPackages(), options, env);
|
|
372
491
|
sdk.start();
|
|
373
|
-
const handle = createHandle(() => sdk.shutdown());
|
|
492
|
+
const handle = createHandle(() => sdk.shutdown(), options.shutdownTimeoutMs);
|
|
374
493
|
registerHandle(handle);
|
|
375
494
|
return handle;
|
|
376
495
|
}
|
package/dist/sdk/index.cjs
CHANGED
|
@@ -56,6 +56,7 @@ function serializeRedisCommandName(commandName) {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// src/sdk/ignore-paths.ts
|
|
59
|
+
var MAX_PATH_LENGTH = 2048;
|
|
59
60
|
function ignorePaths(patterns) {
|
|
60
61
|
const stateless = patterns.map(
|
|
61
62
|
(pattern) => new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ""))
|
|
@@ -66,10 +67,23 @@ function ignorePaths(patterns) {
|
|
|
66
67
|
}
|
|
67
68
|
const cut = request.url.indexOf("?");
|
|
68
69
|
const path = cut === -1 ? request.url : request.url.slice(0, cut);
|
|
69
|
-
return stateless.some((pattern) => pattern.test(path));
|
|
70
|
+
return path.length <= MAX_PATH_LENGTH && stateless.some((pattern) => pattern.test(path));
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
// src/sdk/warning-sink.ts
|
|
75
|
+
var SINK_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/warning-sink");
|
|
76
|
+
function reportWarning(warning) {
|
|
77
|
+
try {
|
|
78
|
+
const sink = Reflect.get(globalThis, SINK_KEY);
|
|
79
|
+
if (sink?.(warning) === true) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
process.emitWarning(warning.text, { type: "Warning", code: warning.code });
|
|
85
|
+
}
|
|
86
|
+
|
|
73
87
|
// src/sdk/handle.ts
|
|
74
88
|
var HANDLE_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/handle");
|
|
75
89
|
var PENDING_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/pending-start");
|
|
@@ -93,7 +107,12 @@ function setPendingStart(start) {
|
|
|
93
107
|
function clearPendingStart() {
|
|
94
108
|
delete store[PENDING_KEY];
|
|
95
109
|
}
|
|
96
|
-
|
|
110
|
+
var DEFAULT_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
111
|
+
var MAX_SHUTDOWN_TIMEOUT_MS = 12e4;
|
|
112
|
+
function shutdownDeadline(requested) {
|
|
113
|
+
return requested !== void 0 && Number.isFinite(requested) && requested > 0 && requested <= MAX_SHUTDOWN_TIMEOUT_MS ? requested : DEFAULT_SHUTDOWN_TIMEOUT_MS;
|
|
114
|
+
}
|
|
115
|
+
function createHandle(stopSdk, shutdownTimeoutMs) {
|
|
97
116
|
let flush;
|
|
98
117
|
let finished = false;
|
|
99
118
|
const handle = {
|
|
@@ -105,12 +124,31 @@ function createHandle(stopSdk) {
|
|
|
105
124
|
if (finished) {
|
|
106
125
|
return Promise.resolve();
|
|
107
126
|
}
|
|
108
|
-
flush ??=
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
Reflect.
|
|
113
|
-
|
|
127
|
+
flush ??= new Promise((resolve, reject) => {
|
|
128
|
+
const deadline = shutdownDeadline(shutdownTimeoutMs);
|
|
129
|
+
const finish = () => {
|
|
130
|
+
finished = true;
|
|
131
|
+
Reflect.set(globalThis, STOPPED_KEY, true);
|
|
132
|
+
if (currentHandle() === handle) {
|
|
133
|
+
Reflect.deleteProperty(globalThis, HANDLE_KEY);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const timer = setTimeout(() => {
|
|
137
|
+
reportWarning({
|
|
138
|
+
code: "BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT",
|
|
139
|
+
text: `The telemetry flush did not finish within ${deadline} ms, so shutdown stopped waiting for it. Spans still queued may be lost. Raise shutdownTimeoutMs or check that the collector answers.`,
|
|
140
|
+
fields: { timeoutMs: deadline }
|
|
141
|
+
});
|
|
142
|
+
finish();
|
|
143
|
+
resolve();
|
|
144
|
+
}, deadline);
|
|
145
|
+
timer.unref();
|
|
146
|
+
new Promise((started) => {
|
|
147
|
+
started(stopSdk());
|
|
148
|
+
}).finally(() => {
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
finish();
|
|
151
|
+
}).then(resolve, reject);
|
|
114
152
|
});
|
|
115
153
|
return flush;
|
|
116
154
|
}
|
|
@@ -128,6 +166,8 @@ var HTTP_INSTRUMENTATION = "@opentelemetry/instrumentation-http";
|
|
|
128
166
|
var IOREDIS_INSTRUMENTATION = "@opentelemetry/instrumentation-ioredis";
|
|
129
167
|
var REDIS_INSTRUMENTATION = "@opentelemetry/instrumentation-redis";
|
|
130
168
|
var PINO_INSTRUMENTATION = "@opentelemetry/instrumentation-pino";
|
|
169
|
+
var WINSTON_INSTRUMENTATION = "@opentelemetry/instrumentation-winston";
|
|
170
|
+
var BUNYAN_INSTRUMENTATION = "@opentelemetry/instrumentation-bunyan";
|
|
131
171
|
var LOG_EXPORTERS = /* @__PURE__ */ new Set(["otlp", "console"]);
|
|
132
172
|
var UNKNOWN_SERVICE_NAME = "unknown_service:node";
|
|
133
173
|
var WARNING_OPTIONS = {
|
|
@@ -163,10 +203,13 @@ function buildInstrumentationConfig(options, signals) {
|
|
|
163
203
|
if (config !== void 0) overrides[name] = { ...config };
|
|
164
204
|
}
|
|
165
205
|
const redactRedis = options.unsafe?.disableRedisArgumentRedaction !== true;
|
|
206
|
+
const disableLogSending = !(instrumentations.sendLogs ?? signals.sendLogs);
|
|
166
207
|
const library = {
|
|
167
208
|
[FS_INSTRUMENTATION]: { enabled: instrumentations.fs === true },
|
|
168
209
|
[HTTP_INSTRUMENTATION]: {},
|
|
169
|
-
[PINO_INSTRUMENTATION]: { disableLogSending
|
|
210
|
+
[PINO_INSTRUMENTATION]: { disableLogSending },
|
|
211
|
+
[WINSTON_INSTRUMENTATION]: { disableLogSending },
|
|
212
|
+
[BUNYAN_INSTRUMENTATION]: { disableLogSending }
|
|
170
213
|
};
|
|
171
214
|
const merged = {};
|
|
172
215
|
for (const name of /* @__PURE__ */ new Set([...Object.keys(library), ...Object.keys(overrides)])) {
|
|
@@ -207,7 +250,13 @@ function warnAboutUnsafeOptions(options) {
|
|
|
207
250
|
}
|
|
208
251
|
if (options.unsafe?.exportProcessCommandArgs === true) {
|
|
209
252
|
process.emitWarning(
|
|
210
|
-
"Process command-line redaction is disabled: process.command_args and process.
|
|
253
|
+
"Process command-line redaction is disabled: process.command_args, process.command_line and process.command are exported in the resource, and command-line arguments can carry secrets.",
|
|
254
|
+
WARNING_OPTIONS
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
if (options.unsafe?.exportHostId === true) {
|
|
258
|
+
process.emitWarning(
|
|
259
|
+
"Host id redaction is disabled: host.id, the machine identifier, is exported in the resource, and it identifies the machine to every collector and backend.",
|
|
211
260
|
WARNING_OPTIONS
|
|
212
261
|
);
|
|
213
262
|
}
|
|
@@ -218,6 +267,14 @@ function warnAboutUnsafeOptions(options) {
|
|
|
218
267
|
);
|
|
219
268
|
}
|
|
220
269
|
}
|
|
270
|
+
function warnAboutOversizedTimers(env) {
|
|
271
|
+
for (const name of env.oversizedTimers) {
|
|
272
|
+
process.emitWarning(
|
|
273
|
+
`${name} is above 2147483647 ms, which a timer cannot hold, so the default applies. Set a value up to 2147483647.`,
|
|
274
|
+
{ type: "Warning", code: "BYMAX_TELEMETRY_BSP_VALUE_IGNORED" }
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
221
278
|
function warnAboutIgnoredOptions(options) {
|
|
222
279
|
const stop = options.enabled === false;
|
|
223
280
|
if (stop || Object.values(options).some((v) => v !== void 0)) {
|
|
@@ -229,6 +286,7 @@ function warnAboutIgnoredOptions(options) {
|
|
|
229
286
|
}
|
|
230
287
|
|
|
231
288
|
// src/sdk/environment.ts
|
|
289
|
+
var MAX_TIMER_MS = 2147483647;
|
|
232
290
|
function readExporterList(raw) {
|
|
233
291
|
return (raw ?? "").split(",").map((name) => name.trim()).filter((name) => name !== "");
|
|
234
292
|
}
|
|
@@ -236,13 +294,30 @@ function readPositiveNumber(raw) {
|
|
|
236
294
|
const value = Number(raw);
|
|
237
295
|
return Number.isFinite(value) && value > 0 ? value : void 0;
|
|
238
296
|
}
|
|
297
|
+
function readTimerMillis(name, raw, oversized) {
|
|
298
|
+
const value = readPositiveNumber(raw);
|
|
299
|
+
if (value !== void 0 && value > MAX_TIMER_MS) {
|
|
300
|
+
oversized.push(name);
|
|
301
|
+
return void 0;
|
|
302
|
+
}
|
|
303
|
+
return value;
|
|
304
|
+
}
|
|
239
305
|
function readTelemetryEnvironment(source = process.env) {
|
|
240
306
|
const tuning = {};
|
|
241
307
|
const maxQueueSize = readPositiveNumber(source["OTEL_BSP_MAX_QUEUE_SIZE"]);
|
|
242
308
|
if (maxQueueSize !== void 0) tuning.maxQueueSize = maxQueueSize;
|
|
243
|
-
const
|
|
309
|
+
const oversizedTimers = [];
|
|
310
|
+
const scheduledDelayMillis = readTimerMillis(
|
|
311
|
+
"OTEL_BSP_SCHEDULE_DELAY",
|
|
312
|
+
source["OTEL_BSP_SCHEDULE_DELAY"],
|
|
313
|
+
oversizedTimers
|
|
314
|
+
);
|
|
244
315
|
if (scheduledDelayMillis !== void 0) tuning.scheduledDelayMillis = scheduledDelayMillis;
|
|
245
|
-
const exportTimeoutMillis =
|
|
316
|
+
const exportTimeoutMillis = readTimerMillis(
|
|
317
|
+
"OTEL_BSP_EXPORT_TIMEOUT",
|
|
318
|
+
source["OTEL_BSP_EXPORT_TIMEOUT"],
|
|
319
|
+
oversizedTimers
|
|
320
|
+
);
|
|
246
321
|
if (exportTimeoutMillis !== void 0) tuning.exportTimeoutMillis = exportTimeoutMillis;
|
|
247
322
|
const maxExportBatchSize = readPositiveNumber(source["OTEL_BSP_MAX_EXPORT_BATCH_SIZE"]);
|
|
248
323
|
if (maxExportBatchSize !== void 0) tuning.maxExportBatchSize = maxExportBatchSize;
|
|
@@ -254,12 +329,49 @@ function readTelemetryEnvironment(source = process.env) {
|
|
|
254
329
|
metricsExporters: readExporterList(source["OTEL_METRICS_EXPORTER"]),
|
|
255
330
|
logsExporters: readExporterList(source["OTEL_LOGS_EXPORTER"]),
|
|
256
331
|
batchSpanProcessor: tuning,
|
|
332
|
+
oversizedTimers,
|
|
257
333
|
resourceDetectors: detectors === void 0 || detectors.trim() === "" ? void 0 : readExporterList(detectors)
|
|
258
334
|
};
|
|
259
335
|
}
|
|
260
336
|
|
|
337
|
+
// src/sdk/export-failure.ts
|
|
338
|
+
var WINDOW_MS = 6e4;
|
|
339
|
+
var SUCCESS = 0;
|
|
340
|
+
var CODE_TOKEN = /^[A-Z0-9_]{1,40}$/;
|
|
341
|
+
function codeOf(error) {
|
|
342
|
+
const code = String(Reflect.get(Object(error), "code"));
|
|
343
|
+
return CODE_TOKEN.test(code) ? code : "UNKNOWN";
|
|
344
|
+
}
|
|
345
|
+
function warnOnExportFailure(exporter, kind, clock = () => performance.now()) {
|
|
346
|
+
const original = exporter.export.bind(exporter);
|
|
347
|
+
let lastWarning = Number.NEGATIVE_INFINITY;
|
|
348
|
+
exporter.export = (spans, done) => {
|
|
349
|
+
original(spans, (result) => {
|
|
350
|
+
if (result.code !== SUCCESS && clock() - lastWarning >= WINDOW_MS) {
|
|
351
|
+
lastWarning = clock();
|
|
352
|
+
try {
|
|
353
|
+
const code = codeOf(result.error);
|
|
354
|
+
reportWarning({
|
|
355
|
+
code: "BYMAX_TELEMETRY_EXPORT_FAILED",
|
|
356
|
+
text: `Exporting spans failed (${kind}, code ${code}), so the batch was dropped. This warning repeats at most once a minute. Check that the collector is reachable.`,
|
|
357
|
+
fields: { kind, code }
|
|
358
|
+
});
|
|
359
|
+
} catch {
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
done(result);
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
return exporter;
|
|
366
|
+
}
|
|
367
|
+
|
|
261
368
|
// src/sdk/resource-detectors.ts
|
|
262
|
-
var COMMAND_LINE_ATTRIBUTES = [
|
|
369
|
+
var COMMAND_LINE_ATTRIBUTES = [
|
|
370
|
+
"process.command_args",
|
|
371
|
+
"process.command_line",
|
|
372
|
+
"process.command"
|
|
373
|
+
];
|
|
374
|
+
var HOST_ID_ATTRIBUTES = ["host.id"];
|
|
263
375
|
var NAMED = /* @__PURE__ */ new Map([
|
|
264
376
|
["process", ["process"]],
|
|
265
377
|
["host", ["host", "os"]],
|
|
@@ -291,8 +403,12 @@ function withoutAttributes(detector, names) {
|
|
|
291
403
|
}
|
|
292
404
|
};
|
|
293
405
|
}
|
|
294
|
-
function selectResourceDetectors(selection, detectors,
|
|
295
|
-
const available =
|
|
406
|
+
function selectResourceDetectors(selection, detectors, keeps) {
|
|
407
|
+
const available = {
|
|
408
|
+
...detectors,
|
|
409
|
+
process: keeps.keepCommandLine ? detectors.process : withoutAttributes(detectors.process, COMMAND_LINE_ATTRIBUTES),
|
|
410
|
+
host: keeps.keepHostId ? detectors.host : withoutAttributes(detectors.host, HOST_ID_ATTRIBUTES)
|
|
411
|
+
};
|
|
296
412
|
if (selection?.includes("none") === true && !selection.includes("all")) {
|
|
297
413
|
return [];
|
|
298
414
|
}
|
|
@@ -367,13 +483,16 @@ function buildSdk(packages, options, env) {
|
|
|
367
483
|
process: resources.processDetector,
|
|
368
484
|
serviceinstance: resources.serviceInstanceIdDetector
|
|
369
485
|
},
|
|
370
|
-
|
|
486
|
+
{
|
|
487
|
+
keepCommandLine: options.unsafe?.exportProcessCommandArgs === true,
|
|
488
|
+
keepHostId: options.unsafe?.exportHostId === true
|
|
489
|
+
}
|
|
371
490
|
),
|
|
372
491
|
spanProcessors: [
|
|
373
492
|
// Ordered before the batch processor, so each span is redacted in place before it is exported.
|
|
374
493
|
...options.unsafe?.disableUrlRedaction === true ? [] : [createUrlQueryRedactor()],
|
|
375
494
|
new sdkTrace.BatchSpanProcessor({
|
|
376
|
-
exporter: new exporter.OTLPTraceExporter(),
|
|
495
|
+
exporter: warnOnExportFailure(new exporter.OTLPTraceExporter(), "otlp-http traces"),
|
|
377
496
|
...env.batchSpanProcessor
|
|
378
497
|
})
|
|
379
498
|
],
|
|
@@ -385,9 +504,10 @@ function buildSdk(packages, options, env) {
|
|
|
385
504
|
async function boot(options, env) {
|
|
386
505
|
warnAboutUnsafeOptions(options);
|
|
387
506
|
warnAboutMissingServiceName(options, env);
|
|
507
|
+
warnAboutOversizedTimers(env);
|
|
388
508
|
const sdk = buildSdk(await loadPackages(), options, env);
|
|
389
509
|
sdk.start();
|
|
390
|
-
const handle = createHandle(() => sdk.shutdown());
|
|
510
|
+
const handle = createHandle(() => sdk.shutdown(), options.shutdownTimeoutMs);
|
|
391
511
|
registerHandle(handle);
|
|
392
512
|
return handle;
|
|
393
513
|
}
|
package/dist/sdk/index.d.cts
CHANGED
|
@@ -41,18 +41,25 @@ interface UnsafeOptions {
|
|
|
41
41
|
/** Keeps Redis command arguments in `db.query.text`, including `EVAL` and `EVALSHA` payloads. */
|
|
42
42
|
readonly disableRedisArgumentRedaction?: true;
|
|
43
43
|
/**
|
|
44
|
-
* Keeps `process.command_args` and `process.
|
|
45
|
-
* removed by default, because command-line arguments are a common carrier of
|
|
44
|
+
* Keeps `process.command_args`, `process.command_line` and `process.command` in the exported
|
|
45
|
+
* resource. They are removed by default, because command-line arguments are a common carrier of
|
|
46
|
+
* secrets, and `process.command` is the first argument for `node -e code ARG`.
|
|
46
47
|
*/
|
|
47
48
|
readonly exportProcessCommandArgs?: true;
|
|
49
|
+
/**
|
|
50
|
+
* Keeps `host.id` in the exported resource. It is removed by default: it is the machine
|
|
51
|
+
* identifier (the platform UUID, or `/etc/machine-id`), a stable hardware id that every collector
|
|
52
|
+
* and backend would see. `host.name` stays.
|
|
53
|
+
*/
|
|
54
|
+
readonly exportHostId?: true;
|
|
48
55
|
}
|
|
49
56
|
/** Instrumentation choices that are the host's policy, with their safe defaults. */
|
|
50
57
|
interface InstrumentationOptions {
|
|
51
58
|
/** File-system instrumentation. Off by default: it floods traces with context-free disk activity. */
|
|
52
59
|
readonly fs?: boolean;
|
|
53
60
|
/**
|
|
54
|
-
* Whether
|
|
55
|
-
* `OTEL_LOGS_EXPORTER` names an exporter that exports logs.
|
|
61
|
+
* Whether the records of pino, winston and bunyan are forwarded to the OpenTelemetry logs
|
|
62
|
+
* pipeline. Defaults to whether `OTEL_LOGS_EXPORTER` names an exporter that exports logs.
|
|
56
63
|
*/
|
|
57
64
|
readonly sendLogs?: boolean;
|
|
58
65
|
/**
|
|
@@ -85,6 +92,13 @@ interface TelemetryOptions {
|
|
|
85
92
|
readonly extraRedactedQueryParams?: readonly string[];
|
|
86
93
|
/** Explicit opt-outs from redaction. */
|
|
87
94
|
readonly unsafe?: UnsafeOptions;
|
|
95
|
+
/**
|
|
96
|
+
* The longest `shutdown` waits for the final flush, in milliseconds. A collector that accepts a
|
|
97
|
+
* connection and never answers cannot hold the process past it: shutdown warns with
|
|
98
|
+
* `BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` and resolves, and spans still queued may be lost. Defaults
|
|
99
|
+
* to 5000; a value that is not a finite number above zero, or is above 120000, means the default.
|
|
100
|
+
*/
|
|
101
|
+
readonly shutdownTimeoutMs?: number;
|
|
88
102
|
/**
|
|
89
103
|
* The environment the library reads, which defaults to the process environment. The OTel SDK
|
|
90
104
|
* reads `OTEL_EXPORTER_OTLP_*` and the sampler variables from the process environment itself.
|
|
@@ -110,8 +124,12 @@ interface TelemetryHandle {
|
|
|
110
124
|
* Only the path is compared: the query string is cut first, so an exclusion cannot be switched off
|
|
111
125
|
* by appending a parameter, and a probe path hidden in a query cannot trigger one. A request with no
|
|
112
126
|
* target is not matched: failing open costs a span nobody wanted, where the opposite silently drops
|
|
113
|
-
* the traffic least likely to be well formed.
|
|
114
|
-
*
|
|
127
|
+
* the traffic least likely to be well formed. A path longer than 2,048 characters is never ignored
|
|
128
|
+
* and the patterns are not run against it, which bounds the work a long path can cause. Anchor
|
|
129
|
+
* the patterns at both ends and give them no nested quantifiers such as `(a+)+`: an unanchored
|
|
130
|
+
* pattern also matches routes that merely contain the word, and the cap does not bound a nested
|
|
131
|
+
* quantifier on a short path: `/^\/(a+)+$/` took 2.5 s and 5.5 s in two runs on 28 characters,
|
|
132
|
+
* and the time grows exponentially with the length.
|
|
115
133
|
*
|
|
116
134
|
* @param patterns - Patterns tested against the path alone.
|
|
117
135
|
* @returns A predicate for `TelemetryOptions.ignoreIncomingRequest`.
|