@stacksjs/bun-router 0.0.16 → 0.0.18

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.
Files changed (176) hide show
  1. package/dist/auth.d.ts +22 -8
  2. package/dist/chunk-1ahs68ys.js +18 -0
  3. package/dist/{chunk-0145g1me.js → chunk-cgptvjdf.js} +147 -75
  4. package/dist/chunk-g3ybefhg.js +1923 -0
  5. package/dist/cli.js +3 -2
  6. package/dist/container/container.d.ts +1 -0
  7. package/dist/container/contextual-binding.d.ts +0 -1
  8. package/dist/container/decorators.d.ts +2 -5
  9. package/dist/container/index.d.ts +16 -0
  10. package/dist/container/index.js +322 -0
  11. package/dist/container/service-provider.d.ts +3 -3
  12. package/dist/development/performance-profiler.d.ts +3 -3
  13. package/dist/development/route-debugger.d.ts +1 -1
  14. package/dist/development/route-inspector.d.ts +2 -2
  15. package/dist/file-serving/static-files.d.ts +16 -0
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +1159 -1380
  18. package/dist/middleware/cors.d.ts +9 -0
  19. package/dist/middleware/csrf.d.ts +7 -0
  20. package/dist/middleware/ddos_protection.d.ts +16 -1
  21. package/dist/middleware/rate_limit.d.ts +7 -1
  22. package/dist/middleware/request_tracer.d.ts +15 -9
  23. package/dist/middleware/response_cache.d.ts +2 -1
  24. package/dist/observability/integration.d.ts +1 -1
  25. package/dist/request/context.d.ts +12 -1
  26. package/dist/request/macros.d.ts +40 -2
  27. package/dist/response/macros.d.ts +4 -4
  28. package/dist/router/fluent-routing.d.ts +50 -13
  29. package/dist/router/handler-resolver.d.ts +8 -0
  30. package/dist/router/index.d.ts +9 -7
  31. package/dist/router/route-compiler.d.ts +8 -10
  32. package/dist/router/route-trie.d.ts +7 -1
  33. package/dist/router/router.d.ts +13 -34
  34. package/dist/router/validation-integration.d.ts +10 -4
  35. package/dist/testing/performance-testing.d.ts +2 -2
  36. package/dist/testing/test-client.d.ts +1 -1
  37. package/dist/testing/websocket-testing.d.ts +1 -1
  38. package/dist/types/middleware-types.d.ts +1 -1
  39. package/dist/types/route-inference.d.ts +9 -2
  40. package/dist/types.d.ts +62 -9
  41. package/dist/utils.d.ts +1 -1
  42. package/package.json +12 -3
  43. package/dist/router/fluent-router.d.ts +0 -315
  44. package/src/auth.ts +0 -469
  45. package/src/cache/lru-cache.ts +0 -457
  46. package/src/cache/middleware-memoization.ts +0 -531
  47. package/src/cache/route-cache-warmer.ts +0 -486
  48. package/src/cache/sqlite-cache.ts +0 -783
  49. package/src/cache/streaming-cache.ts +0 -572
  50. package/src/cli/colors.ts +0 -29
  51. package/src/cli/index.ts +0 -287
  52. package/src/cli/middleware.ts +0 -291
  53. package/src/cli/openapi.ts +0 -407
  54. package/src/cli/router.ts +0 -188
  55. package/src/cli/routes.ts +0 -265
  56. package/src/cli/utils.ts +0 -531
  57. package/src/cli.ts +0 -5
  58. package/src/config.ts +0 -322
  59. package/src/container/container.ts +0 -740
  60. package/src/container/contextual-binding.ts +0 -603
  61. package/src/container/decorators.ts +0 -359
  62. package/src/container/service-provider.ts +0 -596
  63. package/src/development/hot-reload.ts +0 -673
  64. package/src/development/index.ts +0 -499
  65. package/src/development/performance-profiler.ts +0 -717
  66. package/src/development/route-debugger.ts +0 -527
  67. package/src/development/route-inspector.ts +0 -749
  68. package/src/development/typescript-utilities.ts +0 -682
  69. package/src/docs.ts +0 -397
  70. package/src/errors/circuit-breaker.ts +0 -732
  71. package/src/errors/error-handler.ts +0 -569
  72. package/src/errors/error-reporting.ts +0 -672
  73. package/src/errors/exceptions.ts +0 -536
  74. package/src/errors/graceful-degradation.ts +0 -621
  75. package/src/errors/index.ts +0 -21
  76. package/src/errors/router-errors.ts +0 -632
  77. package/src/file-serving/static-files.ts +0 -581
  78. package/src/index.ts +0 -14
  79. package/src/middleware/auth.ts +0 -220
  80. package/src/middleware/content_security_policy.ts +0 -215
  81. package/src/middleware/cors.ts +0 -74
  82. package/src/middleware/csrf.ts +0 -118
  83. package/src/middleware/ddos_protection.ts +0 -255
  84. package/src/middleware/file_security.ts +0 -191
  85. package/src/middleware/file_upload.ts +0 -275
  86. package/src/middleware/helmet.ts +0 -268
  87. package/src/middleware/index.ts +0 -117
  88. package/src/middleware/input_validation.ts +0 -449
  89. package/src/middleware/json_body.ts +0 -37
  90. package/src/middleware/performance_alerting.ts +0 -538
  91. package/src/middleware/performance_dashboard.ts +0 -661
  92. package/src/middleware/performance_monitor.ts +0 -943
  93. package/src/middleware/pipeline.ts +0 -489
  94. package/src/middleware/rate_limit.ts +0 -245
  95. package/src/middleware/request_id.ts +0 -36
  96. package/src/middleware/request_signing.ts +0 -636
  97. package/src/middleware/request_tracer.ts +0 -636
  98. package/src/middleware/response_cache.ts +0 -743
  99. package/src/middleware/security.ts +0 -482
  100. package/src/middleware/security_suite.ts +0 -257
  101. package/src/middleware/session.ts +0 -91
  102. package/src/model-binding/index.ts +0 -18
  103. package/src/model-binding/model-middleware.ts +0 -425
  104. package/src/model-binding/model-registry.ts +0 -550
  105. package/src/model-binding.ts +0 -370
  106. package/src/model-resolver-factory.ts +0 -106
  107. package/src/observability/correlation.ts +0 -691
  108. package/src/observability/health-checks.ts +0 -729
  109. package/src/observability/index.ts +0 -184
  110. package/src/observability/integration.ts +0 -548
  111. package/src/observability/metrics.ts +0 -753
  112. package/src/observability/tracing.ts +0 -638
  113. package/src/optimization/bun-utilities.ts +0 -778
  114. package/src/query-builder-integration.ts +0 -137
  115. package/src/request/context.ts +0 -62
  116. package/src/request/enhanced-request.ts +0 -857
  117. package/src/request/macros.ts +0 -688
  118. package/src/response/macros.ts +0 -665
  119. package/src/response/response-factory.ts +0 -596
  120. package/src/router/api-routes.ts +0 -243
  121. package/src/router/file-based-routing.ts +0 -690
  122. package/src/router/file-streaming.ts +0 -383
  123. package/src/router/fluent-router.ts +0 -927
  124. package/src/router/fluent-routing.ts +0 -797
  125. package/src/router/group-organization.ts +0 -213
  126. package/src/router/handler-resolver.ts +0 -291
  127. package/src/router/http-methods.ts +0 -377
  128. package/src/router/index.ts +0 -187
  129. package/src/router/middleware-groups.ts +0 -222
  130. package/src/router/middleware-integration.ts +0 -399
  131. package/src/router/middleware.ts +0 -231
  132. package/src/router/model-binding.ts +0 -215
  133. package/src/router/optimized-route-matching.ts +0 -253
  134. package/src/router/route-building.ts +0 -221
  135. package/src/router/route-compiler.ts +0 -702
  136. package/src/router/route-matching.ts +0 -349
  137. package/src/router/route-trie.ts +0 -464
  138. package/src/router/router.ts +0 -1604
  139. package/src/router/server.ts +0 -462
  140. package/src/router/validation-integration.ts +0 -443
  141. package/src/router/view-rendering.ts +0 -233
  142. package/src/router/websocket.ts +0 -100
  143. package/src/routing/route-caching.ts +0 -402
  144. package/src/routing/route-throttling.ts +0 -469
  145. package/src/routing/subdomain-routing.ts +0 -492
  146. package/src/session/database-store.ts +0 -109
  147. package/src/session/file-store.ts +0 -148
  148. package/src/session/index.ts +0 -244
  149. package/src/session/memory-store.ts +0 -88
  150. package/src/session/redis-store.ts +0 -93
  151. package/src/streaming/index.ts +0 -17
  152. package/src/streaming/sse-handler.ts +0 -482
  153. package/src/streaming/stream-handler.ts +0 -552
  154. package/src/testing/auth-testing.ts +0 -446
  155. package/src/testing/file-upload-testing.ts +0 -543
  156. package/src/testing/index.ts +0 -10
  157. package/src/testing/middleware-testing.ts +0 -322
  158. package/src/testing/model-binding-testing.ts +0 -645
  159. package/src/testing/performance-testing.ts +0 -738
  160. package/src/testing/test-client.ts +0 -310
  161. package/src/testing/test-request.ts +0 -315
  162. package/src/testing/test-response.ts +0 -332
  163. package/src/testing/types.ts +0 -224
  164. package/src/testing/websocket-testing.ts +0 -590
  165. package/src/types/controller-types.ts +0 -385
  166. package/src/types/core.ts +0 -683
  167. package/src/types/middleware-types.ts +0 -419
  168. package/src/types/request-response-augmentation.ts +0 -489
  169. package/src/types/route-inference.ts +0 -357
  170. package/src/types.ts +0 -2067
  171. package/src/url.ts +0 -126
  172. package/src/utils/index.ts +0 -1
  173. package/src/utils/query-preservation.ts +0 -201
  174. package/src/utils.ts +0 -392
  175. package/src/validation/validator.ts +0 -685
  176. package/src/websocket/clustering.ts +0 -762
@@ -1,548 +0,0 @@
1
- /**
2
- * Observability & Monitoring - Integration Layer
3
- *
4
- * Unified integration for tracing, metrics, health checks, and correlation
5
- */
6
-
7
- import type { EnhancedRequest } from '../types'
8
- import type { CorrelationConfig } from './correlation'
9
- import type { HealthCheckConfig } from './health-checks'
10
- import type { MetricConfig } from './metrics'
11
- import type { TraceConfig } from './tracing'
12
- import process from 'node:process'
13
- import {
14
- CorrelationEndpoints,
15
- createCorrelationMiddleware,
16
- initializeCorrelation,
17
- } from './correlation'
18
- import {
19
-
20
- HealthEndpoints,
21
- initializeHealthChecks,
22
- } from './health-checks'
23
- import {
24
- createMetricsHandler,
25
- createMetricsMiddleware,
26
- initializeMetrics,
27
-
28
- } from './metrics'
29
- import {
30
- createTracingMiddleware,
31
- initializeTracer,
32
-
33
- } from './tracing'
34
-
35
- export interface ObservabilityConfig {
36
- tracing?: TraceConfig
37
- metrics?: MetricConfig
38
- healthChecks?: HealthCheckConfig
39
- correlation?: CorrelationConfig
40
- enableTracing?: boolean
41
- enableMetrics?: boolean
42
- enableHealthChecks?: boolean
43
- enableCorrelation?: boolean
44
- endpoints?: {
45
- metrics?: string
46
- health?: string
47
- ready?: string
48
- live?: string
49
- startup?: string
50
- trace?: string
51
- correlationStats?: string
52
- }
53
- }
54
-
55
- /**
56
- * Observability manager - coordinates all observability features
57
- */
58
- export class ObservabilityManager {
59
- private config: ObservabilityConfig
60
- private initialized = false
61
-
62
- constructor(config: ObservabilityConfig = {}) {
63
- this.config = {
64
- enableTracing: true,
65
- enableMetrics: true,
66
- enableHealthChecks: true,
67
- enableCorrelation: true,
68
- endpoints: {
69
- metrics: '/metrics',
70
- health: '/health',
71
- ready: '/health/ready',
72
- live: '/health/live',
73
- startup: '/health/startup',
74
- trace: '/trace',
75
- correlationStats: '/correlation/stats',
76
- },
77
- ...config,
78
- }
79
- }
80
-
81
- /**
82
- * Initialize all observability components
83
- */
84
- initialize(): void {
85
- if (this.initialized) {
86
- console.warn('Observability already initialized')
87
- return
88
- }
89
-
90
- // Initialize tracing
91
- if (this.config.enableTracing && this.config.tracing) {
92
- initializeTracer(this.config.tracing)
93
- console.log('✓ Distributed tracing initialized')
94
- }
95
-
96
- // Initialize metrics
97
- if (this.config.enableMetrics) {
98
- initializeMetrics(this.config.metrics)
99
- console.log('✓ Metrics collection initialized')
100
- }
101
-
102
- // Initialize health checks
103
- if (this.config.enableHealthChecks) {
104
- initializeHealthChecks(this.config.healthChecks)
105
- console.log('✓ Health checks initialized')
106
- }
107
-
108
- // Initialize correlation
109
- if (this.config.enableCorrelation) {
110
- initializeCorrelation(this.config.correlation)
111
- console.log('✓ Request correlation initialized')
112
- }
113
-
114
- this.initialized = true
115
- console.log('🔍 Observability & Monitoring fully initialized')
116
- }
117
-
118
- /**
119
- * Create observability middleware stack
120
- */
121
- createMiddleware(): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response> {
122
- const middlewares: Array<(req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>> = []
123
-
124
- // Add correlation middleware first (provides context for others)
125
- if (this.config.enableCorrelation) {
126
- middlewares.push(createCorrelationMiddleware(this.config.correlation))
127
- }
128
-
129
- // Add tracing middleware
130
- if (this.config.enableTracing) {
131
- middlewares.push(createTracingMiddleware(this.config.tracing))
132
- }
133
-
134
- // Add metrics middleware
135
- if (this.config.enableMetrics) {
136
- middlewares.push(createMetricsMiddleware(this.config.metrics))
137
- }
138
-
139
- return async (req: EnhancedRequest, next: () => Promise<Response>): Promise<Response> => {
140
- // Execute middleware stack
141
- let index = 0
142
-
143
- const executeNext = async (): Promise<Response> => {
144
- if (index >= middlewares.length) {
145
- return await next()
146
- }
147
-
148
- const middleware = middlewares[index++]!
149
- return await middleware(req, executeNext)
150
- }
151
-
152
- return await executeNext()
153
- }
154
- }
155
-
156
- /**
157
- * Create observability route handlers
158
- */
159
- createRouteHandlers(): Record<string, (req: EnhancedRequest) => Promise<Response>> {
160
- const handlers: Record<string, (req: EnhancedRequest) => Promise<Response>> = {}
161
-
162
- // Metrics endpoint
163
- if (this.config.enableMetrics && this.config.endpoints?.metrics) {
164
- handlers[this.config.endpoints.metrics] = createMetricsHandler()
165
- }
166
-
167
- // Health check endpoints
168
- if (this.config.enableHealthChecks) {
169
- if (this.config.endpoints?.health) {
170
- handlers[this.config.endpoints.health] = HealthEndpoints.health
171
- }
172
- if (this.config.endpoints?.ready) {
173
- handlers[this.config.endpoints.ready] = HealthEndpoints.ready
174
- }
175
- if (this.config.endpoints?.live) {
176
- handlers[this.config.endpoints.live] = HealthEndpoints.live
177
- }
178
- if (this.config.endpoints?.startup) {
179
- handlers[this.config.endpoints.startup] = HealthEndpoints.startup
180
- }
181
- }
182
-
183
- // Correlation endpoints
184
- if (this.config.enableCorrelation) {
185
- if (this.config.endpoints?.trace) {
186
- handlers[this.config.endpoints.trace] = CorrelationEndpoints.trace
187
- }
188
- if (this.config.endpoints?.correlationStats) {
189
- handlers[this.config.endpoints.correlationStats] = CorrelationEndpoints.stats
190
- }
191
- }
192
-
193
- return handlers
194
- }
195
-
196
- /**
197
- * Get observability status
198
- */
199
- getStatus(): {
200
- initialized: boolean
201
- components: {
202
- tracing: boolean
203
- metrics: boolean
204
- healthChecks: boolean
205
- correlation: boolean
206
- }
207
- endpoints: string[]
208
- } {
209
- const handlers = this.createRouteHandlers()
210
-
211
- return {
212
- initialized: this.initialized,
213
- components: {
214
- tracing: this.config.enableTracing || false,
215
- metrics: this.config.enableMetrics || false,
216
- healthChecks: this.config.enableHealthChecks || false,
217
- correlation: this.config.enableCorrelation || false,
218
- },
219
- endpoints: Object.keys(handlers),
220
- }
221
- }
222
- }
223
-
224
- /**
225
- * Global observability manager
226
- */
227
- let globalObservabilityManager: ObservabilityManager | null = null
228
-
229
- /**
230
- * Initialize global observability
231
- */
232
- export function initializeObservability(config?: ObservabilityConfig): ObservabilityManager {
233
- globalObservabilityManager = new ObservabilityManager(config)
234
- globalObservabilityManager.initialize()
235
- return globalObservabilityManager
236
- }
237
-
238
- /**
239
- * Get global observability manager
240
- */
241
- export function getObservabilityManager(): ObservabilityManager | null {
242
- return globalObservabilityManager
243
- }
244
-
245
- /**
246
- * Observability presets for common configurations
247
- */
248
- export const ObservabilityPresets = {
249
- /**
250
- * Development preset - console logging, basic metrics
251
- */
252
- development: (): ObservabilityConfig => ({
253
- enableTracing: true,
254
- enableMetrics: true,
255
- enableHealthChecks: true,
256
- enableCorrelation: true,
257
- tracing: {
258
- serviceName: 'bun-router-dev',
259
- environment: 'development',
260
- enableConsoleExporter: true,
261
- sampleRate: 1.0,
262
- },
263
- metrics: {
264
- enableDefaultMetrics: true,
265
- collectInterval: 15000,
266
- },
267
- healthChecks: {
268
- timeout: 3000,
269
- interval: 30000,
270
- },
271
- correlation: {
272
- enableLogging: true,
273
- logLevel: 'debug',
274
- },
275
- }),
276
-
277
- /**
278
- * Production preset - optimized for performance
279
- */
280
- production: (): ObservabilityConfig => ({
281
- enableTracing: true,
282
- enableMetrics: true,
283
- enableHealthChecks: true,
284
- enableCorrelation: true,
285
- tracing: {
286
- serviceName: 'bun-router',
287
- environment: 'production',
288
- enableConsoleExporter: false,
289
- enableOTLPExporter: true,
290
- sampleRate: 0.1, // 10% sampling
291
- },
292
- metrics: {
293
- enableDefaultMetrics: true,
294
- collectInterval: 30000,
295
- },
296
- healthChecks: {
297
- timeout: 5000,
298
- interval: 60000,
299
- },
300
- correlation: {
301
- enableLogging: false,
302
- },
303
- }),
304
-
305
- /**
306
- * Kubernetes preset - optimized for k8s deployment
307
- */
308
- kubernetes: (): ObservabilityConfig => ({
309
- enableTracing: true,
310
- enableMetrics: true,
311
- enableHealthChecks: true,
312
- enableCorrelation: true,
313
- tracing: {
314
- serviceName: process.env.SERVICE_NAME || 'bun-router',
315
- environment: process.env.ENVIRONMENT || 'production',
316
- enableOTLPExporter: true,
317
- otlpEndpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
318
- sampleRate: Number.parseFloat(process.env.OTEL_SAMPLE_RATE || '0.1'),
319
- },
320
- metrics: {
321
- prefix: process.env.METRICS_PREFIX,
322
- enableDefaultMetrics: true,
323
- collectInterval: 30000,
324
- },
325
- healthChecks: {
326
- timeout: 10000,
327
- gracePeriod: 30000,
328
- },
329
- correlation: {
330
- enableLogging: process.env.NODE_ENV !== 'production',
331
- },
332
- endpoints: {
333
- metrics: '/metrics',
334
- health: '/health',
335
- ready: '/health/ready',
336
- live: '/health/live',
337
- startup: '/health/startup',
338
- },
339
- }),
340
-
341
- /**
342
- * Microservices preset - enhanced correlation and tracing
343
- */
344
- microservices: (): ObservabilityConfig => ({
345
- enableTracing: true,
346
- enableMetrics: true,
347
- enableHealthChecks: true,
348
- enableCorrelation: true,
349
- tracing: {
350
- serviceName: process.env.SERVICE_NAME || 'bun-router-service',
351
- environment: process.env.ENVIRONMENT || 'production',
352
- enableJaegerExporter: true,
353
- jaegerEndpoint: process.env.JAEGER_ENDPOINT,
354
- sampleRate: 0.2, // Higher sampling for microservices
355
- },
356
- metrics: {
357
- enableDefaultMetrics: true,
358
- collectInterval: 15000,
359
- },
360
- healthChecks: {
361
- timeout: 5000,
362
- dependencies: [
363
- // Add your service dependencies here
364
- ],
365
- },
366
- correlation: {
367
- propagateHeaders: [
368
- 'x-correlation-id',
369
- 'x-request-id',
370
- 'x-trace-id',
371
- 'x-span-id',
372
- 'x-user-id',
373
- 'x-session-id',
374
- 'x-tenant-id',
375
- ],
376
- enableLogging: true,
377
- },
378
- }),
379
-
380
- /**
381
- * Minimal preset - basic observability
382
- */
383
- minimal: (): ObservabilityConfig => ({
384
- enableTracing: false,
385
- enableMetrics: true,
386
- enableHealthChecks: true,
387
- enableCorrelation: false,
388
- metrics: {
389
- enableDefaultMetrics: true,
390
- collectInterval: 60000,
391
- },
392
- healthChecks: {
393
- timeout: 3000,
394
- },
395
- endpoints: {
396
- metrics: '/metrics',
397
- health: '/health',
398
- },
399
- }),
400
- }
401
-
402
- /**
403
- * Router integration helpers
404
- */
405
- export const ObservabilityIntegration = {
406
- /**
407
- * Add observability to router
408
- */
409
- enhance: (router: any, config?: ObservabilityConfig): {
410
- manager: ObservabilityManager
411
- middleware: (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>
412
- handlers: Record<string, (req: EnhancedRequest) => Promise<Response>>
413
- status: ReturnType<ObservabilityManager['getStatus']>
414
- } => {
415
- const manager = initializeObservability(config)
416
- const middleware = manager.createMiddleware()
417
- const handlers = manager.createRouteHandlers()
418
-
419
- // Add middleware to router
420
- router.use(middleware)
421
-
422
- // Add observability endpoints
423
- Object.entries(handlers).forEach(([path, handler]: [string, (_req: EnhancedRequest) => Promise<Response>]) => {
424
- router.get(path, handler)
425
- })
426
-
427
- const result: {
428
- manager: ObservabilityManager
429
- middleware: (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>
430
- handlers: Record<string, (req: EnhancedRequest) => Promise<Response>>
431
- status: ReturnType<ObservabilityManager['getStatus']>
432
- } = {
433
- manager,
434
- middleware,
435
- handlers,
436
- status: manager.getStatus(),
437
- }
438
- return result
439
- },
440
-
441
- /**
442
- * Create observability-enabled route builder
443
- */
444
- createRouteBuilder: (config?: ObservabilityConfig) => {
445
- const manager = initializeObservability(config)
446
-
447
- return {
448
- /**
449
- * Create route with observability
450
- */
451
- route: (method: string, path: string, handler: (req: EnhancedRequest) => Promise<Response>): { method: string, path: string, handler: (req: EnhancedRequest) => Promise<Response> } => {
452
- const middleware = manager.createMiddleware()
453
-
454
- return {
455
- method,
456
- path,
457
- handler: async (req: EnhancedRequest) => {
458
- return await middleware(req, () => handler(req))
459
- },
460
- }
461
- },
462
-
463
- /**
464
- * Get observability endpoints
465
- */
466
- getEndpoints: (): Record<string, (req: EnhancedRequest) => Promise<Response>> => manager.createRouteHandlers(),
467
-
468
- /**
469
- * Get manager instance
470
- */
471
- getManager: (): ObservabilityManager => manager,
472
- }
473
- },
474
- }
475
-
476
- /**
477
- * Utility functions for observability
478
- */
479
- export const ObservabilityUtils = {
480
- /**
481
- * Create observability configuration from environment
482
- */
483
- fromEnvironment: (): ObservabilityConfig => {
484
- const preset = process.env.OBSERVABILITY_PRESET as keyof typeof ObservabilityPresets
485
-
486
- if (preset && ObservabilityPresets[preset]) {
487
- return ObservabilityPresets[preset]()
488
- }
489
-
490
- // Default configuration from environment variables
491
- return {
492
- enableTracing: process.env.ENABLE_TRACING !== 'false',
493
- enableMetrics: process.env.ENABLE_METRICS !== 'false',
494
- enableHealthChecks: process.env.ENABLE_HEALTH_CHECKS !== 'false',
495
- enableCorrelation: process.env.ENABLE_CORRELATION !== 'false',
496
- tracing: {
497
- serviceName: process.env.SERVICE_NAME || 'bun-router',
498
- environment: process.env.ENVIRONMENT || process.env.NODE_ENV || 'development',
499
- otlpEndpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
500
- jaegerEndpoint: process.env.JAEGER_ENDPOINT,
501
- sampleRate: Number.parseFloat(process.env.OTEL_SAMPLE_RATE || '1.0'),
502
- },
503
- metrics: {
504
- prefix: process.env.METRICS_PREFIX,
505
- collectInterval: Number.parseInt(process.env.METRICS_COLLECT_INTERVAL || '30000'),
506
- },
507
- correlation: {
508
- enableLogging: process.env.CORRELATION_LOGGING !== 'false',
509
- },
510
- }
511
- },
512
-
513
- /**
514
- * Validate observability configuration
515
- */
516
- validateConfig: (config: ObservabilityConfig): string[] => {
517
- const errors: string[] = []
518
-
519
- if (config.tracing?.sampleRate && (config.tracing.sampleRate < 0 || config.tracing.sampleRate > 1)) {
520
- errors.push('Tracing sample rate must be between 0 and 1')
521
- }
522
-
523
- if (config.metrics?.collectInterval && config.metrics.collectInterval < 1000) {
524
- errors.push('Metrics collect interval must be at least 1000ms')
525
- }
526
-
527
- if (config.healthChecks?.timeout && config.healthChecks.timeout < 100) {
528
- errors.push('Health check timeout must be at least 100ms')
529
- }
530
-
531
- return errors
532
- },
533
-
534
- /**
535
- * Get observability summary
536
- */
537
- getSummary: (): any => {
538
- const manager = getObservabilityManager()
539
- if (!manager) {
540
- return { status: 'not_initialized' }
541
- }
542
-
543
- return {
544
- status: 'initialized',
545
- ...manager.getStatus(),
546
- }
547
- },
548
- }