@gravito/monitor 1.0.0-beta.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.
@@ -0,0 +1,516 @@
1
+ import { GravitoContext, GravitoOrbit, PlanetCore } from 'gravito-core';
2
+
3
+ /**
4
+ * @gravito/monitor - Configuration Types
5
+ */
6
+ /**
7
+ * Health check result
8
+ */
9
+ interface HealthCheckResult {
10
+ status: 'healthy' | 'unhealthy' | 'degraded';
11
+ message?: string;
12
+ latency?: number;
13
+ details?: Record<string, unknown>;
14
+ }
15
+ /**
16
+ * Health check function signature
17
+ */
18
+ type HealthCheckFn = () => Promise<HealthCheckResult> | HealthCheckResult;
19
+ /**
20
+ * Health configuration
21
+ */
22
+ interface HealthConfig {
23
+ /** Enable health endpoints (default: true) */
24
+ enabled?: boolean;
25
+ /** Path for full health check (default: /health) */
26
+ path?: string;
27
+ /** Path for Kubernetes readiness probe (default: /ready) */
28
+ readyPath?: string;
29
+ /** Path for Kubernetes liveness probe (default: /live) */
30
+ livePath?: string;
31
+ /** Timeout for individual health checks in ms (default: 5000) */
32
+ timeout?: number;
33
+ /** Cache health check results for ms (default: 0 = no cache) */
34
+ cacheTtl?: number;
35
+ }
36
+ /**
37
+ * Metric definition options
38
+ */
39
+ interface MetricOptions {
40
+ name: string;
41
+ help: string;
42
+ labels?: string[];
43
+ buckets?: number[];
44
+ percentiles?: number[];
45
+ }
46
+ /**
47
+ * Metrics configuration
48
+ */
49
+ interface MetricsConfig {
50
+ /** Enable metrics endpoint (default: true) */
51
+ enabled?: boolean;
52
+ /** Path for metrics endpoint (default: /metrics) */
53
+ path?: string;
54
+ /** Collect default runtime metrics (default: true) */
55
+ defaultMetrics?: boolean;
56
+ /** Prefix for all metric names (default: gravito_) */
57
+ prefix?: string;
58
+ /** Custom labels to add to all metrics */
59
+ defaultLabels?: Record<string, string>;
60
+ }
61
+ /**
62
+ * Tracing configuration
63
+ */
64
+ interface TracingConfig {
65
+ /** Enable tracing (default: false) */
66
+ enabled?: boolean;
67
+ /** Service name for tracing */
68
+ serviceName?: string;
69
+ /** Service version */
70
+ serviceVersion?: string;
71
+ /** OTLP endpoint URL (default: http://localhost:4318/v1/traces) */
72
+ endpoint?: string;
73
+ /** Sample rate 0.0 - 1.0 (default: 1.0) */
74
+ sampleRate?: number;
75
+ /** Additional resource attributes */
76
+ resourceAttributes?: Record<string, string>;
77
+ /** Trace HTTP requests automatically (default: true) */
78
+ traceHttp?: boolean;
79
+ /** Headers to propagate in traces */
80
+ propagateHeaders?: string[];
81
+ }
82
+ /**
83
+ * Monitor configuration
84
+ */
85
+ interface MonitorConfig {
86
+ /** Health check configuration */
87
+ health?: HealthConfig;
88
+ /** Metrics configuration */
89
+ metrics?: MetricsConfig;
90
+ /** Tracing configuration */
91
+ tracing?: TracingConfig;
92
+ }
93
+ /**
94
+ * Define monitor configuration (type helper)
95
+ */
96
+ declare function defineMonitorConfig(config: MonitorConfig): MonitorConfig;
97
+
98
+ /**
99
+ * @gravito/monitor - Health Check Registry
100
+ *
101
+ * Manages health check registrations and executions
102
+ */
103
+
104
+ interface HealthReport {
105
+ status: 'healthy' | 'unhealthy' | 'degraded';
106
+ timestamp: string;
107
+ uptime: number;
108
+ checks: Record<string, HealthCheckResult & {
109
+ name: string;
110
+ }>;
111
+ }
112
+ /**
113
+ * HealthRegistry manages all health checks
114
+ */
115
+ declare class HealthRegistry {
116
+ private checks;
117
+ private startTime;
118
+ private cachedReport;
119
+ private cacheExpiry;
120
+ private timeout;
121
+ private cacheTtl;
122
+ constructor(config?: HealthConfig);
123
+ /**
124
+ * Register a health check
125
+ */
126
+ register(name: string, check: HealthCheckFn): this;
127
+ /**
128
+ * Unregister a health check
129
+ */
130
+ unregister(name: string): boolean;
131
+ /**
132
+ * Get all registered check names
133
+ */
134
+ getCheckNames(): string[];
135
+ /**
136
+ * Execute a single health check with timeout
137
+ */
138
+ private executeCheck;
139
+ /**
140
+ * Execute all health checks and generate report
141
+ */
142
+ check(): Promise<HealthReport>;
143
+ /**
144
+ * Simple liveness check (is the process running?)
145
+ */
146
+ liveness(): Promise<{
147
+ status: 'healthy' | 'unhealthy';
148
+ }>;
149
+ /**
150
+ * Readiness check (is the app ready to serve traffic?)
151
+ * By default, requires all checks to be healthy
152
+ */
153
+ readiness(): Promise<{
154
+ status: 'healthy' | 'unhealthy';
155
+ reason?: string;
156
+ }>;
157
+ }
158
+
159
+ /**
160
+ * @gravito/monitor - Health Controller
161
+ *
162
+ * HTTP handlers for health check endpoints
163
+ */
164
+
165
+ /**
166
+ * HealthController handles health check HTTP endpoints
167
+ */
168
+ declare class HealthController {
169
+ private registry;
170
+ constructor(registry: HealthRegistry);
171
+ /**
172
+ * GET /health - Full health check with all registered checks
173
+ */
174
+ health(c: GravitoContext): Promise<Response>;
175
+ /**
176
+ * GET /ready - Kubernetes readiness probe
177
+ * Returns 200 if ready to serve traffic, 503 otherwise
178
+ */
179
+ ready(c: GravitoContext): Promise<Response>;
180
+ /**
181
+ * GET /live - Kubernetes liveness probe
182
+ * Returns 200 if the process is alive
183
+ */
184
+ live(c: GravitoContext): Promise<Response>;
185
+ }
186
+
187
+ /**
188
+ * @gravito/monitor - Built-in Health Checks
189
+ */
190
+
191
+ /**
192
+ * Create a database health check
193
+ */
194
+ declare function createDatabaseCheck(connectionFn: () => Promise<boolean> | boolean): HealthCheckFn;
195
+ /**
196
+ * Create a Redis health check
197
+ */
198
+ declare function createRedisCheck(pingFn: () => Promise<string> | string): HealthCheckFn;
199
+ /**
200
+ * Create a memory usage health check
201
+ */
202
+ declare function createMemoryCheck(options?: {
203
+ maxHeapUsedPercent?: number;
204
+ }): HealthCheckFn;
205
+ /**
206
+ * Create a custom HTTP endpoint health check
207
+ */
208
+ declare function createHttpCheck(url: string, options?: {
209
+ timeout?: number;
210
+ expectedStatus?: number;
211
+ method?: string;
212
+ }): HealthCheckFn;
213
+ /**
214
+ * Create a disk space health check
215
+ */
216
+ declare function createDiskCheck(options?: {
217
+ path?: string;
218
+ minFreePercent?: number;
219
+ }): HealthCheckFn;
220
+
221
+ /**
222
+ * @gravito/monitor - Metrics Registry
223
+ *
224
+ * Manages metric collection and Prometheus exposition
225
+ */
226
+
227
+ interface MetricValue {
228
+ value: number;
229
+ labels: Record<string, string>;
230
+ timestamp?: number;
231
+ }
232
+ /**
233
+ * Counter metric - monotonically increasing value
234
+ */
235
+ declare class Counter {
236
+ readonly name: string;
237
+ readonly help: string;
238
+ readonly labelNames: string[];
239
+ private values;
240
+ constructor(name: string, help: string, labelNames?: string[]);
241
+ /**
242
+ * Increment the counter
243
+ */
244
+ inc(labels?: Record<string, string>, delta?: number): void;
245
+ /**
246
+ * Get current values
247
+ */
248
+ getValues(): MetricValue[];
249
+ /**
250
+ * Reset all values
251
+ */
252
+ reset(): void;
253
+ private labelsToKey;
254
+ private keyToLabels;
255
+ }
256
+ /**
257
+ * Gauge metric - value that can go up or down
258
+ */
259
+ declare class Gauge {
260
+ readonly name: string;
261
+ readonly help: string;
262
+ readonly labelNames: string[];
263
+ private values;
264
+ constructor(name: string, help: string, labelNames?: string[]);
265
+ /**
266
+ * Set the gauge value
267
+ */
268
+ set(value: number, labels?: Record<string, string>): void;
269
+ /**
270
+ * Increment the gauge
271
+ */
272
+ inc(labels?: Record<string, string>, delta?: number): void;
273
+ /**
274
+ * Decrement the gauge
275
+ */
276
+ dec(labels?: Record<string, string>, delta?: number): void;
277
+ /**
278
+ * Get current values
279
+ */
280
+ getValues(): MetricValue[];
281
+ private labelsToKey;
282
+ private keyToLabels;
283
+ }
284
+ /**
285
+ * Histogram metric - tracks distribution of values
286
+ */
287
+ declare class Histogram {
288
+ readonly name: string;
289
+ readonly help: string;
290
+ readonly labelNames: string[];
291
+ private bucketCounts;
292
+ private sums;
293
+ private counts;
294
+ readonly buckets: number[];
295
+ constructor(name: string, help: string, labelNames?: string[], buckets?: number[]);
296
+ /**
297
+ * Observe a value
298
+ */
299
+ observe(value: number, labels?: Record<string, string>): void;
300
+ /**
301
+ * Start a timer and return a function to stop it
302
+ */
303
+ startTimer(labels?: Record<string, string>): () => void;
304
+ /**
305
+ * Get values for Prometheus format
306
+ */
307
+ getValues(): {
308
+ buckets: Map<string, Map<number, number>>;
309
+ sums: Map<string, number>;
310
+ counts: Map<string, number>;
311
+ };
312
+ private labelsToKey;
313
+ }
314
+ /**
315
+ * MetricsRegistry manages all metrics
316
+ */
317
+ declare class MetricsRegistry {
318
+ private counters;
319
+ private gauges;
320
+ private histograms;
321
+ private startTime;
322
+ private prefix;
323
+ private defaultLabels;
324
+ private collectDefaultMetrics;
325
+ constructor(config?: MetricsConfig);
326
+ /**
327
+ * Create or get a counter
328
+ */
329
+ counter(options: MetricOptions): Counter;
330
+ /**
331
+ * Create or get a gauge
332
+ */
333
+ gauge(options: MetricOptions): Gauge;
334
+ /**
335
+ * Create or get a histogram
336
+ */
337
+ histogram(options: MetricOptions): Histogram;
338
+ /**
339
+ * Initialize default runtime metrics
340
+ */
341
+ private initDefaultMetrics;
342
+ /**
343
+ * Update default metrics with current values
344
+ */
345
+ private updateDefaultMetrics;
346
+ /**
347
+ * Export metrics in Prometheus format
348
+ */
349
+ toPrometheus(): string;
350
+ private formatLabels;
351
+ private keyToLabels;
352
+ }
353
+
354
+ /**
355
+ * @gravito/monitor - Metrics Controller
356
+ */
357
+
358
+ /**
359
+ * MetricsController handles the /metrics endpoint
360
+ */
361
+ declare class MetricsController {
362
+ private registry;
363
+ constructor(registry: MetricsRegistry);
364
+ /**
365
+ * GET /metrics - Prometheus metrics endpoint
366
+ */
367
+ metrics(c: GravitoContext): Promise<Response>;
368
+ }
369
+
370
+ /**
371
+ * @gravito/monitor - HTTP Metrics Middleware
372
+ */
373
+
374
+ /**
375
+ * Create middleware that collects HTTP metrics
376
+ */
377
+ declare function createHttpMetricsMiddleware(registry: MetricsRegistry): (c: GravitoContext, next: () => Promise<void>) => Promise<Response | undefined>;
378
+
379
+ /**
380
+ * @gravito/monitor - Tracing Manager
381
+ *
382
+ * OpenTelemetry OTLP tracing integration
383
+ */
384
+
385
+ /**
386
+ * Span interface for tracing
387
+ */
388
+ interface Span {
389
+ name: string;
390
+ traceId: string;
391
+ spanId: string;
392
+ parentSpanId?: string;
393
+ startTime: number;
394
+ endTime?: number;
395
+ attributes: Record<string, string | number | boolean>;
396
+ status: 'ok' | 'error' | 'unset';
397
+ events: SpanEvent[];
398
+ }
399
+ interface SpanEvent {
400
+ name: string;
401
+ timestamp: number;
402
+ attributes?: Record<string, string | number | boolean>;
403
+ }
404
+ /**
405
+ * Span context for propagation
406
+ */
407
+ interface SpanContext {
408
+ traceId: string;
409
+ spanId: string;
410
+ traceFlags: number;
411
+ }
412
+ /**
413
+ * TracingManager handles distributed tracing
414
+ */
415
+ declare class TracingManager {
416
+ private otelSdk;
417
+ private spans;
418
+ private isInitialized;
419
+ private serviceName;
420
+ private serviceVersion;
421
+ private endpoint;
422
+ private resourceAttributes;
423
+ constructor(config?: TracingConfig);
424
+ /**
425
+ * Initialize OpenTelemetry SDK if available
426
+ */
427
+ initialize(): Promise<void>;
428
+ /**
429
+ * Shutdown tracing
430
+ */
431
+ shutdown(): Promise<void>;
432
+ /**
433
+ * Start a new span
434
+ */
435
+ startSpan(name: string, options?: {
436
+ attributes?: Record<string, string | number | boolean>;
437
+ parentSpan?: Span;
438
+ }): Span;
439
+ /**
440
+ * End a span
441
+ */
442
+ endSpan(span: Span, status?: 'ok' | 'error'): void;
443
+ /**
444
+ * Add an event to a span
445
+ */
446
+ addEvent(span: Span, name: string, attributes?: Record<string, string | number | boolean>): void;
447
+ /**
448
+ * Set span attribute
449
+ */
450
+ setAttribute(span: Span, key: string, value: string | number | boolean): void;
451
+ /**
452
+ * Get the currently active span
453
+ */
454
+ getActiveSpan(): Span | null;
455
+ /**
456
+ * Extract trace context from headers
457
+ */
458
+ extractContext(headers: Headers): SpanContext | null;
459
+ /**
460
+ * Inject trace context into headers
461
+ */
462
+ injectContext(headers: Headers, span: Span): void;
463
+ /**
464
+ * Get all collected spans (for debugging)
465
+ */
466
+ getSpans(): Span[];
467
+ /**
468
+ * Clear collected spans
469
+ */
470
+ clearSpans(): void;
471
+ }
472
+
473
+ /**
474
+ * @gravito/monitor - Tracing Middleware
475
+ */
476
+
477
+ /**
478
+ * Create middleware that traces HTTP requests
479
+ */
480
+ declare function createTracingMiddleware(tracer: TracingManager): (c: GravitoContext, next: () => Promise<void>) => Promise<Response | undefined>;
481
+
482
+ /**
483
+ * @gravito/monitor - MonitorOrbit Plugin
484
+ *
485
+ * Gravito Orbit plugin for observability (Health, Metrics, Tracing)
486
+ */
487
+
488
+ /**
489
+ * Monitor service container
490
+ */
491
+ interface MonitorService {
492
+ health: HealthRegistry;
493
+ metrics: MetricsRegistry;
494
+ tracing: TracingManager;
495
+ }
496
+ /**
497
+ * MonitorOrbit - Observability plugin for Gravito
498
+ */
499
+ declare class MonitorOrbit implements GravitoOrbit {
500
+ readonly name = "monitor";
501
+ private userConfig;
502
+ private healthRegistry;
503
+ private metricsRegistry;
504
+ private tracingManager;
505
+ constructor(config?: MonitorConfig);
506
+ /**
507
+ * Install the orbit (required by GravitoOrbit interface)
508
+ */
509
+ install(core: PlanetCore): Promise<void>;
510
+ /**
511
+ * Shutdown hook
512
+ */
513
+ shutdown(): Promise<void>;
514
+ }
515
+
516
+ export { Counter, Gauge, type HealthCheckFn, type HealthCheckResult, type HealthConfig, HealthController, HealthRegistry, type HealthReport, Histogram, type MetricOptions, type MetricsConfig, MetricsController, MetricsRegistry, type MonitorConfig, MonitorOrbit, type MonitorService, type Span, type SpanContext, type SpanEvent, type TracingConfig, TracingManager, createDatabaseCheck, createDiskCheck, createHttpCheck, createHttpMetricsMiddleware, createMemoryCheck, createRedisCheck, createTracingMiddleware, defineMonitorConfig };