@backendkit-labs/observability 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +371 -367
- package/dist/index.cjs +86 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -4
- package/dist/index.d.ts +12 -4
- package/dist/index.js +85 -33
- package/dist/index.js.map +1 -1
- package/package.json +96 -96
package/README.md
CHANGED
|
@@ -1,367 +1,371 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
|
10
|
-
|
|
11
|
-
| **
|
|
12
|
-
| **
|
|
13
|
-
| **
|
|
14
|
-
| **
|
|
15
|
-
|
|
|
16
|
-
| **
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
@
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
app.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
//
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
this.logger.
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
tags: {
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
"
|
|
237
|
-
"
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
|
329
|
-
|
|
330
|
-
| `
|
|
331
|
-
| `
|
|
332
|
-
| `
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
1
|
+
# @backendkit-labs/observability
|
|
2
|
+
|
|
3
|
+
[](https://backendkitlabs.dev/docs/observability/)
|
|
4
|
+
|
|
5
|
+
Structured logging, distributed tracing correlation, metrics shipping, performance interceptors, and exception handling for **NestJS** — with optional OpenTelemetry integration.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
| Feature | Description |
|
|
10
|
+
|---|---|
|
|
11
|
+
| **CorrelationIdService** | AsyncLocalStorage-based correlation ID propagation across the full async call stack |
|
|
12
|
+
| **LoggerService** | Winston-backed structured logger with optional batched HTTP transport |
|
|
13
|
+
| **MetricsService** | Fire-and-forget metric event shipping with buffering and circuit breaker |
|
|
14
|
+
| **CorrelationInterceptor** | Reads/generates `x-correlation-id` and sets it on the response header |
|
|
15
|
+
| **PerformanceInterceptor** | Logs and records HTTP request duration for every route |
|
|
16
|
+
| **AllExceptionsFilter** | Unified error response shape with pluggable error mappers |
|
|
17
|
+
| **@TrackPerformance** | Method decorator that wraps any async method in an OTel span |
|
|
18
|
+
| **OTel optional** | `@opentelemetry/api` is a peer — drop it and everything becomes a no-op |
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @backendkit-labs/observability
|
|
26
|
+
|
|
27
|
+
# optional — only if you use OTel tracing
|
|
28
|
+
npm install @opentelemetry/api
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
// app.module.ts
|
|
37
|
+
import { Module } from '@nestjs/common';
|
|
38
|
+
import { ObservabilityModule } from '@backendkit-labs/observability';
|
|
39
|
+
|
|
40
|
+
@Module({
|
|
41
|
+
imports: [
|
|
42
|
+
ObservabilityModule.forRoot({
|
|
43
|
+
serviceName: 'my-api',
|
|
44
|
+
environment: 'production',
|
|
45
|
+
logLevel: 'info',
|
|
46
|
+
|
|
47
|
+
// Ship logs to a remote endpoint (optional)
|
|
48
|
+
http: {
|
|
49
|
+
url: 'https://logs.example.com/ingest',
|
|
50
|
+
authToken: process.env.OBS_AUTH_TOKEN,
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
// Ship metrics to a remote endpoint (optional)
|
|
54
|
+
metrics: {
|
|
55
|
+
url: 'https://metrics.example.com/ingest',
|
|
56
|
+
authToken: process.env.OBS_AUTH_TOKEN,
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
})
|
|
61
|
+
export class AppModule {}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Register interceptors and the exception filter globally in `main.ts`:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { NestFactory } from '@nestjs/core';
|
|
68
|
+
import {
|
|
69
|
+
CorrelationInterceptor,
|
|
70
|
+
PerformanceInterceptor,
|
|
71
|
+
AllExceptionsFilter,
|
|
72
|
+
LoggerService,
|
|
73
|
+
} from '@backendkit-labs/observability';
|
|
74
|
+
import { AppModule } from './app.module';
|
|
75
|
+
|
|
76
|
+
async function bootstrap() {
|
|
77
|
+
const app = await NestFactory.create(AppModule);
|
|
78
|
+
|
|
79
|
+
app.useGlobalInterceptors(
|
|
80
|
+
app.get(CorrelationInterceptor),
|
|
81
|
+
app.get(PerformanceInterceptor),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
app.useGlobalFilters(app.get(AllExceptionsFilter));
|
|
85
|
+
|
|
86
|
+
// Use LoggerService as the NestJS application logger
|
|
87
|
+
app.useLogger(app.get(LoggerService));
|
|
88
|
+
|
|
89
|
+
await app.listen(3000);
|
|
90
|
+
}
|
|
91
|
+
bootstrap();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## ObservabilityModule.forRoot options
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
interface ObservabilityOptions {
|
|
100
|
+
serviceName: string;
|
|
101
|
+
environment?: string; // default: "production"
|
|
102
|
+
logLevel?: 'error' | 'warn' | 'info' | 'http' | 'verbose' | 'debug' | 'silly';
|
|
103
|
+
http?: HttpTransportOptions;
|
|
104
|
+
metrics?: MetricsOptions;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### HTTP log transport options
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
interface HttpTransportOptions {
|
|
112
|
+
url: string;
|
|
113
|
+
authToken?: string;
|
|
114
|
+
headers?: Record<string, string>;
|
|
115
|
+
batchSize?: number; // default 100
|
|
116
|
+
maxBufferSize?: number; // default 2000
|
|
117
|
+
flushIntervalMs?: number; // default 5000
|
|
118
|
+
timeoutMs?: number; // default 5000
|
|
119
|
+
circuitBreaker?: Partial<CircuitBreakerConfig>; // see below
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Metrics transport options
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface MetricsOptions {
|
|
127
|
+
url: string;
|
|
128
|
+
authToken?: string;
|
|
129
|
+
headers?: Record<string, string>;
|
|
130
|
+
flushIntervalMs?: number; // default 10000
|
|
131
|
+
maxBufferSize?: number; // default 5000
|
|
132
|
+
timeoutMs?: number; // default 5000
|
|
133
|
+
circuitBreaker?: Partial<CircuitBreakerConfig>; // see below
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## CorrelationIdService
|
|
140
|
+
|
|
141
|
+
Propagates a request-scoped correlation ID through every `await` using `AsyncLocalStorage`.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
@Injectable()
|
|
145
|
+
export class OrdersService {
|
|
146
|
+
constructor(private readonly correlation: CorrelationIdService) {}
|
|
147
|
+
|
|
148
|
+
async processOrder(id: string) {
|
|
149
|
+
// Returns the ID for the current request context, or 'no-context' when
|
|
150
|
+
// called outside a context (e.g. background jobs, bootstrap code).
|
|
151
|
+
const cid = this.correlation.get();
|
|
152
|
+
|
|
153
|
+
// Returns undefined when called outside a context — use this when you need
|
|
154
|
+
// to distinguish "no active context" from a real correlation ID.
|
|
155
|
+
const maybeId = this.correlation.getOrUndefined();
|
|
156
|
+
|
|
157
|
+
// OTel trace/span IDs (undefined when OTel not installed)
|
|
158
|
+
const trace = this.correlation.getTraceContext();
|
|
159
|
+
// => { traceId: 'abc…', spanId: '123…' } | undefined
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The `CorrelationInterceptor` automatically seeds the context from the incoming `x-correlation-id` header (or generates a fresh UUID) and echoes the ID back in the response header. Incoming values are validated against an allowlist (`[a-zA-Z0-9\-_:]{1,64}`) — invalid values are replaced with a fresh UUID.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## LoggerService
|
|
169
|
+
|
|
170
|
+
Drop-in replacement for NestJS's built-in logger. All log entries include `service`, `environment`, and `correlationId` automatically.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
@Injectable()
|
|
174
|
+
export class PaymentsService {
|
|
175
|
+
constructor(private readonly logger: LoggerService) {}
|
|
176
|
+
|
|
177
|
+
async charge(amount: number) {
|
|
178
|
+
this.logger.log('Charging card', PaymentsService.name);
|
|
179
|
+
this.logger.warn('High amount', PaymentsService.name);
|
|
180
|
+
this.logger.error('Card declined', undefined, PaymentsService.name);
|
|
181
|
+
|
|
182
|
+
// Arbitrary extra fields
|
|
183
|
+
this.logger.logWithMeta('info', 'Payment processed', {
|
|
184
|
+
amount,
|
|
185
|
+
currency: 'USD',
|
|
186
|
+
userId: 'u_123',
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Console output format
|
|
193
|
+
|
|
194
|
+
```
|
|
195
|
+
2024-01-15T10:30:00.000Z [info] Charging card {"service":"payments","correlationId":"a1b2…"}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### HTTP transport
|
|
199
|
+
|
|
200
|
+
When `http` is configured, log entries are buffered in memory and flushed in batches. A built-in circuit breaker pauses sends after repeated failures so logging never blocks your application.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## MetricsService
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
@Injectable()
|
|
208
|
+
export class CheckoutService {
|
|
209
|
+
constructor(private readonly metrics: MetricsService) {}
|
|
210
|
+
|
|
211
|
+
async checkout(cart: Cart) {
|
|
212
|
+
const start = Date.now();
|
|
213
|
+
// ... process ...
|
|
214
|
+
this.metrics.record('checkout.duration', Date.now() - start, {
|
|
215
|
+
unit: 'ms',
|
|
216
|
+
tags: { region: 'us-east-1' },
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
this.metrics.record('checkout.items', cart.items.length, {
|
|
220
|
+
tags: { currency: cart.currency },
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Events are buffered and shipped in batches. The service flushes remaining events on `onModuleDestroy` (graceful shutdown).
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## AllExceptionsFilter
|
|
231
|
+
|
|
232
|
+
Returns a consistent error shape for every unhandled exception:
|
|
233
|
+
|
|
234
|
+
```json
|
|
235
|
+
{
|
|
236
|
+
"ok": false,
|
|
237
|
+
"statusCode": 404,
|
|
238
|
+
"message": "Resource not found",
|
|
239
|
+
"code": "NOT_FOUND",
|
|
240
|
+
"correlationId": "a1b2c3d4-...",
|
|
241
|
+
"timestamp": "2024-01-15T10:30:00.000Z"
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Custom error mappers
|
|
246
|
+
|
|
247
|
+
Register domain-specific error classes so they are mapped to the correct HTTP status:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { AllExceptionsFilter, ErrorMapper } from '@backendkit-labs/observability';
|
|
251
|
+
|
|
252
|
+
// In main.ts, after app.get(AllExceptionsFilter)
|
|
253
|
+
const filter = app.get(AllExceptionsFilter);
|
|
254
|
+
|
|
255
|
+
const domainMapper: ErrorMapper = (err) => {
|
|
256
|
+
if (err instanceof ResourceNotFoundError) {
|
|
257
|
+
return { statusCode: 404, message: err.message, code: 'NOT_FOUND' };
|
|
258
|
+
}
|
|
259
|
+
if (err instanceof ValidationError) {
|
|
260
|
+
return { statusCode: 422, message: err.message, code: 'VALIDATION_ERROR' };
|
|
261
|
+
}
|
|
262
|
+
return null; // fall through to next mapper or default handling
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
filter.addMapper(domainMapper);
|
|
266
|
+
app.useGlobalFilters(filter);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Multiple mappers are tried in registration order; the first non-`null` result wins.
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## @TrackPerformance decorator
|
|
274
|
+
|
|
275
|
+
Wraps any `async` method in an OpenTelemetry span. When OTel is not installed, it becomes a pure pass-through with zero overhead.
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import { TrackPerformance } from '@backendkit-labs/observability';
|
|
279
|
+
|
|
280
|
+
@Injectable()
|
|
281
|
+
export class ReportsService {
|
|
282
|
+
@TrackPerformance()
|
|
283
|
+
async generateReport(id: string): Promise<Report> {
|
|
284
|
+
// Span name: "ReportsService.generateReport"
|
|
285
|
+
return this.db.buildReport(id);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
@TrackPerformance({
|
|
289
|
+
operation: 'custom-operation-name',
|
|
290
|
+
attributes: { team: 'analytics', critical: true },
|
|
291
|
+
})
|
|
292
|
+
async exportToCsv(id: string): Promise<Buffer> {
|
|
293
|
+
return this.db.export(id);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## OpenTelemetry integration
|
|
301
|
+
|
|
302
|
+
Install `@opentelemetry/api` and configure an SDK (e.g. `@opentelemetry/sdk-node`) separately. This package auto-detects the API and attaches spans — no extra configuration needed here.
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
npm install @opentelemetry/api @opentelemetry/sdk-node
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
`CorrelationIdService.getTraceContext()` returns the active `traceId` and `spanId` when OTel is active, useful for log correlation:
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
const trace = this.correlation.getTraceContext();
|
|
312
|
+
// { traceId: 'abc123…', spanId: 'def456…' }
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## Circuit breaker behaviour
|
|
318
|
+
|
|
319
|
+
Both the HTTP log transport and the metrics transport use [`@backendkit-labs/circuit-breaker`](../circuit-breaker) to protect your application from cascading failures in the observability backend:
|
|
320
|
+
|
|
321
|
+
```
|
|
322
|
+
CLOSED ──(failure rate ≥ threshold)──► OPEN ──(openTimeoutMs)──► HALF_OPEN ──(probe succeeds)──► CLOSED
|
|
323
|
+
└─(probe fails)───► OPEN
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Transport defaults
|
|
327
|
+
|
|
328
|
+
| Option | Default | Description |
|
|
329
|
+
|---|---|---|
|
|
330
|
+
| `failureThreshold` | `60` | % of calls in the window that must fail to open the circuit |
|
|
331
|
+
| `slidingWindowSize` | `5` | Number of calls in the evaluation window |
|
|
332
|
+
| `minimumCalls` | `3` | Minimum calls before thresholds are evaluated |
|
|
333
|
+
| `openTimeoutMs` | `30 000` | Time to wait in OPEN before transitioning to HALF_OPEN |
|
|
334
|
+
| `halfOpenMaxCalls` | `1` | Probe calls allowed in HALF_OPEN |
|
|
335
|
+
| `slowCallThreshold` | `100` | % of slow calls to open the circuit (disabled by default) |
|
|
336
|
+
| `slowCallDurationMs` | `60 000` | Duration above which a call is considered slow |
|
|
337
|
+
|
|
338
|
+
### Customising the circuit breaker
|
|
339
|
+
|
|
340
|
+
Pass any subset of `CircuitBreakerConfig` via the `circuitBreaker` option. `name` and `isFailure` are managed internally.
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
import { CircuitBreakerState } from '@backendkit-labs/circuit-breaker';
|
|
344
|
+
|
|
345
|
+
ObservabilityModule.forRoot({
|
|
346
|
+
serviceName: 'my-api',
|
|
347
|
+
metrics: {
|
|
348
|
+
url: 'https://metrics.example.com/ingest',
|
|
349
|
+
circuitBreaker: {
|
|
350
|
+
failureThreshold: 80, // open only when 80% of calls fail
|
|
351
|
+
slidingWindowSize: 10,
|
|
352
|
+
minimumCalls: 5,
|
|
353
|
+
openTimeoutMs: 60_000, // stay open for 60 s
|
|
354
|
+
halfOpenMaxCalls: 2, // send 2 probes before closing
|
|
355
|
+
onStateChange: (from, to, metrics) => {
|
|
356
|
+
if (to === CircuitBreakerState.OPEN) {
|
|
357
|
+
alerting.trigger(`Metrics CB opened — failure rate ${metrics.failureRate}%`);
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The same `circuitBreaker` option is available on the `http` log transport.
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
## License
|
|
370
|
+
|
|
371
|
+
Apache-2.0
|