@bymax-one/nest-telemetry 1.0.1 → 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/CHANGELOG.md +113 -0
- package/README.md +96 -62
- package/dist/nest/index.cjs +48 -0
- package/dist/nest/index.d.cts +35 -6
- package/dist/nest/index.d.ts +35 -6
- package/dist/nest/index.mjs +48 -2
- package/dist/register/index.mjs +106 -15
- package/dist/sdk/index.cjs +108 -16
- package/dist/sdk/index.d.cts +13 -2
- package/dist/sdk/index.d.ts +13 -2
- package/dist/sdk/index.mjs +108 -16
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,117 @@ The `release.yml` workflow extracts the section matching the pushed `vX.Y.Z` tag
|
|
|
8
8
|
as the GitHub Release body, so each released version needs a matching `## [X.Y.Z]`
|
|
9
9
|
heading here.
|
|
10
10
|
|
|
11
|
+
## [1.0.3] - 2026-11-14
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **`shutdownTimeoutMs`** on `TelemetryOptions`: the longest `shutdown()` and `shutdownTelemetry()` wait for the
|
|
16
|
+
final flush, in milliseconds. It defaults to 5000. A value that is not a finite number above zero, or is above
|
|
17
|
+
120000, means the default. When the flush has not finished by then, shutdown emits
|
|
18
|
+
`BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` and resolves; the flush keeps running in the background and a failure after
|
|
19
|
+
the deadline is dropped. Spans still queued at that point may be lost.
|
|
20
|
+
- `BYMAX_TELEMETRY_EXPORT_FAILED`: a span export failed, so the batch was dropped. The warning names the
|
|
21
|
+
exporter and a short upper-case error code (`UNKNOWN` when there is none), never the payload, the endpoint or
|
|
22
|
+
the error message, and repeats at most once a minute. Before, a collector that was down left the application
|
|
23
|
+
silent.
|
|
24
|
+
- **A bound `TELEMETRY_LOGGER` now receives the two runtime warnings.** `BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` is
|
|
25
|
+
reported through `errorStructured` under `TELEMETRY_SHUTDOWN_TIMEOUT_KEY` (`TRACING_SHUTDOWN_TIMEOUT`), and
|
|
26
|
+
`BYMAX_TELEMETRY_EXPORT_FAILED` through the optional `warnStructured` under `TELEMETRY_EXPORT_FAILED_KEY`
|
|
27
|
+
(`TRACING_EXPORT_FAILED`), with the exporter kind and the error code as metadata and never a message, an
|
|
28
|
+
endpoint or a payload. With no logger bound, or a logger without the method, they stay process warnings,
|
|
29
|
+
unchanged. Both keys are exported from `./nest`.
|
|
30
|
+
- A **Production** section in the README with measured, scoped figures for sampling, TLS to the collector, a
|
|
31
|
+
collector that is down, CPU, memory and the optional `--max-semi-space-size` lever. "What This Library Does
|
|
32
|
+
NOT Do" says that log records forwarded through `OTEL_LOGS_EXPORTER` are not redacted by this library.
|
|
33
|
+
|
|
34
|
+
### Changed
|
|
35
|
+
|
|
36
|
+
- **`ignorePaths` never ignores a path longer than 2,048 characters**, and the patterns are not run against it,
|
|
37
|
+
so the request keeps its span. Before, the patterns ran against a path of any length. The query is cut first,
|
|
38
|
+
so only the path counts. The cap does not bound a pattern with nested quantifiers on a short path, so anchor
|
|
39
|
+
patterns with no nested quantifiers.
|
|
40
|
+
- **A down collector now ends shutdown at the deadline with a warning, where it used to reject.** Against a
|
|
41
|
+
closed port, shutdown took 7 seconds before this release and rejected with the connection error. The
|
|
42
|
+
exporter's retries outlast the 5-second default, so shutdown now resolves with
|
|
43
|
+
`BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT`.
|
|
44
|
+
|
|
45
|
+
### Apply to a derived backend
|
|
46
|
+
|
|
47
|
+
The declarations gained `shutdownTimeoutMs`, two exported log keys and an optional `warnStructured` on the logger shape; a derived backend acts on these behaviours:
|
|
48
|
+
|
|
49
|
+
1. A caller that branches on the rejection of `shutdown()` or `shutdownTelemetry()` to detect a collector that
|
|
50
|
+
is down now has to read the `BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` and `BYMAX_TELEMETRY_EXPORT_FAILED`
|
|
51
|
+
warnings instead, or set `shutdownTimeoutMs` above the time the exporter needs to give up. Measured against a
|
|
52
|
+
closed port, a deadline of 5000 ms resolved at 5002 ms with the warning, and a deadline of 20000 ms rejected
|
|
53
|
+
with the connection error after 7.8 s. A collector that accepts a connection and never answers took 10
|
|
54
|
+
seconds, the exporter's own timeout.
|
|
55
|
+
2. Set `shutdownTimeoutMs` to fit the termination grace period of the platform: a shutdown that waits longer
|
|
56
|
+
than the grace period is killed before it finishes.
|
|
57
|
+
3. A host on `TelemetryModule` that binds `TELEMETRY_LOGGER` now gets a structured entry for a down collector.
|
|
58
|
+
Alert on `TRACING_SHUTDOWN_TIMEOUT` and `TRACING_EXPORT_FAILED`; the first replaces the rejection a down
|
|
59
|
+
collector used to cause at shutdown. A logger that should receive the export failure needs a
|
|
60
|
+
`warnStructured(logKey, message, userId, metadata)` method.
|
|
61
|
+
4. A route that `ignorePaths` was ignoring with a path over 2,048 characters now produces a span. Anchor the
|
|
62
|
+
patterns, with no nested quantifiers.
|
|
63
|
+
|
|
64
|
+
## [1.0.2] - 2026-11-13
|
|
65
|
+
|
|
66
|
+
### Security
|
|
67
|
+
|
|
68
|
+
- **node-redis command arguments and keys no longer reach `db.query.text`.** The command-name serializer
|
|
69
|
+
was applied to `instrumentation-ioredis` only, so `instrumentation-redis` kept the default one: an
|
|
70
|
+
`EVAL` argument and a key were exported on node-redis 3.1.2, 4.7.1, 5.12.2 and 6.3.1. Both
|
|
71
|
+
instrumentations now export the command name only, behind the same
|
|
72
|
+
`unsafe.disableRedisArgumentRedaction`, and a host override cannot replace the serializer of either.
|
|
73
|
+
|
|
74
|
+
### Fixed
|
|
75
|
+
|
|
76
|
+
- `OTEL_SDK_DISABLED` is read as the SDK reads it, trimmed. A value such as `"true "` or `" TRUE"`
|
|
77
|
+
made the library load the loader hook and every OpenTelemetry package and report `enabled: true`,
|
|
78
|
+
while the SDK itself did nothing.
|
|
79
|
+
- A later `startTelemetry` call against a running SDK no longer drops its options silently: see the
|
|
80
|
+
warning below.
|
|
81
|
+
|
|
82
|
+
### Added
|
|
83
|
+
|
|
84
|
+
- `BYMAX_TELEMETRY_OPTIONS_IGNORED`: a `startTelemetry` call reached an SDK that was already running, so its
|
|
85
|
+
options were ignored, or `enabled: false` did not stop it. Any defined option counts, `env` included, and a
|
|
86
|
+
call that passes none stays silent.
|
|
87
|
+
- `BYMAX_TELEMETRY_RESTART_UNSUPPORTED`: see below.
|
|
88
|
+
|
|
89
|
+
### Changed
|
|
90
|
+
|
|
91
|
+
- **A start after a shutdown in the same process is refused.** `startTelemetry` rejects with an error that
|
|
92
|
+
says to start once per process, and once in a global setup for a test suite, and the process emits
|
|
93
|
+
`BYMAX_TELEMETRY_RESTART_UNSUPPORTED`; the preload turns it into its start-failed warning. Auto-instrumented
|
|
94
|
+
spans can never resume after a shutdown in the same process, because the modules are already patched;
|
|
95
|
+
spans created through the API could, but a half-working restart is refused on purpose. Before, the second
|
|
96
|
+
start reported `enabled: true` and exported nothing. A test suite that started one SDK per case now has to
|
|
97
|
+
start once.
|
|
98
|
+
- The README names both Redis clients, says which one needs an active span, states what was measured about
|
|
99
|
+
mysql2 bound values, and lists the identifiers the library does not redact. A canary in the runtime proofs
|
|
100
|
+
fails if a bound mysql2 value ever reaches `db.query.text`.
|
|
101
|
+
|
|
102
|
+
### Shutdown order of `TelemetryModule`
|
|
103
|
+
|
|
104
|
+
`TelemetryModule` is now global, so its shutdown hook runs among the last. A span that another global module
|
|
105
|
+
ends in its own shutdown hook, such as a queue drain, is now exported when `TelemetryModule` is listed first in
|
|
106
|
+
the application module's imports, and was dropped before. Nest closes global modules in the reverse of the
|
|
107
|
+
order it registers them, so list it before any module that is global or imports a global module. Applications
|
|
108
|
+
that provided `TelemetryShutdownHook` in a module of their own to get this order can import the module.
|
|
109
|
+
|
|
110
|
+
### Apply to a derived backend
|
|
111
|
+
|
|
112
|
+
The declarations did not change; four behaviours did, and a derived backend acts on them:
|
|
113
|
+
|
|
114
|
+
1. A start after a shutdown in the same process now throws. Start telemetry once per process, and in a
|
|
115
|
+
test suite start it once in a global setup.
|
|
116
|
+
2. `TelemetryModule` is global. List it **first** in the application module's imports, so its flush runs
|
|
117
|
+
last; a module of your own that only provided `TelemetryShutdownHook` to get that order can import it.
|
|
118
|
+
3. `OTEL_SDK_DISABLED` is trimmed as the SDK trims it, so a padded `" true "` now really disables.
|
|
119
|
+
4. node-redis spans now carry the command name only. A dashboard or alert that matched node-redis
|
|
120
|
+
arguments or keys in `db.query.text` has to change.
|
|
121
|
+
|
|
11
122
|
## [1.0.1] - 2026-11-12
|
|
12
123
|
|
|
13
124
|
Documentation only; the built files are the same as in 1.0.0.
|
|
@@ -55,5 +166,7 @@ First release.
|
|
|
55
166
|
- Verification from the packed tarball: real `node --import` processes in a CommonJS and a native-ESM
|
|
56
167
|
application, with planted secrets absent from the exported body and negative controls.
|
|
57
168
|
|
|
169
|
+
[1.0.3]: https://github.com/bymaxone/nest-telemetry/releases/tag/v1.0.3
|
|
170
|
+
[1.0.2]: https://github.com/bymaxone/nest-telemetry/releases/tag/v1.0.2
|
|
58
171
|
[1.0.1]: https://github.com/bymaxone/nest-telemetry/releases/tag/v1.0.1
|
|
59
172
|
[1.0.0]: https://github.com/bymaxone/nest-telemetry/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ Install it with the OpenTelemetry peers you use: the command is in [Quick Start]
|
|
|
71
71
|
|
|
72
72
|
- ✅ **Span-end query redaction** — `url.query` is replaced and the query is cut from `url.full`, `http.url` and `http.target`, on spans from any instrumentation
|
|
73
73
|
- ✅ **Outgoing-URL credential list** — A list of credential-bearing parameters, checked in a test against the SDK's own default list; a host can add names and never drop them
|
|
74
|
-
- ✅ **Redis commands reduced to their name** — `
|
|
74
|
+
- ✅ **Redis commands reduced to their name** — for `ioredis` and node-redis (`redis`), arguments and keys never leave the process, `EVAL` and `EVALSHA` payloads included
|
|
75
75
|
- ✅ **Command line kept out of the resource** — `process.command_args` and `process.command_line` are dropped
|
|
76
76
|
- ✅ **Explicit, loud opt-outs** — Each off switch is a literal `unsafe.*: true` and emits a `SecurityWarning`
|
|
77
77
|
|
|
@@ -81,11 +81,11 @@ Install it with the OpenTelemetry peers you use: the command is in [Quick Start]
|
|
|
81
81
|
|
|
82
82
|
One package, separate entry points. Import only what your app needs:
|
|
83
83
|
|
|
84
|
-
| Subpath | Import | Purpose
|
|
85
|
-
| ------------ | ------------------------------------ |
|
|
86
|
-
| **SDK** | `@bymax-one/nest-telemetry` | `startTelemetry`, `shutdownTelemetry`, `ignorePaths`, `DEFAULT_REDACTED_QUERY_PARAMS`, types
|
|
87
|
-
| **Register** | `@bymax-one/nest-telemetry/register` | The preload for `node --import`; importing it runs it, and it exports nothing
|
|
88
|
-
| **Nest** | `@bymax-one/nest-telemetry/nest` | `TelemetryModule`, `TelemetryShutdownHook`, `TELEMETRY_LOGGER`, `TELEMETRY_FLUSH_FAILED_KEY`, `ITelemetryErrorLogger` (type) | NestJS 11 |
|
|
84
|
+
| Subpath | Import | Purpose | Dependencies |
|
|
85
|
+
| ------------ | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----------------: |
|
|
86
|
+
| **SDK** | `@bymax-one/nest-telemetry` | `startTelemetry`, `shutdownTelemetry`, `ignorePaths`, `DEFAULT_REDACTED_QUERY_PARAMS`, types | OpenTelemetry peers |
|
|
87
|
+
| **Register** | `@bymax-one/nest-telemetry/register` | The preload for `node --import`; importing it runs it, and it exports nothing | OpenTelemetry peers |
|
|
88
|
+
| **Nest** | `@bymax-one/nest-telemetry/nest` | `TelemetryModule`, `TelemetryShutdownHook`, `TELEMETRY_LOGGER`, `TELEMETRY_FLUSH_FAILED_KEY`, `TELEMETRY_SHUTDOWN_TIMEOUT_KEY`, `TELEMETRY_EXPORT_FAILED_KEY`, `ITelemetryErrorLogger` (type) | NestJS 11 |
|
|
89
89
|
|
|
90
90
|
The SDK entry and `./register` import no `@nestjs/*` package; only `./nest` does. Requiring `./register` from CommonJS throws and names `node --import`.
|
|
91
91
|
|
|
@@ -128,6 +128,8 @@ import { TelemetryModule } from '@bymax-one/nest-telemetry/nest'
|
|
|
128
128
|
export class AppModule {}
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
+
`TelemetryModule` is global, and Nest closes global modules last, in the reverse of the order it registers them. List it **first** in the application module's imports, before any module that is global or that imports a global module, so it closes after them: a span that another global module, such as a queue drain or a database module, ends in its own shutdown hook is exported only if `TelemetryModule` was registered first. Measured with Nest 11: listed first, the span arrives whether the other global module is imported directly or through a feature module; listed after a feature module that imports a global module, or after the global module itself, the span is dropped.
|
|
132
|
+
|
|
131
133
|
Things to know before you rely on it:
|
|
132
134
|
|
|
133
135
|
1. **Traces only, by default.** Metrics and logs stay off unless `OTEL_METRICS_EXPORTER` or `OTEL_LOGS_EXPORTER` names an exporter. This departs from the OpenTelemetry default, which exports both through OTLP, and it does so on purpose: otherwise a host that already ships metrics and logs another way would send them twice.
|
|
@@ -153,7 +155,7 @@ const telemetry = await startTelemetry({
|
|
|
153
155
|
await telemetry.shutdown()
|
|
154
156
|
```
|
|
155
157
|
|
|
156
|
-
This package installs no signal handler and shows none, because handling signals belongs to the application: a `SIGTERM` listener replaces Node's default termination, so it has to end the process itself once its work is done. `startTelemetry` resolves to a handle with `enabled` and `shutdown()`. Starting twice returns the handle already running, including across two copies of the module (an ES-module preload and a CommonJS application share the handle through `globalThis`).
|
|
158
|
+
This package installs no signal handler and shows none, because handling signals belongs to the application: a `SIGTERM` listener replaces Node's default termination, so it has to end the process itself once its work is done. `startTelemetry` resolves to a handle with `enabled` and `shutdown()`. Starting twice returns the handle already running, including across two copies of the module (an ES-module preload and a CommonJS application share the handle through `globalThis`). A later call that passes options is told they were ignored, and `enabled: false` against a running SDK is told it does not stop it: `shutdownTelemetry()` does.
|
|
157
159
|
|
|
158
160
|
---
|
|
159
161
|
|
|
@@ -163,20 +165,21 @@ This package installs no signal handler and shows none, because handling signals
|
|
|
163
165
|
|
|
164
166
|
Every field is the host's policy, and none is required.
|
|
165
167
|
|
|
166
|
-
| Option | Default | What it does
|
|
167
|
-
| -------------------------------------- | ---------------------------- |
|
|
168
|
-
| `enabled` | `true` | `false` never starts the SDK. `OTEL_SDK_DISABLED=true` disables it regardless.
|
|
169
|
-
| `serviceName` | `unknown_service:node` | The service name. `OTEL_SERVICE_NAME` overrides it.
|
|
170
|
-
| `serviceVersion` | none | The service version.
|
|
171
|
-
| `ignoreIncomingRequest` | none | Returns `true` for an incoming request that must produce no server span. `ignorePaths(patterns)` builds one from anchored patterns.
|
|
172
|
-
| `instrumentations.fs` | `false` | File-system instrumentation.
|
|
173
|
-
| `instrumentations.sendLogs` | follows `OTEL_LOGS_EXPORTER` | Whether pino's records are forwarded to the OpenTelemetry logs pipeline.
|
|
174
|
-
| `instrumentations.overrides` | none | Per-instrumentation configuration, merged over the library's. Redaction settings cannot be replaced here.
|
|
175
|
-
| `extraRedactedQueryParams` | none | Names added to the list redacted from outgoing request URLs. They add to the defaults and never replace them.
|
|
176
|
-
| `unsafe.disableUrlRedaction` | off | Keeps `url.query`, `url.full`, `http.url` and `http.target` as recorded. Literal `true`. Warns.
|
|
177
|
-
| `unsafe.disableRedisArgumentRedaction` | off | Keeps Redis command arguments in `db.query.text`. Literal `true`. Warns.
|
|
178
|
-
| `unsafe.exportProcessCommandArgs` | off | Keeps `process.command_args` and `process.command_line` in the resource. Literal `true`. Warns.
|
|
179
|
-
| `
|
|
168
|
+
| Option | Default | What it does |
|
|
169
|
+
| -------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
170
|
+
| `enabled` | `true` | `false` never starts the SDK. `OTEL_SDK_DISABLED=true` disables it regardless. |
|
|
171
|
+
| `serviceName` | `unknown_service:node` | The service name. `OTEL_SERVICE_NAME` overrides it. |
|
|
172
|
+
| `serviceVersion` | none | The service version. |
|
|
173
|
+
| `ignoreIncomingRequest` | none | Returns `true` for an incoming request that must produce no server span. `ignorePaths(patterns)` builds one from anchored patterns. |
|
|
174
|
+
| `instrumentations.fs` | `false` | File-system instrumentation. |
|
|
175
|
+
| `instrumentations.sendLogs` | follows `OTEL_LOGS_EXPORTER` | Whether pino's records are forwarded to the OpenTelemetry logs pipeline. |
|
|
176
|
+
| `instrumentations.overrides` | none | Per-instrumentation configuration, merged over the library's. Redaction settings cannot be replaced here. |
|
|
177
|
+
| `extraRedactedQueryParams` | none | Names added to the list redacted from outgoing request URLs. They add to the defaults and never replace them. |
|
|
178
|
+
| `unsafe.disableUrlRedaction` | off | Keeps `url.query`, `url.full`, `http.url` and `http.target` as recorded. Literal `true`. Warns. |
|
|
179
|
+
| `unsafe.disableRedisArgumentRedaction` | off | Keeps Redis command arguments in `db.query.text`. Literal `true`. Warns. |
|
|
180
|
+
| `unsafe.exportProcessCommandArgs` | off | Keeps `process.command_args` and `process.command_line` in the resource. Literal `true`. Warns. |
|
|
181
|
+
| `shutdownTimeoutMs` | `5000` | The longest shutdown waits for the final flush. Past it, shutdown warns and resolves. A value that is not a finite number above zero, or is above `120000`, means the default. |
|
|
182
|
+
| `env` | the process environment | The environment the library reads. The OpenTelemetry SDK itself always reads the process environment. |
|
|
180
183
|
|
|
181
184
|
### Environment variables
|
|
182
185
|
|
|
@@ -184,7 +187,7 @@ The library reads these in one source file, and a test fails if any other file n
|
|
|
184
187
|
|
|
185
188
|
| Variable | Effect |
|
|
186
189
|
| ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
187
|
-
| `OTEL_SDK_DISABLED` | `true`
|
|
190
|
+
| `OTEL_SDK_DISABLED` | `true` disables the SDK, in any letter case and with surrounding whitespace ignored, as the SDK reads it. |
|
|
188
191
|
| `OTEL_SERVICE_NAME` | Service name; overrides the option, because the operator's standard variable wins over what the code chose. Blank values count as absent. |
|
|
189
192
|
| `OTEL_METRICS_EXPORTER`, `OTEL_LOGS_EXPORTER` | Metrics and logs are exported only when these name an exporter. |
|
|
190
193
|
| `OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE` | Mapped onto the batch span processor the library passes; the SDK applies them only to one it builds itself. |
|
|
@@ -202,27 +205,46 @@ The OpenTelemetry SDK reads these itself, and the library never touches them: `O
|
|
|
202
205
|
|
|
203
206
|
The start emits these warnings:
|
|
204
207
|
|
|
205
|
-
| Code | Meaning
|
|
206
|
-
| ------------------------------------------- |
|
|
207
|
-
| `BYMAX_TELEMETRY_START_FAILED` | The SDK could not start (a peer is missing, or the SDK threw). Tracing is off.
|
|
208
|
-
| `BYMAX_TELEMETRY_ESM_HOOK_UNAVAILABLE` | The loader hook could not be registered. Native-ESM code is not instrumented.
|
|
209
|
-
| `BYMAX_TELEMETRY_SERVICE_NAME_MISSING` | No service name is set; spans use `unknown_service:node`.
|
|
210
|
-
| `BYMAX_TELEMETRY_UNSAFE_REDACTION_DISABLED` | A redaction was turned off through an `unsafe` option.
|
|
208
|
+
| Code | Meaning |
|
|
209
|
+
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
210
|
+
| `BYMAX_TELEMETRY_START_FAILED` | The SDK could not start (a peer is missing, or the SDK threw). Tracing is off. |
|
|
211
|
+
| `BYMAX_TELEMETRY_ESM_HOOK_UNAVAILABLE` | The loader hook could not be registered. Native-ESM code is not instrumented. |
|
|
212
|
+
| `BYMAX_TELEMETRY_SERVICE_NAME_MISSING` | No service name is set; spans use `unknown_service:node`. |
|
|
213
|
+
| `BYMAX_TELEMETRY_UNSAFE_REDACTION_DISABLED` | A redaction was turned off through an `unsafe` option. |
|
|
214
|
+
| `BYMAX_TELEMETRY_OPTIONS_IGNORED` | A `startTelemetry` call reached an SDK that was already running, so its options were ignored, or `enabled: false` did not stop it. Any defined option counts, `env` included. |
|
|
215
|
+
| `BYMAX_TELEMETRY_RESTART_UNSUPPORTED` | `startTelemetry` was called after a shutdown in the same process, and is refused. |
|
|
216
|
+
| `BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` | The final flush did not finish within `shutdownTimeoutMs`. Shutdown resolved without it, and spans still queued may be lost. With `TelemetryModule` and a bound `TELEMETRY_LOGGER`, it is reported through `errorStructured` under `TRACING_SHUTDOWN_TIMEOUT` instead. |
|
|
217
|
+
| `BYMAX_TELEMETRY_EXPORT_FAILED` | A span export failed, so the batch was dropped. The warning names the exporter and a short error code, never the payload or the endpoint, and repeats at most once a minute. With `TelemetryModule` and a bound `TELEMETRY_LOGGER` that has `warnStructured`, it is reported through it under `TRACING_EXPORT_FAILED` instead. |
|
|
211
218
|
|
|
212
219
|
The programmatic `startTelemetry` keeps its rejection when the SDK cannot start, for callers that want it.
|
|
213
220
|
|
|
214
221
|
### What is traced
|
|
215
222
|
|
|
216
223
|
- **Load order matters only for modules your application loads itself.** The core `http` module is patched even when the preload runs after it. A client such as `ioredis` is patched only if the preload ran before the application required it. Measured: an application that required `ioredis` first exported no `ioredis` span.
|
|
217
|
-
-
|
|
224
|
+
- **`ioredis` commands are traced only under an active span; node-redis commands are not.** The `ioredis` instrumentation defaults to `requireParentSpan: true`, so a command issued outside any request produces no span. The node-redis (`redis`) instrumentation defaults to `false`, so it traces a command with no request around it too. Measured with `redis` 5.
|
|
218
225
|
- **`fs` instrumentation is off by default**, because it floods traces with disk activity that has no context. Turn it on with `instrumentations.fs`.
|
|
219
226
|
|
|
220
227
|
### Flushing
|
|
221
228
|
|
|
222
|
-
Awaiting `shutdown()` or `shutdownTelemetry()` means the spans were exported, whoever started the flush: overlapping callers wait for the same flush, and a shutdown that arrives while the SDK is still starting waits for the start and then stops what it built. A flush the collector refuses is reported, never thrown into your shutdown path, by the Nest module; the programmatic functions reject, so their caller decides.
|
|
229
|
+
Awaiting `shutdown()` or `shutdownTelemetry()` means the spans were exported, whoever started the flush, unless the deadline below passed first: overlapping callers wait for the same flush, and a shutdown that arrives while the SDK is still starting waits for the start and then stops what it built. A flush the collector refuses is reported, never thrown into your shutdown path, by the Nest module; the programmatic functions reject, so their caller decides.
|
|
230
|
+
|
|
231
|
+
The wait is bounded. A collector that accepts a connection and never answers would otherwise hold shutdown for the exporter's own timeout, ten seconds by default, so a flush that has not finished within `shutdownTimeoutMs` (5 seconds unless set, at most 120 seconds) stops holding the caller: shutdown emits `BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT` and resolves. The flush keeps running in the background and a failure after the deadline is dropped. Spans still queued at that point may be lost.
|
|
232
|
+
|
|
233
|
+
A start after a shutdown in the same process is refused: `startTelemetry` rejects, with an error that says to start once per process (in a test suite, once in a global setup), and the process emits `BYMAX_TELEMETRY_RESTART_UNSUPPORTED`. Auto-instrumented spans can never resume after a shutdown, because the modules the first start instrumented are already patched against its tracer provider; spans created through the API could, but a half-working restart is refused on purpose. Measured with an in-process HTTP request and one manual span per run: after a restart that released the API globals, the manual span was exported and the HTTP spans were not. The preload turns the refusal into a `BYMAX_TELEMETRY_START_FAILED` warning and the application still boots.
|
|
223
234
|
|
|
224
235
|
---
|
|
225
236
|
|
|
237
|
+
## 🏭 Production
|
|
238
|
+
|
|
239
|
+
Every figure here was measured on one laptop, with Node 24 and a handler that does nothing but answer. Read them as orders of magnitude for your own measurement to confirm, not as promises.
|
|
240
|
+
|
|
241
|
+
- **Sampling.** The default samples every trace. For high traffic set `OTEL_TRACES_SAMPLER=parentbased_traceidratio` and `OTEL_TRACES_SAMPLER_ARG=0.1`. Of 2,000 spans created in one run each, the default exported all of them, a ratio of `0.1` exported 210 and a ratio of `0.01` exported 25.
|
|
242
|
+
- **TLS to the collector.** Against a collector whose certificate Node does not trust, every export failed with `self-signed certificate`. Setting `OTEL_EXPORTER_OTLP_CERTIFICATE` to the CA file, or `NODE_EXTRA_CA_CERTS`, exported all 2,000 spans in the same setup.
|
|
243
|
+
- **A collector that is down.** The batch is dropped and the library emits `BYMAX_TELEMETRY_EXPORT_FAILED`, at most once a minute. Shutdown stops waiting at `shutdownTimeoutMs`, 5 seconds by default. Before that deadline existed, shutdown against a closed port took 7,048 ms and 7,214 ms in two runs.
|
|
244
|
+
- **CPU.** One process, 4 runs per mode, median CPU time per request: 25 µs without the library, 26 µs with it loaded and disabled, 58 µs with a healthy collector (range 35 to 66) and 43 µs with the collector down (range 26 to 65). The extra cost in that handler is roughly 10 to 40 µs per request, and a handler that does real I/O dilutes it. The same configuration varied by up to a factor of two between runs.
|
|
245
|
+
- **Memory.** After 10 seconds idle, RSS was 61 to 63 MB without the library and with it disabled, 83 to 95 MB with a healthy collector and 142 to 160 MB with the collector unreachable. Under sustained load with the collector unreachable, the default settings reached between about 195 and 310 MB in one 90-second run.
|
|
246
|
+
- **Small containers.** `node --max-semi-space-size=8` is an optional V8 lever that shrinks the young generation. Under the same unreachable-collector load, RSS had a median of 138 MB (maximum 158) against about 195 to 310 MB for the default settings. The memory figures are one run each, so they are noisy, and a setting of 4 did not do better than 8 (RSS levelled at about 220 MB). At 1,000 requests per second against an unreachable collector, p99 latency over three runs each was 3.6 to 7.3 ms with the default settings and 4.8 to 6.8 ms with 8, so no cost showed at that load. The extra memory with the collector down is most likely churn from failed exports, which was not measured separately.
|
|
247
|
+
|
|
226
248
|
## 🔄 OpenTelemetry Versions
|
|
227
249
|
|
|
228
250
|
Every `@opentelemetry/*` package is an optional peer. The `0.x` packages break between minors, and `sdk-node` pins its own dependencies to exact versions, so the ranges here name only the generation the tests run against:
|
|
@@ -243,16 +265,18 @@ A caret on a `0.x` version allows patch releases only, so each OpenTelemetry min
|
|
|
243
265
|
|
|
244
266
|
## 🩺 Troubleshooting
|
|
245
267
|
|
|
246
|
-
| You see
|
|
247
|
-
|
|
|
248
|
-
| No spans at all
|
|
249
|
-
| Spans from a CommonJS app but none from a native-ESM app
|
|
250
|
-
| Spans arrive but carry no service name
|
|
251
|
-
| `BYMAX_TELEMETRY_START_FAILED`
|
|
252
|
-
| HTTP spans but no Redis spans
|
|
253
|
-
| Metrics or logs missing
|
|
254
|
-
| The last spans are lost
|
|
255
|
-
|
|
|
268
|
+
| You see | Look at |
|
|
269
|
+
| --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
270
|
+
| No spans at all | `OTEL_SDK_DISABLED`; that the application was started with `--import`; `OTEL_EXPORTER_OTLP_ENDPOINT`; the start warnings. |
|
|
271
|
+
| Spans from a CommonJS app but none from a native-ESM app | `BYMAX_TELEMETRY_ESM_HOOK_UNAVAILABLE`: install `@opentelemetry/instrumentation`. |
|
|
272
|
+
| Spans arrive but carry no service name | `BYMAX_TELEMETRY_SERVICE_NAME_MISSING`: set `OTEL_SERVICE_NAME` or the `serviceName` option. A name set only through `OTEL_RESOURCE_ATTRIBUTES` still reaches the spans, unless `OTEL_NODE_RESOURCE_DETECTORS` leaves out both the `env` and the `service` detector. |
|
|
273
|
+
| `BYMAX_TELEMETRY_START_FAILED` | The warning names the cause, usually an OpenTelemetry peer that is not installed. |
|
|
274
|
+
| HTTP spans but no Redis spans | `ioredis` commands are traced under an active span only, and the client must be loaded after the preload. |
|
|
275
|
+
| Metrics or logs missing | They are off unless `OTEL_METRICS_EXPORTER` or `OTEL_LOGS_EXPORTER` names an exporter. |
|
|
276
|
+
| The last spans of another module are lost when the application closes | List `TelemetryModule` first in the application module's imports, so it closes last. |
|
|
277
|
+
| No spans arrive and nothing else is logged | `BYMAX_TELEMETRY_EXPORT_FAILED`: the collector did not accept the export. Check the endpoint in `OTEL_EXPORTER_OTLP_ENDPOINT` and that the collector is reachable. |
|
|
278
|
+
| The last spans are lost on `SIGTERM` | Call `app.enableShutdownHooks()` in Nest, or `shutdownTelemetry()` from your own shutdown path. |
|
|
279
|
+
| A query string still appears in a span | It is on an attribute other than `url.query`, `url.full`, `http.url` or `http.target`: see [What This Library Does NOT Do](#-what-this-library-does-not-do). |
|
|
256
280
|
|
|
257
281
|
---
|
|
258
282
|
|
|
@@ -293,7 +317,7 @@ The resource carries `host.name`, `os.*`, `process.pid`, `process.owner`, the ex
|
|
|
293
317
|
|
|
294
318
|
### How this is verified
|
|
295
319
|
|
|
296
|
-
The packed tarball is installed and run with real `node --import` processes, in a CommonJS application and a native-ESM application, against a local OTLP receiver and a fake Redis. A real HTTP request carries a planted query secret and a real `EVAL` carries a planted argument; both are absent from the whole exported body, while the HTTP spans and the Redis command name are present. Negative controls (no loader hook, no preload, a preload loaded after `ioredis`, `OTEL_SDK_DISABLED=true`) export no such span, so the checks can fail. The
|
|
320
|
+
The packed tarball is installed and run with real `node --import` processes, in a CommonJS application and a native-ESM application, against a local OTLP receiver and a fake Redis. A real HTTP request carries a planted query secret and a real `EVAL` carries a planted argument; both are absent from the whole exported body, while the HTTP spans and the Redis command name are present. Negative controls (no loader hook, no preload, a preload loaded after `ioredis`, `OTEL_SDK_DISABLED=true`) export no such span, so the checks can fail. The span-end layer works on attributes, so it applies to spans from any instrumentation; an instrumentation the proofs do not exercise is not proven individually.
|
|
297
321
|
|
|
298
322
|
What is **not** redacted is listed flat in [What This Library Does NOT Do](#-what-this-library-does-not-do). To report a vulnerability, follow [SECURITY.md](./SECURITY.md).
|
|
299
323
|
|
|
@@ -305,7 +329,7 @@ What is **not** redacted is listed flat in [What This Library Does NOT Do](#-wha
|
|
|
305
329
|
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
306
330
|
| Query redaction | `url.query` is replaced by `[redacted]` and the query is cut from `url.full`, `http.url` and `http.target` as each span ends, for any instrumentation |
|
|
307
331
|
| Outgoing URLs | The HTTP instrumentation redacts a credential-parameter list (`token`, `code`, `access_token`, `signature`, `X-Amz-*`, and more); hosts add names, never drop them; a test compares the list with the SDK's own, so a name the SDK adds later is caught |
|
|
308
|
-
| Redis | `db.query.text` holds the command name only; `EVAL` and `EVALSHA` payloads, which carry `KEYS` and `ARGV`, never leave the process
|
|
332
|
+
| Redis | `db.query.text` holds the command name only, for `ioredis` and node-redis; `EVAL` and `EVALSHA` payloads, which carry `KEYS` and `ARGV`, and every key never leave the process |
|
|
309
333
|
| Process command line | `process.command_args` and `process.command_line` are dropped from the resource, because command-line arguments are a common carrier of secrets |
|
|
310
334
|
| Opt-outs | Literal `unsafe.*: true` options only, each emitting a `SecurityWarning`; no environment variable turns redaction off |
|
|
311
335
|
| Overrides | `instrumentations.overrides` is merged under the library's redaction, which is applied again on top |
|
|
@@ -354,19 +378,19 @@ pnpm check:runtime # packed-tarball consumer proofs
|
|
|
354
378
|
|
|
355
379
|
### SDK: `@bymax-one/nest-telemetry`
|
|
356
380
|
|
|
357
|
-
| Export | Signature | Purpose
|
|
358
|
-
| ------------------------------- | ---------------------------------------------------------- |
|
|
359
|
-
| `startTelemetry` | `(options?: TelemetryOptions) => Promise<TelemetryHandle>` | Assembles and starts the SDK; returns the running handle when one exists; rejects when the SDK cannot start
|
|
360
|
-
| `shutdownTelemetry` | `() => Promise<void>` | Waits for a start in progress, then flushes and stops the SDK; resolves at once when none was started
|
|
361
|
-
| `ignorePaths` | `(patterns: readonly RegExp[]) => (request) => boolean` | Predicate for `ignoreIncomingRequest`: matches the request path with its query cut; a request with no target is not matched |
|
|
362
|
-
| `DEFAULT_REDACTED_QUERY_PARAMS` | `readonly string[]` | The credential-parameter list redacted from outgoing request URLs
|
|
363
|
-
| `TelemetryOptions` | type | The options of `startTelemetry` ([Configuration](#-configuration))
|
|
364
|
-
| `TelemetryHandle` | type | `{ enabled: boolean, shutdown(): Promise<void> }`
|
|
365
|
-
| `UnsafeOptions` | type | The literal-`true` redaction opt-outs
|
|
366
|
-
| `InstrumentationOptions` | type | `fs`, `sendLogs` and `overrides`
|
|
367
|
-
| `IncomingRequestTarget` | type | What an incoming-request predicate sees: the request `url`
|
|
381
|
+
| Export | Signature | Purpose |
|
|
382
|
+
| ------------------------------- | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
383
|
+
| `startTelemetry` | `(options?: TelemetryOptions) => Promise<TelemetryHandle>` | Assembles and starts the SDK; returns the running handle when one exists; rejects when the SDK cannot start or was already shut down in this process |
|
|
384
|
+
| `shutdownTelemetry` | `() => Promise<void>` | Waits for a start in progress, then flushes and stops the SDK; resolves at once when none was started |
|
|
385
|
+
| `ignorePaths` | `(patterns: readonly RegExp[]) => (request) => boolean` | Predicate for `ignoreIncomingRequest`: matches the request path with its query cut; a request with no target, or a path over 2,048 characters, is not matched |
|
|
386
|
+
| `DEFAULT_REDACTED_QUERY_PARAMS` | `readonly string[]` | The credential-parameter list redacted from outgoing request URLs |
|
|
387
|
+
| `TelemetryOptions` | type | The options of `startTelemetry` ([Configuration](#-configuration)) |
|
|
388
|
+
| `TelemetryHandle` | type | `{ enabled: boolean, shutdown(): Promise<void> }` |
|
|
389
|
+
| `UnsafeOptions` | type | The literal-`true` redaction opt-outs |
|
|
390
|
+
| `InstrumentationOptions` | type | `fs`, `sendLogs` and `overrides` |
|
|
391
|
+
| `IncomingRequestTarget` | type | What an incoming-request predicate sees: the request `url` |
|
|
368
392
|
|
|
369
|
-
`ignorePaths` copies each pattern without its `g` and `y` flags, which would keep state between calls. Anchor the patterns at both ends
|
|
393
|
+
`ignorePaths` copies each pattern without its `g` and `y` flags, which would keep state between calls. Anchor the patterns at both ends, with no nested quantifiers: an unanchored one also matches routes that merely contain the word, and `/^\/(a+)+$/` took 2.5 s and 5.5 s in two runs on a 28-character path, and the time grows exponentially with the length.
|
|
370
394
|
|
|
371
395
|
### Register: `@bymax-one/nest-telemetry/register`
|
|
372
396
|
|
|
@@ -374,13 +398,15 @@ No exports. `node --import @bymax-one/nest-telemetry/register` registers the loa
|
|
|
374
398
|
|
|
375
399
|
### Nest: `@bymax-one/nest-telemetry/nest`
|
|
376
400
|
|
|
377
|
-
| Export
|
|
378
|
-
|
|
|
379
|
-
| `TelemetryModule`
|
|
380
|
-
| `TelemetryShutdownHook`
|
|
381
|
-
| `TELEMETRY_LOGGER`
|
|
382
|
-
| `TELEMETRY_FLUSH_FAILED_KEY`
|
|
383
|
-
| `
|
|
401
|
+
| Export | Kind | Purpose |
|
|
402
|
+
| -------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
403
|
+
| `TelemetryModule` | module | Global; registers `TelemetryShutdownHook`. List it first in the application module's imports |
|
|
404
|
+
| `TelemetryShutdownHook` | provider | `OnApplicationShutdown`: awaits `shutdownTelemetry()`; a refused flush is reported and never rejects |
|
|
405
|
+
| `TELEMETRY_LOGGER` | token | Optional provider with an `errorStructured(logKey, error)` method for a failed flush and a shutdown that hit its deadline, and an optional `warnStructured(logKey, message, userId, metadata)` for a failed export |
|
|
406
|
+
| `TELEMETRY_FLUSH_FAILED_KEY` | constant | The stable key, `TRACING_FLUSH_FAILED`, under which a failed flush is reported |
|
|
407
|
+
| `TELEMETRY_SHUTDOWN_TIMEOUT_KEY` | constant | The stable key, `TRACING_SHUTDOWN_TIMEOUT`, under which a shutdown that hit `shutdownTimeoutMs` is reported |
|
|
408
|
+
| `TELEMETRY_EXPORT_FAILED_KEY` | constant | The stable key, `TRACING_EXPORT_FAILED`, under which a failed span export is reported, with the exporter kind and the error code as metadata |
|
|
409
|
+
| `ITelemetryErrorLogger` | type | The shape `TELEMETRY_LOGGER` provides: `errorStructured(logKey, error)` and an optional `warnStructured` |
|
|
384
410
|
|
|
385
411
|
---
|
|
386
412
|
|
|
@@ -389,15 +415,23 @@ No exports. `node --import @bymax-one/nest-telemetry/register` registers the loa
|
|
|
389
415
|
By design, the following are out of scope:
|
|
390
416
|
|
|
391
417
|
- ❌ **Redact secrets in the URL path** — only the query string is removed. A path such as `/reset/<token>` is exported as it is
|
|
392
|
-
- ❌ **Redact SQL text with inline literals** — database instrumentations record the statement, and values written into it are exported
|
|
418
|
+
- ❌ **Redact SQL text with inline literals** — database instrumentations record the statement, and values written into it are exported. A value bound to a placeholder is not recorded by `instrumentation-mysql2` 0.71, measured with mysql2 2.3.3, 3.0.0, 3.9.0 and 3.24.6; a canary in the proofs fails if that stops being true
|
|
393
419
|
- ❌ **Redact `exception.message` in span events** — an exception's message is exported as recorded
|
|
420
|
+
- ❌ **Redact the text of database driver errors** — not measured; an error recorded on a span carries whatever message the driver wrote
|
|
421
|
+
- ❌ **Redact the memcached key** — `db.memcached.key` is recorded on every memcached span
|
|
422
|
+
- ❌ **Redact an SNS phone number** — a publish to a `PhoneNumber` records the number as the destination name
|
|
423
|
+
- ❌ **Redact the Kafka message key** — `messaging.kafka.message.key` is recorded on producer and consumer spans
|
|
424
|
+
- ❌ **Redact gRPC error messages** — `grpc.error_message` is recorded when a call fails
|
|
425
|
+
- ❌ **Stop the OpenAI instrumentation from capturing prompts** — it reads `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` itself, and when that is set, message content is recorded
|
|
394
426
|
- ❌ **Redact span names your own code builds from raw URLs**
|
|
395
427
|
- ❌ **Redact baggage**
|
|
396
428
|
- ❌ **Redact request and response bodies, or HTTP headers** — they are not captured by default, and they are not redacted if you enable capturing them
|
|
397
429
|
- ❌ **Redact attributes your own code sets**
|
|
430
|
+
- ❌ **Redact log records forwarded through `OTEL_LOGS_EXPORTER`** — the library redacts spans, and a log record is exported as the logger produced it, so redact in the logger. Measured with `@bymax-one/nest-logger` 1.4.16, which redacts before it hands the record over: a password and a nested token never reached the collector, and the `[REDACTED]` marker did. A plain pino logger with no redaction exported its password
|
|
398
431
|
- ❌ **Choose a sampler** — configure `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG`
|
|
399
432
|
- ❌ **Run a collector or a backend** — point `OTEL_EXPORTER_OTLP_ENDPOINT` at one
|
|
400
433
|
- ❌ **Decide your span names** — those come from the instrumentations and from your code
|
|
434
|
+
- ❌ **Start again after a shutdown in the same process** — `startTelemetry` is refused, because auto-instrumented modules stay patched against the first SDK
|
|
401
435
|
- ❌ **Install a signal handler** — handling `SIGTERM` belongs to the application
|
|
402
436
|
- ❌ **Provide strict semantic versioning** — each release in the [CHANGELOG](./CHANGELOG.md) says what changed and what to do about it, and the version number alone is not the compatibility information
|
|
403
437
|
|
package/dist/nest/index.cjs
CHANGED
|
@@ -13,6 +13,17 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
13
13
|
};
|
|
14
14
|
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
15
15
|
|
|
16
|
+
// src/sdk/warning-sink.ts
|
|
17
|
+
var SINK_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/warning-sink");
|
|
18
|
+
function registerWarningSink(sink) {
|
|
19
|
+
Reflect.set(globalThis, SINK_KEY, sink);
|
|
20
|
+
return () => {
|
|
21
|
+
if (Reflect.get(globalThis, SINK_KEY) === sink) {
|
|
22
|
+
Reflect.deleteProperty(globalThis, SINK_KEY);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
16
27
|
// src/sdk/handle.ts
|
|
17
28
|
var HANDLE_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/handle");
|
|
18
29
|
var PENDING_KEY = /* @__PURE__ */ Symbol.for("@bymax-one/nest-telemetry/pending-start");
|
|
@@ -30,6 +41,8 @@ async function shutdownTelemetry() {
|
|
|
30
41
|
|
|
31
42
|
// src/nest/telemetry-shutdown.hook.ts
|
|
32
43
|
var TELEMETRY_FLUSH_FAILED_KEY = "TRACING_FLUSH_FAILED";
|
|
44
|
+
var TELEMETRY_SHUTDOWN_TIMEOUT_KEY = "TRACING_SHUTDOWN_TIMEOUT";
|
|
45
|
+
var TELEMETRY_EXPORT_FAILED_KEY = "TRACING_EXPORT_FAILED";
|
|
33
46
|
var TELEMETRY_LOGGER = /* @__PURE__ */ Symbol("TELEMETRY_LOGGER");
|
|
34
47
|
exports.TelemetryShutdownHook = class TelemetryShutdownHook {
|
|
35
48
|
/**
|
|
@@ -38,6 +51,15 @@ exports.TelemetryShutdownHook = class TelemetryShutdownHook {
|
|
|
38
51
|
constructor(moduleRef) {
|
|
39
52
|
this.moduleRef = moduleRef;
|
|
40
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Routes the library's two runtime warnings to the structured logger the application bound, when
|
|
56
|
+
* there is one. Without it they stay process warnings.
|
|
57
|
+
*/
|
|
58
|
+
onModuleInit() {
|
|
59
|
+
if (this.structuredLogger() !== void 0) {
|
|
60
|
+
this.releaseSink = registerWarningSink((warning) => this.route(warning));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
41
63
|
/**
|
|
42
64
|
* Flushes and stops the SDK, if one was started.
|
|
43
65
|
*
|
|
@@ -54,7 +76,30 @@ exports.TelemetryShutdownHook = class TelemetryShutdownHook {
|
|
|
54
76
|
await shutdownTelemetry();
|
|
55
77
|
} catch (error) {
|
|
56
78
|
this.report(error);
|
|
79
|
+
} finally {
|
|
80
|
+
this.releaseSink?.();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Writes a warning to the structured logger under its key.
|
|
85
|
+
*
|
|
86
|
+
* @param warning - The warning.
|
|
87
|
+
* @returns Whether the logger took it; a warning with no key or no method on the logger is left
|
|
88
|
+
* to the process warning.
|
|
89
|
+
*/
|
|
90
|
+
route(warning) {
|
|
91
|
+
const logger = this.structuredLogger();
|
|
92
|
+
if (warning.code === "BYMAX_TELEMETRY_SHUTDOWN_TIMEOUT" && typeof logger?.errorStructured === "function") {
|
|
93
|
+
logger.errorStructured(TELEMETRY_SHUTDOWN_TIMEOUT_KEY, new Error(warning.text));
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (warning.code === "BYMAX_TELEMETRY_EXPORT_FAILED" && typeof logger?.warnStructured === "function") {
|
|
97
|
+
logger.warnStructured(TELEMETRY_EXPORT_FAILED_KEY, warning.text, void 0, {
|
|
98
|
+
...warning.fields
|
|
99
|
+
});
|
|
100
|
+
return true;
|
|
57
101
|
}
|
|
102
|
+
return false;
|
|
58
103
|
}
|
|
59
104
|
/**
|
|
60
105
|
* Reports a failed flush: through the structured logger when the application provides a usable
|
|
@@ -104,8 +149,11 @@ exports.TelemetryShutdownHook = __decorateClass([
|
|
|
104
149
|
exports.TelemetryModule = class TelemetryModule {
|
|
105
150
|
};
|
|
106
151
|
exports.TelemetryModule = __decorateClass([
|
|
152
|
+
common.Global(),
|
|
107
153
|
common.Module({ providers: [exports.TelemetryShutdownHook] })
|
|
108
154
|
], exports.TelemetryModule);
|
|
109
155
|
|
|
156
|
+
exports.TELEMETRY_EXPORT_FAILED_KEY = TELEMETRY_EXPORT_FAILED_KEY;
|
|
110
157
|
exports.TELEMETRY_FLUSH_FAILED_KEY = TELEMETRY_FLUSH_FAILED_KEY;
|
|
111
158
|
exports.TELEMETRY_LOGGER = TELEMETRY_LOGGER;
|
|
159
|
+
exports.TELEMETRY_SHUTDOWN_TIMEOUT_KEY = TELEMETRY_SHUTDOWN_TIMEOUT_KEY;
|
package/dist/nest/index.d.cts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { OnApplicationShutdown } from '@nestjs/common';
|
|
1
|
+
import { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
|
|
2
2
|
|
|
3
3
|
/** Log key of the entry a failed flush leaves, stable so an alert can match it. */
|
|
4
4
|
declare const TELEMETRY_FLUSH_FAILED_KEY = "TRACING_FLUSH_FAILED";
|
|
5
|
+
/** Log key of the entry a shutdown that hit its deadline leaves, stable so an alert can match it. */
|
|
6
|
+
declare const TELEMETRY_SHUTDOWN_TIMEOUT_KEY = "TRACING_SHUTDOWN_TIMEOUT";
|
|
7
|
+
/** Log key of the entry a failed span export leaves, stable so an alert can match it. */
|
|
8
|
+
declare const TELEMETRY_EXPORT_FAILED_KEY = "TRACING_EXPORT_FAILED";
|
|
5
9
|
/** Injection token of the optional structured logger a failed flush is reported through. */
|
|
6
10
|
declare const TELEMETRY_LOGGER: unique symbol;
|
|
7
11
|
/** The one call the hook makes on a structured logger. */
|
|
@@ -13,6 +17,16 @@ interface ITelemetryErrorLogger {
|
|
|
13
17
|
* @param error - The failure.
|
|
14
18
|
*/
|
|
15
19
|
errorStructured(logKey: string, error: Error): void;
|
|
20
|
+
/**
|
|
21
|
+
* Writes a warning entry under a log key, for a failed span export. Optional: a logger without
|
|
22
|
+
* it gets that warning as a process warning.
|
|
23
|
+
*
|
|
24
|
+
* @param logKey - The entry's log key.
|
|
25
|
+
* @param message - The library's own text, never anything from the exporter.
|
|
26
|
+
* @param userId - Always `undefined`.
|
|
27
|
+
* @param metadata - The exporter kind and a short error code.
|
|
28
|
+
*/
|
|
29
|
+
warnStructured?(logKey: string, message: string, userId?: string, metadata?: Record<string, unknown>): void;
|
|
16
30
|
}
|
|
17
31
|
/** What the hook needs of Nest's module reference: to look a provider up anywhere in the application. */
|
|
18
32
|
interface IProviderLookup {
|
|
@@ -27,12 +41,19 @@ interface IProviderLookup {
|
|
|
27
41
|
}): unknown;
|
|
28
42
|
}
|
|
29
43
|
/** Awaits the SDK flush as part of the application's own graceful shutdown. */
|
|
30
|
-
declare class TelemetryShutdownHook implements OnApplicationShutdown {
|
|
44
|
+
declare class TelemetryShutdownHook implements OnApplicationShutdown, OnModuleInit {
|
|
31
45
|
private readonly moduleRef;
|
|
46
|
+
/** Removes this hook's warning sink, when it registered one. */
|
|
47
|
+
private releaseSink;
|
|
32
48
|
/**
|
|
33
49
|
* @param moduleRef - Resolves the optional structured logger when a flush fails.
|
|
34
50
|
*/
|
|
35
51
|
constructor(moduleRef: IProviderLookup);
|
|
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(): void;
|
|
36
57
|
/**
|
|
37
58
|
* Flushes and stops the SDK, if one was started.
|
|
38
59
|
*
|
|
@@ -45,6 +66,14 @@ declare class TelemetryShutdownHook implements OnApplicationShutdown {
|
|
|
45
66
|
* has been reported. Never rejects.
|
|
46
67
|
*/
|
|
47
68
|
onApplicationShutdown(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Writes a warning to the structured logger under its key.
|
|
71
|
+
*
|
|
72
|
+
* @param warning - The warning.
|
|
73
|
+
* @returns Whether the logger took it; a warning with no key or no method on the logger is left
|
|
74
|
+
* to the process warning.
|
|
75
|
+
*/
|
|
76
|
+
private route;
|
|
48
77
|
/**
|
|
49
78
|
* Reports a failed flush: through the structured logger when the application provides a usable
|
|
50
79
|
* one, else through the Nest logger with the key in the message. Reporting is itself guarded at
|
|
@@ -65,12 +94,12 @@ declare class TelemetryShutdownHook implements OnApplicationShutdown {
|
|
|
65
94
|
}
|
|
66
95
|
|
|
67
96
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
97
|
+
* List it first in the imports of the application module, before any module that is global or
|
|
98
|
+
* imports a global module. Nest then awaits the SDK flush while closing the application, so
|
|
99
|
+
* buffered spans are exported before the process exits.
|
|
71
100
|
*/
|
|
72
101
|
declare class TelemetryModule {
|
|
73
102
|
}
|
|
74
103
|
|
|
75
|
-
export { TELEMETRY_FLUSH_FAILED_KEY, TELEMETRY_LOGGER, TelemetryModule, TelemetryShutdownHook };
|
|
104
|
+
export { TELEMETRY_EXPORT_FAILED_KEY, TELEMETRY_FLUSH_FAILED_KEY, TELEMETRY_LOGGER, TELEMETRY_SHUTDOWN_TIMEOUT_KEY, TelemetryModule, TelemetryShutdownHook };
|
|
76
105
|
export type { ITelemetryErrorLogger };
|