@backendkit-labs/observability 0.1.0

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