@zudojs/observability 0.1.0 → 1.0.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/LICENSE +21 -0
- package/README.md +262 -13
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.js +5 -0
- package/dist/errors/observabilityError.core.d.ts +32 -0
- package/dist/errors/observabilityError.core.js +56 -0
- package/dist/exporter/exporter.console.d.ts +51 -3
- package/dist/exporter/exporter.console.js +110 -23
- package/dist/exporter/index.d.ts +2 -2
- package/dist/exporter/index.js +2 -2
- package/dist/index.d.ts +17 -11
- package/dist/index.js +20 -12
- package/dist/internal/ids.core.d.ts +26 -0
- package/dist/internal/ids.core.js +51 -0
- package/dist/internal/index.d.ts +5 -0
- package/dist/internal/index.js +5 -0
- package/dist/logLevel/index.d.ts +1 -1
- package/dist/logLevel/index.js +1 -1
- package/dist/logLevel/logLevel.type.d.ts +8 -2
- package/dist/logLevel/logLevel.type.js +27 -20
- package/dist/logRecord/index.d.ts +1 -1
- package/dist/logRecord/index.js +1 -1
- package/dist/logRecord/logRecord.core.d.ts +15 -3
- package/dist/logRecord/logRecord.core.js +48 -26
- package/dist/logger/logger.core.d.ts +16 -8
- package/dist/logger/logger.core.js +54 -23
- package/dist/metrics/counter/counter.core.d.ts +4 -1
- package/dist/metrics/counter/counter.core.js +11 -3
- package/dist/metrics/gauge/gauge.core.d.ts +1 -0
- package/dist/metrics/gauge/gauge.core.js +9 -0
- package/dist/metrics/histogram/histogram.core.d.ts +22 -11
- package/dist/metrics/histogram/histogram.core.js +102 -7
- package/dist/metrics/histogram/index.d.ts +1 -1
- package/dist/metrics/histogram/index.js +1 -1
- package/dist/metrics/index.d.ts +4 -3
- package/dist/metrics/index.js +4 -3
- package/dist/metrics/metrics.reader.d.ts +39 -0
- package/dist/metrics/metrics.reader.js +81 -0
- package/dist/metrics/metrics.registry.d.ts +55 -4
- package/dist/metrics/metrics.registry.js +136 -49
- package/dist/noop/index.d.ts +1 -1
- package/dist/noop/index.js +1 -1
- package/dist/noop/noopObservability.core.d.ts +14 -2
- package/dist/noop/noopObservability.core.js +48 -11
- package/dist/observability/observability.core.d.ts +57 -3
- package/dist/observability/observability.core.js +206 -50
- package/dist/processor/index.d.ts +4 -3
- package/dist/processor/index.js +4 -3
- package/dist/processor/processor.batch.d.ts +66 -11
- package/dist/processor/processor.batch.js +130 -29
- package/dist/processor/processor.log.d.ts +56 -0
- package/dist/processor/processor.log.js +119 -0
- package/dist/propagation/index.d.ts +1 -1
- package/dist/propagation/index.js +1 -1
- package/dist/propagation/propagation.core.d.ts +26 -6
- package/dist/propagation/propagation.core.js +35 -19
- package/dist/redaction/index.d.ts +1 -1
- package/dist/redaction/index.js +1 -1
- package/dist/redaction/redaction.core.d.ts +40 -5
- package/dist/redaction/redaction.core.js +221 -36
- package/dist/sampling/index.d.ts +1 -1
- package/dist/sampling/index.js +1 -1
- package/dist/sampling/sampler.type.d.ts +38 -6
- package/dist/sampling/sampler.type.js +64 -23
- package/dist/tracing/index.d.ts +2 -2
- package/dist/tracing/index.js +2 -2
- package/dist/tracing/span/index.d.ts +1 -1
- package/dist/tracing/span/index.js +1 -1
- package/dist/tracing/span/span.core.d.ts +51 -8
- package/dist/tracing/span/span.core.js +90 -10
- package/dist/tracing/span/spanContext.type.d.ts +12 -3
- package/dist/tracing/span/spanContext.type.js +19 -16
- package/dist/tracing/tracer/index.d.ts +2 -2
- package/dist/tracing/tracer/index.js +2 -2
- package/dist/tracing/tracer/tracer.core.d.ts +44 -14
- package/dist/tracing/tracer/tracer.core.js +81 -25
- package/dist/types/config.types.d.ts +138 -0
- package/dist/types/config.types.js +5 -0
- package/dist/types/logging.types.d.ts +94 -0
- package/dist/types/logging.types.js +20 -0
- package/dist/types/metrics.types.d.ts +93 -0
- package/dist/types/metrics.types.js +5 -0
- package/dist/types/tracing.types.d.ts +115 -0
- package/dist/types/tracing.types.js +28 -0
- package/package.json +27 -14
- package/dist/exporter/exporter.console.d.ts.map +0 -1
- package/dist/exporter/exporter.console.js.map +0 -1
- package/dist/exporter/index.d.ts.map +0 -1
- package/dist/exporter/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logLevel/index.d.ts.map +0 -1
- package/dist/logLevel/index.js.map +0 -1
- package/dist/logLevel/logLevel.type.d.ts.map +0 -1
- package/dist/logLevel/logLevel.type.js.map +0 -1
- package/dist/logRecord/index.d.ts.map +0 -1
- package/dist/logRecord/index.js.map +0 -1
- package/dist/logRecord/logRecord.core.d.ts.map +0 -1
- package/dist/logRecord/logRecord.core.js.map +0 -1
- package/dist/logger/index.d.ts.map +0 -1
- package/dist/logger/index.js.map +0 -1
- package/dist/logger/logger.core.d.ts.map +0 -1
- package/dist/logger/logger.core.js.map +0 -1
- package/dist/metrics/counter/counter.core.d.ts.map +0 -1
- package/dist/metrics/counter/counter.core.js.map +0 -1
- package/dist/metrics/counter/index.d.ts.map +0 -1
- package/dist/metrics/counter/index.js.map +0 -1
- package/dist/metrics/gauge/gauge.core.d.ts.map +0 -1
- package/dist/metrics/gauge/gauge.core.js.map +0 -1
- package/dist/metrics/gauge/index.d.ts.map +0 -1
- package/dist/metrics/gauge/index.js.map +0 -1
- package/dist/metrics/histogram/histogram.core.d.ts.map +0 -1
- package/dist/metrics/histogram/histogram.core.js.map +0 -1
- package/dist/metrics/histogram/index.d.ts.map +0 -1
- package/dist/metrics/histogram/index.js.map +0 -1
- package/dist/metrics/index.d.ts.map +0 -1
- package/dist/metrics/index.js.map +0 -1
- package/dist/metrics/metrics.registry.d.ts.map +0 -1
- package/dist/metrics/metrics.registry.js.map +0 -1
- package/dist/noop/index.d.ts.map +0 -1
- package/dist/noop/index.js.map +0 -1
- package/dist/noop/noopObservability.core.d.ts.map +0 -1
- package/dist/noop/noopObservability.core.js.map +0 -1
- package/dist/observability/index.d.ts.map +0 -1
- package/dist/observability/index.js.map +0 -1
- package/dist/observability/observability.core.d.ts.map +0 -1
- package/dist/observability/observability.core.js.map +0 -1
- package/dist/processor/index.d.ts.map +0 -1
- package/dist/processor/index.js.map +0 -1
- package/dist/processor/processor.batch.d.ts.map +0 -1
- package/dist/processor/processor.batch.js.map +0 -1
- package/dist/propagation/index.d.ts.map +0 -1
- package/dist/propagation/index.js.map +0 -1
- package/dist/propagation/propagation.core.d.ts.map +0 -1
- package/dist/propagation/propagation.core.js.map +0 -1
- package/dist/redaction/index.d.ts.map +0 -1
- package/dist/redaction/index.js.map +0 -1
- package/dist/redaction/redaction.core.d.ts.map +0 -1
- package/dist/redaction/redaction.core.js.map +0 -1
- package/dist/sampling/index.d.ts.map +0 -1
- package/dist/sampling/index.js.map +0 -1
- package/dist/sampling/sampler.type.d.ts.map +0 -1
- package/dist/sampling/sampler.type.js.map +0 -1
- package/dist/tracing/index.d.ts.map +0 -1
- package/dist/tracing/index.js.map +0 -1
- package/dist/tracing/span/index.d.ts.map +0 -1
- package/dist/tracing/span/index.js.map +0 -1
- package/dist/tracing/span/span.core.d.ts.map +0 -1
- package/dist/tracing/span/span.core.js.map +0 -1
- package/dist/tracing/span/spanContext.type.d.ts.map +0 -1
- package/dist/tracing/span/spanContext.type.js.map +0 -1
- package/dist/tracing/tracer/index.d.ts.map +0 -1
- package/dist/tracing/tracer/index.js.map +0 -1
- package/dist/tracing/tracer/tracer.core.d.ts.map +0 -1
- package/dist/tracing/tracer/tracer.core.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zudojs Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @zudojs/observability
|
|
2
2
|
|
|
3
|
-
Structured logging, metrics, tracing, context propagation, and exporters for Zudojs applications.
|
|
3
|
+
Structured logging, metrics, tracing, context propagation, and telemetry exporters for Zudojs applications.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,25 +10,274 @@ npm install @zudojs/observability
|
|
|
10
10
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
|
+
`createObservability` is the entry point. It wires the logger, the metrics
|
|
14
|
+
registry, the tracer and the propagation manager into one object, and owns
|
|
15
|
+
their shutdown.
|
|
16
|
+
|
|
13
17
|
```typescript
|
|
14
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
createObservability,
|
|
20
|
+
createProbabilitySampler,
|
|
21
|
+
LogLevel,
|
|
22
|
+
} from "@zudojs/observability";
|
|
23
|
+
|
|
24
|
+
const obs = createObservability({
|
|
25
|
+
serviceName: "checkout-api",
|
|
26
|
+
serviceVersion: "1.4.0",
|
|
27
|
+
environment: "production",
|
|
28
|
+
logLevel: LogLevel.INFO,
|
|
29
|
+
|
|
30
|
+
// Opt in to redaction — see "Redaction" below.
|
|
31
|
+
redaction: {},
|
|
32
|
+
|
|
33
|
+
// Sample 10% of traces. The decision is derived from the trace ID, so a
|
|
34
|
+
// trace is never sampled in half across services.
|
|
35
|
+
sampler: createProbabilitySampler(0.1),
|
|
36
|
+
|
|
37
|
+
// Console exporters are the development default; turn them off in
|
|
38
|
+
// production and supply real ones.
|
|
39
|
+
useConsoleExporters: false,
|
|
40
|
+
});
|
|
15
41
|
|
|
16
|
-
|
|
17
|
-
|
|
42
|
+
obs.logger.info("server started", { port: 3000 });
|
|
43
|
+
obs.metrics.counter("http.requests.total", { route: "/checkout" }).increment();
|
|
18
44
|
|
|
19
|
-
const span = tracer.startSpan("handle-request");
|
|
20
|
-
span.setAttribute("http.method", "
|
|
21
|
-
await handleRequest();
|
|
45
|
+
const span = obs.tracer.startSpan("handle-request");
|
|
46
|
+
span.setAttribute("http.method", "POST");
|
|
22
47
|
span.end();
|
|
48
|
+
|
|
49
|
+
await obs.shutdown(); // drains every buffer, then closes the exporters
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Logging
|
|
53
|
+
|
|
54
|
+
Records are structured, level-filtered, and stamped with the ambient trace.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
obs.logger.info("order placed", { orderId: 42 });
|
|
58
|
+
|
|
59
|
+
// An Error goes in the third parameter. Putting it in the context would
|
|
60
|
+
// serialize to {} — Error's fields are non-enumerable.
|
|
61
|
+
obs.logger.error("charge failed", { orderId: 42 }, error);
|
|
62
|
+
|
|
63
|
+
const child = obs.logger.child("payments", { provider: "stripe" });
|
|
64
|
+
child.setLevel(LogLevel.DEBUG); // adjustable at runtime
|
|
65
|
+
await obs.logger.flush();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Every record carries `traceId` and `spanId` when a propagation context is
|
|
69
|
+
active, so logs and traces line up without threading IDs by hand.
|
|
70
|
+
|
|
71
|
+
## Redaction
|
|
72
|
+
|
|
73
|
+
Redaction is off unless you configure it, and on once you do. The logger
|
|
74
|
+
applies it to every record before any transport sees it, and the tracer
|
|
75
|
+
applies it to every span attribute and span event attribute before any
|
|
76
|
+
processor or exporter sees it — a `Bearer` token attached to a span is
|
|
77
|
+
redacted the same way one written to a log field is.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const obs = createObservability({
|
|
81
|
+
serviceName: "api",
|
|
82
|
+
redaction: {
|
|
83
|
+
// Defaults cover passwords, tokens, cookies, keys and card numbers.
|
|
84
|
+
fields: ["password", "token", "ssn"],
|
|
85
|
+
patterns: [/^x-.*-secret$/i],
|
|
86
|
+
// "contains" (the default) matches on word boundaries: "userPassword"
|
|
87
|
+
// and "x-api-key" match, "shippingAddress" and "authorId" do not.
|
|
88
|
+
matchMode: "contains",
|
|
89
|
+
replacement: "[REDACTED]",
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
obs.logger.info("login", {
|
|
94
|
+
username: "ada",
|
|
95
|
+
password: "hunter2", // → "[REDACTED]"
|
|
96
|
+
headers: [{ authorization: "…" }], // arrays are traversed too
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Traversal handles the shapes secrets actually arrive in: arrays, nested
|
|
101
|
+
objects, instances of your own classes (a DTO carrying a `password` field is
|
|
102
|
+
redacted and keeps its prototype), and cyclic graphs (a request object in a log
|
|
103
|
+
context becomes `[CIRCULAR]` rather than a stack overflow). Built-ins —
|
|
104
|
+
`Error`, `Date`, `Map`, `Set`, typed arrays, `URL` — are left intact instead of
|
|
105
|
+
being flattened to `{}`.
|
|
106
|
+
|
|
107
|
+
`redactObject`, `redactValue` and `createStructureRedactor` are exported for
|
|
108
|
+
use outside the logger.
|
|
109
|
+
|
|
110
|
+
## Metrics
|
|
111
|
+
|
|
112
|
+
Counters, gauges and histograms, keyed by name **and** labels.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
obs.metrics.counter("jobs.processed", { queue: "email" }).increment();
|
|
116
|
+
obs.metrics.gauge("queue.depth", { queue: "email" }).setValue(17);
|
|
117
|
+
|
|
118
|
+
const latency = obs.metrics.histogram("http.duration", { route: "/checkout" });
|
|
119
|
+
latency.record(87);
|
|
120
|
+
|
|
121
|
+
const value = latency.getValue();
|
|
122
|
+
value.p95; // interpolated from the bucket boundaries
|
|
123
|
+
value.mean;
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Histograms keep cumulative buckets, so `p50`/`p90`/`p95`/`p99` are real
|
|
127
|
+
answers rather than something you have to reconstruct from count and sum.
|
|
128
|
+
Pass your own boundaries when the unit is not milliseconds.
|
|
129
|
+
|
|
130
|
+
Values that cannot be aggregated — `NaN`, `Infinity`, a negative counter
|
|
131
|
+
increment — throw a `MetricValueError` rather than being silently dropped.
|
|
132
|
+
|
|
133
|
+
**Cardinality.** The registry caps the number of distinct series (default
|
|
134
|
+
10,000). A label carrying a user ID is the usual way to blow past that, and
|
|
135
|
+
the cap turns an unbounded memory leak into a reported error. Lower it — or
|
|
136
|
+
set the histogram boundaries used registry-wide — through `metrics`:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const obs = createObservability({
|
|
140
|
+
serviceName: "api",
|
|
141
|
+
metrics: { maxSeries: 2_000 },
|
|
142
|
+
onError: (error, source) => report(error, source), // cardinality is reported here
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
One metric name may only ever be one type: registering `counter("latency")`
|
|
147
|
+
and then `histogram("latency")` throws, because a document carrying the same
|
|
148
|
+
name as two types is rejected wholesale by OTLP and Prometheus.
|
|
149
|
+
|
|
150
|
+
Metrics are exported by a `PeriodicMetricReader`, which the facade starts for
|
|
151
|
+
you when a `metricExporter` is configured. `metricExportIntervalMs: 0`
|
|
152
|
+
disables the periodic export while still collecting a final snapshot on
|
|
153
|
+
`flush()` and `shutdown()`:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const obs = createObservability({
|
|
157
|
+
serviceName: "api",
|
|
158
|
+
metricExporter: myExporter,
|
|
159
|
+
metricExportIntervalMs: 30_000,
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Tracing
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const span = obs.tracer.startSpan("db.query", { kind: SpanKind.CLIENT });
|
|
167
|
+
try {
|
|
168
|
+
span.setAttribute("db.statement", sql);
|
|
169
|
+
return await run(sql);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
span.recordError(error as Error);
|
|
172
|
+
throw error;
|
|
173
|
+
} finally {
|
|
174
|
+
span.end();
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Spans are capped: 128 attributes, 128 events, 4096-character values by
|
|
179
|
+
default, with the overflow counted in `droppedAttributes` / `droppedEvents`.
|
|
180
|
+
Durations use a monotonic clock, so a clock adjustment cannot produce a
|
|
181
|
+
negative one. `setStatus(status, message)` keeps the message — it is exported
|
|
182
|
+
alongside the status.
|
|
183
|
+
|
|
184
|
+
Stack traces recorded by `recordError` reach the backend unredacted; set
|
|
185
|
+
`captureStackTraces: false` to omit them.
|
|
186
|
+
|
|
187
|
+
### Sampling
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import {
|
|
191
|
+
createAlwaysOnSampler,
|
|
192
|
+
createParentBasedSampler,
|
|
193
|
+
createProbabilitySampler,
|
|
194
|
+
} from "@zudojs/observability";
|
|
195
|
+
|
|
196
|
+
createObservability({
|
|
197
|
+
serviceName: "api",
|
|
198
|
+
sampler: createParentBasedSampler({ root: createProbabilitySampler(0.05) }),
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
The decision is stamped into `SpanContext.traceFlags` and inherited by every
|
|
203
|
+
child, which is what keeps a sampled trace whole across services.
|
|
204
|
+
|
|
205
|
+
## Context propagation
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { createPropagationContext } from "@zudojs/observability";
|
|
209
|
+
|
|
210
|
+
await obs.propagation.run(createPropagationContext({ requestId }), async () => {
|
|
211
|
+
obs.logger.info("handling"); // carries traceId, spanId
|
|
212
|
+
await handle();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
obs.propagation.current(); // undefined outside a run() scope
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
`current()` returns `undefined` when there is no active context, so "no trace"
|
|
219
|
+
stays distinguishable from a real one.
|
|
220
|
+
|
|
221
|
+
## Exporters
|
|
222
|
+
|
|
223
|
+
Console exporters ship for development. They serialize defensively — a
|
|
224
|
+
circular attribute or a BigInt cannot make an exporter throw inside the
|
|
225
|
+
logging path — and emit one line per record by default, which is what log
|
|
226
|
+
shippers parse.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
import {
|
|
230
|
+
createConsoleSpanExporter,
|
|
231
|
+
createBatchSpanProcessor,
|
|
232
|
+
createSimpleSpanProcessor,
|
|
233
|
+
} from "@zudojs/observability";
|
|
234
|
+
|
|
235
|
+
createObservability({
|
|
236
|
+
serviceName: "api",
|
|
237
|
+
processors: [
|
|
238
|
+
createBatchSpanProcessor({
|
|
239
|
+
exporter: createConsoleSpanExporter({ pretty: true }),
|
|
240
|
+
batchSize: 512,
|
|
241
|
+
maxQueueSize: 2048,
|
|
242
|
+
onError: (error, source) => console.error(source, error),
|
|
243
|
+
}),
|
|
244
|
+
],
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Both the span and log processors have a hard queue cap: past it, records are
|
|
249
|
+
dropped and counted rather than growing the process until it dies. Export
|
|
250
|
+
failures go to `onError` instead of vanishing.
|
|
251
|
+
|
|
252
|
+
Exporters for OpenTelemetry, Prometheus or Datadog belong in separate
|
|
253
|
+
packages; implement `SpanExporter`, `LogExporter` or `MetricExporter`.
|
|
254
|
+
|
|
255
|
+
## Scopes
|
|
256
|
+
|
|
257
|
+
`resource()` creates a view that differs only in its resource attributes,
|
|
258
|
+
sharing the parent's logger, registry, processors and exporters:
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
const podScoped = obs.resource({ "k8s.pod": process.env.POD_NAME });
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Shut down the root; a scope does not own the pipeline. `flush()` works from
|
|
265
|
+
either, because the buffers it drains are the shared ones.
|
|
266
|
+
|
|
267
|
+
## Disabling telemetry
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
import { createNoopObservability } from "@zudojs/observability";
|
|
271
|
+
|
|
272
|
+
const obs = enabled ? createObservability(config) : createNoopObservability();
|
|
23
273
|
```
|
|
24
274
|
|
|
25
|
-
##
|
|
275
|
+
## Errors
|
|
26
276
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
- Exporters (OTLP, Prometheus, etc.)
|
|
277
|
+
`ObservabilityError` and its subclasses — `ExporterError`,
|
|
278
|
+
`ObservabilityConfigError`, `MetricValueError` — cover the cases where this
|
|
279
|
+
package throws. Transport failures never throw; they are reported through
|
|
280
|
+
`onError`.
|
|
32
281
|
|
|
33
282
|
## Use Cases
|
|
34
283
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/observability — Errors
|
|
3
|
+
*
|
|
4
|
+
* Telemetry must never be the reason an application fails, so this package
|
|
5
|
+
* throws only for programmer errors — a malformed configuration, a metric
|
|
6
|
+
* recorded with a value that cannot be aggregated. Failures in the transport
|
|
7
|
+
* layer are reported through `ObservabilityConfig.onError` instead.
|
|
8
|
+
*/
|
|
9
|
+
import { BaseError, ErrorCode } from "@zudojs/errors";
|
|
10
|
+
/** Base error for all observability failures. */
|
|
11
|
+
export declare class ObservabilityError extends BaseError {
|
|
12
|
+
constructor(message: string, options?: {
|
|
13
|
+
readonly code?: ErrorCode;
|
|
14
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
15
|
+
readonly cause?: unknown;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** An exporter failed to deliver telemetry. */
|
|
19
|
+
export declare class ExporterError extends ObservabilityError {
|
|
20
|
+
constructor(exporterName: string, cause?: unknown);
|
|
21
|
+
}
|
|
22
|
+
/** A configuration value is unusable. */
|
|
23
|
+
export declare class ObservabilityConfigError extends ObservabilityError {
|
|
24
|
+
constructor(message: string, metadata?: Readonly<Record<string, unknown>>);
|
|
25
|
+
}
|
|
26
|
+
/** A metric was given a value it cannot aggregate. */
|
|
27
|
+
export declare class MetricValueError extends ObservabilityError {
|
|
28
|
+
constructor(metricName: string, value: number, reason: string);
|
|
29
|
+
}
|
|
30
|
+
/** Determines whether an unknown value is an observability error. */
|
|
31
|
+
export declare function isObservabilityError(value: unknown): value is ObservabilityError;
|
|
32
|
+
//# sourceMappingURL=observabilityError.core.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/observability — Errors
|
|
3
|
+
*
|
|
4
|
+
* Telemetry must never be the reason an application fails, so this package
|
|
5
|
+
* throws only for programmer errors — a malformed configuration, a metric
|
|
6
|
+
* recorded with a value that cannot be aggregated. Failures in the transport
|
|
7
|
+
* layer are reported through `ObservabilityConfig.onError` instead.
|
|
8
|
+
*/
|
|
9
|
+
import { BaseError, ErrorCode, ErrorCategory, ErrorSeverity, } from "@zudojs/errors";
|
|
10
|
+
/** Base error for all observability failures. */
|
|
11
|
+
export class ObservabilityError extends BaseError {
|
|
12
|
+
constructor(message, options) {
|
|
13
|
+
super(message, {
|
|
14
|
+
code: options?.code ?? ErrorCode.OPERATION_FAILED,
|
|
15
|
+
category: ErrorCategory.INTERNAL,
|
|
16
|
+
severity: ErrorSeverity.ERROR,
|
|
17
|
+
statusCode: 500,
|
|
18
|
+
expose: false,
|
|
19
|
+
metadata: options?.metadata,
|
|
20
|
+
cause: options?.cause,
|
|
21
|
+
});
|
|
22
|
+
this.name = "ObservabilityError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** An exporter failed to deliver telemetry. */
|
|
26
|
+
export class ExporterError extends ObservabilityError {
|
|
27
|
+
constructor(exporterName, cause) {
|
|
28
|
+
super(`Exporter "${exporterName}" failed to export telemetry`, {
|
|
29
|
+
metadata: { exporterName },
|
|
30
|
+
cause,
|
|
31
|
+
});
|
|
32
|
+
this.name = "ExporterError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** A configuration value is unusable. */
|
|
36
|
+
export class ObservabilityConfigError extends ObservabilityError {
|
|
37
|
+
constructor(message, metadata) {
|
|
38
|
+
super(message, { code: ErrorCode.VALIDATION_FAILED, metadata });
|
|
39
|
+
this.name = "ObservabilityConfigError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** A metric was given a value it cannot aggregate. */
|
|
43
|
+
export class MetricValueError extends ObservabilityError {
|
|
44
|
+
constructor(metricName, value, reason) {
|
|
45
|
+
super(`Metric "${metricName}" rejected value ${value}: ${reason}`, {
|
|
46
|
+
code: ErrorCode.VALIDATION_FAILED,
|
|
47
|
+
metadata: { metricName, value, reason },
|
|
48
|
+
});
|
|
49
|
+
this.name = "MetricValueError";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Determines whether an unknown value is an observability error. */
|
|
53
|
+
export function isObservabilityError(value) {
|
|
54
|
+
return value instanceof ObservabilityError;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=observabilityError.core.js.map
|
|
@@ -2,12 +2,50 @@
|
|
|
2
2
|
* @zudojs/observability — Console Exporter
|
|
3
3
|
*
|
|
4
4
|
* Exports telemetry to the console for development and debugging.
|
|
5
|
+
*
|
|
6
|
+
* Serialization is defensive, because everything here is fed values the API
|
|
7
|
+
* declares as `unknown`: circular structures, BigInts and throwing getters all
|
|
8
|
+
* reach `JSON.stringify` eventually, and an exporter that throws inside the
|
|
9
|
+
* logging path takes the process with it.
|
|
5
10
|
*/
|
|
6
11
|
import type { LogExporter, LogRecord, MetricExporter, MetricSnapshot, ReadableSpan, SpanExporter } from "../types.js";
|
|
12
|
+
/** Shape of the console methods the exporters use. */
|
|
13
|
+
export interface ConsoleLike {
|
|
14
|
+
log(message: string): void;
|
|
15
|
+
warn(message: string): void;
|
|
16
|
+
error(message: string): void;
|
|
17
|
+
}
|
|
18
|
+
/** Options shared by the console exporters. */
|
|
19
|
+
export interface ConsoleExporterOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Indent the JSON. Default: `false` — one line per record is what log
|
|
22
|
+
* shippers parse, and pretty-printing a span is expensive under load.
|
|
23
|
+
*/
|
|
24
|
+
readonly pretty?: boolean;
|
|
25
|
+
/** Where to write. Defaults to the global console. */
|
|
26
|
+
readonly console?: ConsoleLike;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* JSON.stringify that cannot throw.
|
|
30
|
+
*
|
|
31
|
+
* Cycles become `"[Circular]"`, BigInts become their decimal string, and a
|
|
32
|
+
* value that defeats serialization entirely falls back to `String(value)`.
|
|
33
|
+
*
|
|
34
|
+
* "Cycle" means an ancestor, not "seen before". A `WeakSet` of every object
|
|
35
|
+
* already visited also matches a value referenced twice from different
|
|
36
|
+
* branches — `{ user, actor: user }` — and silently replaced the second copy
|
|
37
|
+
* with `[Circular]`, losing real telemetry that was never circular. The
|
|
38
|
+
* replacer's `this` is the object currently being serialized, which is what
|
|
39
|
+
* lets the ancestor chain be tracked exactly.
|
|
40
|
+
*/
|
|
41
|
+
export declare function safeStringify(value: unknown, pretty?: boolean): string;
|
|
7
42
|
/**
|
|
8
43
|
* Exports completed spans to the console.
|
|
9
44
|
*/
|
|
10
45
|
export declare class ConsoleSpanExporter implements SpanExporter {
|
|
46
|
+
private readonly pretty;
|
|
47
|
+
private readonly out;
|
|
48
|
+
constructor(options?: ConsoleExporterOptions);
|
|
11
49
|
export(spans: readonly ReadableSpan[]): Promise<void>;
|
|
12
50
|
shutdown(): Promise<void>;
|
|
13
51
|
}
|
|
@@ -15,6 +53,9 @@ export declare class ConsoleSpanExporter implements SpanExporter {
|
|
|
15
53
|
* Exports log records to the console.
|
|
16
54
|
*/
|
|
17
55
|
export declare class ConsoleLogExporter implements LogExporter {
|
|
56
|
+
private readonly pretty;
|
|
57
|
+
private readonly out;
|
|
58
|
+
constructor(options?: ConsoleExporterOptions);
|
|
18
59
|
export(records: readonly LogRecord[]): Promise<void>;
|
|
19
60
|
shutdown(): Promise<void>;
|
|
20
61
|
}
|
|
@@ -22,13 +63,20 @@ export declare class ConsoleLogExporter implements LogExporter {
|
|
|
22
63
|
* Exports metric snapshots to the console.
|
|
23
64
|
*/
|
|
24
65
|
export declare class ConsoleMetricExporter implements MetricExporter {
|
|
66
|
+
private readonly pretty;
|
|
67
|
+
private readonly out;
|
|
68
|
+
constructor(options?: ConsoleExporterOptions);
|
|
25
69
|
export(snapshots: readonly MetricSnapshot[]): Promise<void>;
|
|
26
70
|
shutdown(): Promise<void>;
|
|
27
71
|
}
|
|
72
|
+
/** A log exporter that discards everything. */
|
|
73
|
+
export declare const noopLogExporter: LogExporter;
|
|
74
|
+
/** A metric exporter that discards everything. */
|
|
75
|
+
export declare const noopMetricExporter: MetricExporter;
|
|
28
76
|
/** Creates a console span exporter. */
|
|
29
|
-
export declare function createConsoleSpanExporter(): ConsoleSpanExporter;
|
|
77
|
+
export declare function createConsoleSpanExporter(options?: ConsoleExporterOptions): ConsoleSpanExporter;
|
|
30
78
|
/** Creates a console log exporter. */
|
|
31
|
-
export declare function createConsoleLogExporter(): ConsoleLogExporter;
|
|
79
|
+
export declare function createConsoleLogExporter(options?: ConsoleExporterOptions): ConsoleLogExporter;
|
|
32
80
|
/** Creates a console metric exporter. */
|
|
33
|
-
export declare function createConsoleMetricExporter(): ConsoleMetricExporter;
|
|
81
|
+
export declare function createConsoleMetricExporter(options?: ConsoleExporterOptions): ConsoleMetricExporter;
|
|
34
82
|
//# sourceMappingURL=exporter.console.d.ts.map
|