@gravito/core 1.6.1 → 2.0.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 +100 -6
- package/README.zh-TW.md +101 -6
- package/dist/Application.d.ts +256 -0
- package/dist/CommandKernel.d.ts +33 -0
- package/dist/ConfigManager.d.ts +65 -0
- package/dist/Container/RequestScopeManager.d.ts +62 -0
- package/dist/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/Container.d.ts +153 -0
- package/dist/ErrorHandler.d.ts +66 -0
- package/dist/Event.d.ts +5 -0
- package/dist/EventManager.d.ts +123 -0
- package/dist/GlobalErrorHandlers.d.ts +47 -0
- package/dist/GravitoServer.d.ts +28 -0
- package/dist/HookManager.d.ts +435 -0
- package/dist/Listener.d.ts +4 -0
- package/dist/Logger.d.ts +20 -0
- package/dist/PlanetCore.d.ts +402 -0
- package/dist/RequestContext.d.ts +97 -0
- package/dist/Route.d.ts +36 -0
- package/dist/Router.d.ts +270 -0
- package/dist/ServiceProvider.d.ts +178 -0
- package/dist/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/adapters/bun/AdaptiveAdapter.d.ts +99 -0
- package/dist/adapters/bun/BunContext.d.ts +54 -0
- package/dist/adapters/bun/BunNativeAdapter.d.ts +66 -0
- package/dist/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/adapters/bun/RadixRouter.d.ts +32 -0
- package/dist/adapters/bun/index.d.ts +7 -0
- package/dist/adapters/bun/types.d.ts +20 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/types.d.ts +235 -0
- package/dist/binary/BinaryUtils.d.ts +105 -0
- package/dist/binary/index.d.ts +5 -0
- package/dist/cli/queue-commands.d.ts +6 -0
- package/dist/compat/async-local-storage.browser.d.ts +9 -0
- package/dist/compat/async-local-storage.d.ts +7 -0
- package/dist/compat/crypto.browser.d.ts +5 -0
- package/dist/compat/crypto.d.ts +6 -0
- package/dist/compat.cjs +42 -11
- package/dist/compat.cjs.map +9 -0
- package/dist/compat.d.ts +23 -1
- package/dist/compat.js +3 -0
- package/dist/compat.js.map +9 -0
- package/dist/engine/AOTRouter.d.ts +139 -0
- package/dist/engine/FastContext.d.ts +141 -0
- package/dist/engine/Gravito.d.ts +131 -0
- package/dist/engine/MinimalContext.d.ts +102 -0
- package/dist/engine/analyzer.d.ts +113 -0
- package/dist/engine/constants.d.ts +23 -0
- package/dist/engine/index.cjs +576 -647
- package/dist/engine/index.cjs.map +22 -0
- package/dist/engine/index.d.ts +14 -910
- package/dist/engine/index.js +576 -623
- package/dist/engine/index.js.map +22 -0
- package/dist/engine/path.d.ts +26 -0
- package/dist/engine/pool.d.ts +83 -0
- package/dist/engine/types.d.ts +149 -0
- package/dist/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/events/BackpressureManager.d.ts +215 -0
- package/dist/events/CircuitBreaker.d.ts +229 -0
- package/dist/events/DeadLetterQueue.d.ts +219 -0
- package/dist/events/EventBackend.d.ts +12 -0
- package/dist/events/EventOptions.d.ts +204 -0
- package/dist/events/EventPriorityQueue.d.ts +63 -0
- package/dist/events/FlowControlStrategy.d.ts +109 -0
- package/dist/events/IdempotencyCache.d.ts +60 -0
- package/dist/events/MessageQueueBridge.d.ts +184 -0
- package/dist/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/events/RetryScheduler.d.ts +104 -0
- package/dist/events/WorkerPool.d.ts +98 -0
- package/dist/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/events/aggregation/index.d.ts +10 -0
- package/dist/events/aggregation/types.d.ts +117 -0
- package/dist/events/index.d.ts +26 -0
- package/dist/events/observability/EventMetrics.d.ts +132 -0
- package/dist/events/observability/EventTracer.d.ts +68 -0
- package/dist/events/observability/EventTracing.d.ts +161 -0
- package/dist/events/observability/OTelEventMetrics.d.ts +332 -0
- package/dist/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/events/observability/index.d.ts +24 -0
- package/dist/events/observability/metrics-types.d.ts +16 -0
- package/dist/events/queue-core.d.ts +77 -0
- package/dist/events/task-executor.d.ts +51 -0
- package/dist/events/types.d.ts +134 -0
- package/dist/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/exceptions/GravitoException.d.ts +23 -0
- package/dist/exceptions/HttpException.d.ts +9 -0
- package/dist/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/exceptions/ValidationException.d.ts +22 -0
- package/dist/exceptions/index.d.ts +7 -0
- package/dist/ffi/NativeAccelerator.d.ts +69 -0
- package/dist/ffi/NativeHasher.d.ts +139 -0
- package/dist/ffi/cbor-fallback.d.ts +96 -0
- package/dist/ffi/hash-fallback.d.ts +33 -0
- package/dist/ffi/index.cjs +621 -0
- package/dist/ffi/index.cjs.map +14 -0
- package/dist/ffi/index.d.ts +10 -0
- package/dist/ffi/index.js +602 -0
- package/dist/ffi/index.js.map +14 -0
- package/dist/ffi/types.d.ts +135 -0
- package/dist/health/HealthProvider.d.ts +67 -0
- package/dist/helpers/Arr.d.ts +19 -0
- package/dist/helpers/Str.d.ts +38 -0
- package/dist/helpers/data.d.ts +25 -0
- package/dist/helpers/errors.d.ts +34 -0
- package/dist/helpers/response.d.ts +41 -0
- package/dist/helpers.d.ts +338 -0
- package/dist/hooks/ActionManager.d.ts +132 -0
- package/dist/hooks/AsyncDetector.d.ts +84 -0
- package/dist/hooks/FilterManager.d.ts +71 -0
- package/dist/hooks/MigrationWarner.d.ts +24 -0
- package/dist/hooks/dlq-operations.d.ts +60 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/types.d.ts +107 -0
- package/dist/http/CookieJar.d.ts +51 -0
- package/dist/http/cookie.d.ts +29 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/{compat-CI8hiulX.d.cts → http/types.d.ts} +29 -16
- package/dist/index.browser.d.ts +34 -0
- package/dist/index.cjs +10525 -11171
- package/dist/index.cjs.map +168 -0
- package/dist/index.d.ts +58 -10981
- package/dist/index.js +10861 -10997
- package/dist/index.js.map +168 -0
- package/dist/observability/QueueDashboard.d.ts +136 -0
- package/dist/observability/contracts.d.ts +137 -0
- package/dist/observability/index.d.ts +13 -0
- package/dist/reliability/DeadLetterQueueManager.d.ts +349 -0
- package/dist/reliability/RetryPolicy.d.ts +217 -0
- package/dist/reliability/index.d.ts +6 -0
- package/dist/router/ControllerDispatcher.d.ts +12 -0
- package/dist/router/RequestValidator.d.ts +20 -0
- package/dist/runtime/adapter-bun.d.ts +12 -0
- package/dist/runtime/adapter-deno.d.ts +12 -0
- package/dist/runtime/adapter-node.d.ts +12 -0
- package/dist/runtime/adapter-unknown.d.ts +13 -0
- package/dist/runtime/archive.d.ts +17 -0
- package/dist/runtime/compression.d.ts +21 -0
- package/dist/runtime/deep-equals.d.ts +56 -0
- package/dist/runtime/detection.d.ts +22 -0
- package/dist/runtime/escape.d.ts +34 -0
- package/dist/runtime/index.browser.d.ts +20 -0
- package/dist/runtime/index.d.ts +44 -0
- package/dist/runtime/markdown.d.ts +44 -0
- package/dist/runtime/types.d.ts +436 -0
- package/dist/runtime-helpers.d.ts +67 -0
- package/dist/runtime.d.ts +11 -0
- package/dist/security/Encrypter.d.ts +33 -0
- package/dist/security/Hasher.d.ts +29 -0
- package/dist/testing/HttpTester.d.ts +39 -0
- package/dist/testing/TestResponse.d.ts +78 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/transpiler-utils.d.ts +170 -0
- package/dist/types/events.d.ts +94 -0
- package/dist/types.d.ts +13 -0
- package/package.json +21 -52
- package/src/ffi/native/cbor.c +1148 -0
- package/dist/Metrics-VOWWRNNR.js +0 -219
- package/dist/chunk-R5U7XKVJ.js +0 -16
- package/dist/compat-CI8hiulX.d.ts +0 -376
- package/dist/compat.d.cts +0 -1
- package/dist/engine/index.d.cts +0 -922
- package/dist/index.d.cts +0 -11008
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Event System Tracer
|
|
3
|
+
*
|
|
4
|
+
* Manages OpenTelemetry distributed tracing for event dispatch.
|
|
5
|
+
*/
|
|
6
|
+
import type { Span } from '@opentelemetry/api';
|
|
7
|
+
/**
|
|
8
|
+
* Event tracer for distributed tracing support.
|
|
9
|
+
*
|
|
10
|
+
* Integrates with OpenTelemetry to provide:
|
|
11
|
+
* - Event dispatch tracing
|
|
12
|
+
* - Listener execution tracing
|
|
13
|
+
* - Error recording
|
|
14
|
+
* - Span hierarchy
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export declare class EventTracer {
|
|
19
|
+
private tracer;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new EventTracer instance.
|
|
22
|
+
*
|
|
23
|
+
* @param tracerName - Name of the tracer (default: '@gravito/core')
|
|
24
|
+
*/
|
|
25
|
+
constructor(tracerName?: string);
|
|
26
|
+
/**
|
|
27
|
+
* Start a span for event dispatch.
|
|
28
|
+
*
|
|
29
|
+
* @param eventName - Name of the event
|
|
30
|
+
* @param callbackCount - Number of callbacks registered for this event
|
|
31
|
+
* @param priority - Priority level (high, normal, low)
|
|
32
|
+
* @returns Span for the event dispatch
|
|
33
|
+
*/
|
|
34
|
+
startDispatchSpan(eventName: string, callbackCount: number, priority: string): Span;
|
|
35
|
+
/**
|
|
36
|
+
* Start a span for listener execution.
|
|
37
|
+
*
|
|
38
|
+
* @param _parentSpan - Parent span for this listener
|
|
39
|
+
* @param eventName - Name of the event
|
|
40
|
+
* @param listenerIndex - Index of the listener in the callback list
|
|
41
|
+
* @returns Child span for the listener execution
|
|
42
|
+
*/
|
|
43
|
+
startListenerSpan(_parentSpan: Span, eventName: string, listenerIndex: number): Span;
|
|
44
|
+
/**
|
|
45
|
+
* Record an error in the span.
|
|
46
|
+
*
|
|
47
|
+
* @param span - Span to record error in
|
|
48
|
+
* @param error - Error that occurred
|
|
49
|
+
*/
|
|
50
|
+
recordError(span: Span, error: Error): void;
|
|
51
|
+
/**
|
|
52
|
+
* End a span with a specific status.
|
|
53
|
+
*
|
|
54
|
+
* @param span - Span to end
|
|
55
|
+
* @param status - Status ('ok' or 'error')
|
|
56
|
+
*/
|
|
57
|
+
endSpan(span: Span, status?: 'ok' | 'error'): void;
|
|
58
|
+
/**
|
|
59
|
+
* Create a timer span for measuring duration.
|
|
60
|
+
*
|
|
61
|
+
* @param eventName - Name of the event
|
|
62
|
+
* @returns Object with span and duration recording function
|
|
63
|
+
*/
|
|
64
|
+
startTimer(eventName: string): {
|
|
65
|
+
span: Span;
|
|
66
|
+
endTimer: (status?: 'ok' | 'error') => void;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Event System Tracing
|
|
3
|
+
*
|
|
4
|
+
* 使用 OpenTelemetry API 實現事件系統的分佈式追蹤。
|
|
5
|
+
* 提供事件派發、監聽器執行與隊列操作的端到端追蹤。
|
|
6
|
+
*/
|
|
7
|
+
import type { Span } from '@opentelemetry/api';
|
|
8
|
+
/**
|
|
9
|
+
* 事件追蹤配置
|
|
10
|
+
*/
|
|
11
|
+
export interface EventTracingConfig {
|
|
12
|
+
/** Tracer 名稱 */
|
|
13
|
+
tracerName?: string;
|
|
14
|
+
/** 是否記錄詳細的 payload 信息 */
|
|
15
|
+
capturePayload?: boolean;
|
|
16
|
+
/** 最大 payload 大小(字節) */
|
|
17
|
+
maxPayloadSize?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 事件追蹤管理器
|
|
21
|
+
*
|
|
22
|
+
* 提供事件系統的分佈式追蹤能力,包括:
|
|
23
|
+
* - 事件派發追蹤
|
|
24
|
+
* - 監聽器執行追蹤
|
|
25
|
+
* - 隊列操作追蹤
|
|
26
|
+
* - 異常記錄與追蹤
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare class EventTracing {
|
|
31
|
+
private tracer;
|
|
32
|
+
private config;
|
|
33
|
+
constructor(config?: EventTracingConfig);
|
|
34
|
+
/**
|
|
35
|
+
* 為事件派發創建追蹤 Span
|
|
36
|
+
*
|
|
37
|
+
* @param eventName - 事件名稱
|
|
38
|
+
* @param listenerCount - 監聽器數量
|
|
39
|
+
* @param priority - 優先級(high, normal, low)
|
|
40
|
+
* @param payload - 事件負載(可選,用於調試)
|
|
41
|
+
* @returns Span 實例
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const tracer = new EventTracing()
|
|
46
|
+
* const span = tracer.startDispatchSpan('order:created', 3, 'high', orderData)
|
|
47
|
+
* try {
|
|
48
|
+
* // 派發事件
|
|
49
|
+
* await dispatchEvent()
|
|
50
|
+
* tracer.endDispatchSpan(span, 'ok')
|
|
51
|
+
* } catch (error) {
|
|
52
|
+
* tracer.endDispatchSpan(span, 'error', error)
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
startDispatchSpan(eventName: string, listenerCount: number, priority?: 'high' | 'normal' | 'low', payload?: unknown): Span;
|
|
57
|
+
/**
|
|
58
|
+
* 為監聽器執行創建追蹤 Span
|
|
59
|
+
*
|
|
60
|
+
* @param _parentSpan - 父 Span
|
|
61
|
+
* @param eventName - 事件名稱
|
|
62
|
+
* @param listenerName - 監聽器名稱/標識
|
|
63
|
+
* @param listenerIndex - 監聽器索引
|
|
64
|
+
* @returns 子 Span 實例
|
|
65
|
+
*/
|
|
66
|
+
startListenerSpan(_parentSpan: Span, eventName: string, listenerName: string, listenerIndex: number): Span;
|
|
67
|
+
/**
|
|
68
|
+
* 為隊列操作創建追蹤 Span
|
|
69
|
+
*
|
|
70
|
+
* @param _parentSpan - 父 Span
|
|
71
|
+
* @param operationType - 操作類型(enqueue, dequeue, requeue)
|
|
72
|
+
* @param eventName - 事件名稱
|
|
73
|
+
* @param priority - 優先級
|
|
74
|
+
* @returns Span 實例
|
|
75
|
+
*/
|
|
76
|
+
startQueueOperationSpan(_parentSpan: Span, operationType: 'enqueue' | 'dequeue' | 'requeue', eventName: string, priority: 'high' | 'normal' | 'low'): Span;
|
|
77
|
+
/**
|
|
78
|
+
* 結束事件派發 Span
|
|
79
|
+
*
|
|
80
|
+
* @param span - Span 實例
|
|
81
|
+
* @param status - 完成狀態(ok, error)
|
|
82
|
+
* @param error - 如果 status 為 error,傳遞錯誤對象
|
|
83
|
+
*/
|
|
84
|
+
endDispatchSpan(span: Span, status?: 'ok' | 'error', error?: Error): void;
|
|
85
|
+
/**
|
|
86
|
+
* 結束監聽器執行 Span
|
|
87
|
+
*
|
|
88
|
+
* @param span - Span 實例
|
|
89
|
+
* @param status - 完成狀態(ok, error)
|
|
90
|
+
* @param duration - 執行時間(毫秒)
|
|
91
|
+
* @param error - 如果發生錯誤,傳遞錯誤對象
|
|
92
|
+
*/
|
|
93
|
+
endListenerSpan(span: Span, status?: 'ok' | 'error', duration?: number, error?: Error): void;
|
|
94
|
+
/**
|
|
95
|
+
* 結束隊列操作 Span
|
|
96
|
+
*
|
|
97
|
+
* @param span - Span 實例
|
|
98
|
+
* @param status - 完成狀態(ok, error)
|
|
99
|
+
* @param queueDepth - 當前隊列深度
|
|
100
|
+
* @param error - 如果發生錯誤,傳遞錯誤對象
|
|
101
|
+
*/
|
|
102
|
+
endQueueOperationSpan(span: Span, status?: 'ok' | 'error', queueDepth?: number, error?: Error): void;
|
|
103
|
+
/**
|
|
104
|
+
* 在 Span 中添加自定義屬性
|
|
105
|
+
*
|
|
106
|
+
* @param span - Span 實例
|
|
107
|
+
* @param attributes - 屬性對象
|
|
108
|
+
*/
|
|
109
|
+
addSpanAttributes(span: Span, attributes: Record<string, string | number | boolean>): void;
|
|
110
|
+
/**
|
|
111
|
+
* 在 Span 中添加自定義事件
|
|
112
|
+
*
|
|
113
|
+
* @param span - Span 實例
|
|
114
|
+
* @param eventName - 事件名稱
|
|
115
|
+
* @param attributes - 事件屬性(可選)
|
|
116
|
+
*/
|
|
117
|
+
addSpanEvent(span: Span, eventName: string, attributes?: Record<string, string | number | boolean>): void;
|
|
118
|
+
/**
|
|
119
|
+
* 記錄異常到 Span
|
|
120
|
+
*
|
|
121
|
+
* @param span - Span 實例
|
|
122
|
+
* @param error - 錯誤對象
|
|
123
|
+
*/
|
|
124
|
+
recordException(span: Span, error: Error | string): void;
|
|
125
|
+
/**
|
|
126
|
+
* 創建定時器 Span(用於測量操作時間)
|
|
127
|
+
*
|
|
128
|
+
* @param operationName - 操作名稱
|
|
129
|
+
* @returns 對象包含 span 和結束函數
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const { span, endTimer } = tracer.startTimer('slow-operation')
|
|
134
|
+
* try {
|
|
135
|
+
* await slowOperation()
|
|
136
|
+
* endTimer('ok')
|
|
137
|
+
* } catch (error) {
|
|
138
|
+
* endTimer('error', error)
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
startTimer(operationName: string): {
|
|
143
|
+
span: Span;
|
|
144
|
+
endTimer: (status?: 'ok' | 'error', error?: Error) => void;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* 獲取 tracer 實例(用於高級用法)
|
|
148
|
+
*
|
|
149
|
+
* @returns OpenTelemetry Tracer 實例
|
|
150
|
+
*/
|
|
151
|
+
getTracer(): import("@opentelemetry/api").Tracer;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 獲取或創建全局 EventTracing 實例
|
|
155
|
+
*
|
|
156
|
+
* @param config - 配置選項
|
|
157
|
+
* @returns EventTracing 實例
|
|
158
|
+
*
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
export declare function getEventTracing(config?: EventTracingConfig): EventTracing;
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - OpenTelemetry Event Metrics
|
|
3
|
+
*
|
|
4
|
+
* Provides event system metrics using OpenTelemetry API directly.
|
|
5
|
+
* This enables Prometheus export without requiring @gravito/monitor dependency.
|
|
6
|
+
*
|
|
7
|
+
* Metrics:
|
|
8
|
+
* - gravito_event_dispatch_duration_seconds (Histogram)
|
|
9
|
+
* - gravito_event_listener_duration_seconds (Histogram)
|
|
10
|
+
* - gravito_event_queue_depth (Observable Gauge)
|
|
11
|
+
*
|
|
12
|
+
* @packageDocumentation
|
|
13
|
+
*/
|
|
14
|
+
import type { Meter } from '@opentelemetry/api';
|
|
15
|
+
import type { CircuitBreakerMetricsRecorder } from '../CircuitBreaker';
|
|
16
|
+
/**
|
|
17
|
+
* Queue depth callback type.
|
|
18
|
+
* Returns the current queue depths for each priority level.
|
|
19
|
+
*/
|
|
20
|
+
export type QueueDepthCallback = () => {
|
|
21
|
+
critical: number;
|
|
22
|
+
high: number;
|
|
23
|
+
normal: number;
|
|
24
|
+
low: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Circuit breaker state callback type.
|
|
28
|
+
* Returns the current state of a circuit breaker.
|
|
29
|
+
*/
|
|
30
|
+
export type CircuitBreakerStateCallback = () => {
|
|
31
|
+
eventName: string;
|
|
32
|
+
listenerIndex: number;
|
|
33
|
+
state: 0 | 1 | 2;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* OpenTelemetry-based Event Metrics collector.
|
|
37
|
+
*
|
|
38
|
+
* Uses OpenTelemetry API directly for metrics collection, enabling
|
|
39
|
+
* Prometheus export through the OpenTelemetry SDK.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import { metrics } from '@opentelemetry/api'
|
|
44
|
+
* import { OTelEventMetrics } from '@gravito/core'
|
|
45
|
+
*
|
|
46
|
+
* const meter = metrics.getMeter('@gravito/core', '1.0.0')
|
|
47
|
+
* const eventMetrics = new OTelEventMetrics(meter)
|
|
48
|
+
*
|
|
49
|
+
* // Record dispatch duration
|
|
50
|
+
* eventMetrics.recordDispatchDuration('order:created', 'high', 0.123)
|
|
51
|
+
*
|
|
52
|
+
* // Record listener duration
|
|
53
|
+
* eventMetrics.recordListenerDuration('order:created', 0, 0.456)
|
|
54
|
+
*
|
|
55
|
+
* // Set queue depth callback
|
|
56
|
+
* eventMetrics.setQueueDepthCallback(() => ({
|
|
57
|
+
* high: 10,
|
|
58
|
+
* normal: 50,
|
|
59
|
+
* low: 100
|
|
60
|
+
* }))
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export declare class OTelEventMetrics implements CircuitBreakerMetricsRecorder {
|
|
66
|
+
private readonly meter;
|
|
67
|
+
private readonly prefix;
|
|
68
|
+
private readonly dispatchDurationHistogram;
|
|
69
|
+
private readonly listenerDurationHistogram;
|
|
70
|
+
private readonly queueDepthGauge;
|
|
71
|
+
private readonly cbStateGauge;
|
|
72
|
+
private readonly cbFailuresCounter;
|
|
73
|
+
private readonly cbSuccessesCounter;
|
|
74
|
+
private readonly cbTransitionsCounter;
|
|
75
|
+
private readonly cbOpenDurationHistogram;
|
|
76
|
+
private readonly backpressureRejectionsCounter;
|
|
77
|
+
private readonly backpressureStateGauge;
|
|
78
|
+
private readonly backpressureDegradationsCounter;
|
|
79
|
+
private backpressureStateValue;
|
|
80
|
+
private readonly dlqEntriesCounter;
|
|
81
|
+
private readonly dlqDepthGauge;
|
|
82
|
+
private readonly dlqRequeueCounter;
|
|
83
|
+
private readonly retryAttemptsCounter;
|
|
84
|
+
private dlqDepthCallback?;
|
|
85
|
+
private readonly priorityEscalationCounter;
|
|
86
|
+
private queueDepthCallback?;
|
|
87
|
+
private circuitBreakerStateCallbacks;
|
|
88
|
+
private recordedCircuitBreakerStates;
|
|
89
|
+
/**
|
|
90
|
+
* Bucket boundaries for dispatch duration histogram.
|
|
91
|
+
*/
|
|
92
|
+
private readonly dispatchDurationBuckets;
|
|
93
|
+
/**
|
|
94
|
+
* Bucket boundaries for listener duration histogram.
|
|
95
|
+
*/
|
|
96
|
+
private readonly listenerDurationBuckets;
|
|
97
|
+
/**
|
|
98
|
+
* Bucket boundaries for circuit breaker open duration histogram.
|
|
99
|
+
*/
|
|
100
|
+
private readonly cbOpenDurationBuckets;
|
|
101
|
+
/**
|
|
102
|
+
* Create a new OTelEventMetrics instance.
|
|
103
|
+
*
|
|
104
|
+
* @param meter - OpenTelemetry Meter instance
|
|
105
|
+
* @param prefix - Metric name prefix (default: 'gravito_event_')
|
|
106
|
+
*/
|
|
107
|
+
constructor(meter: Meter, prefix?: string);
|
|
108
|
+
/**
|
|
109
|
+
* Record event dispatch duration.
|
|
110
|
+
*
|
|
111
|
+
* @param eventName - Name of the event
|
|
112
|
+
* @param priority - Priority level (high, normal, low)
|
|
113
|
+
* @param durationSeconds - Duration in seconds
|
|
114
|
+
*/
|
|
115
|
+
recordDispatchDuration(eventName: string, priority: string, durationSeconds: number): void;
|
|
116
|
+
/**
|
|
117
|
+
* Record listener execution duration.
|
|
118
|
+
*
|
|
119
|
+
* @param eventName - Name of the event
|
|
120
|
+
* @param listenerIndex - Index of the listener in the callback list
|
|
121
|
+
* @param durationSeconds - Duration in seconds
|
|
122
|
+
*/
|
|
123
|
+
recordListenerDuration(eventName: string, listenerIndex: number, durationSeconds: number): void;
|
|
124
|
+
/**
|
|
125
|
+
* Set the callback for queue depth observable gauge.
|
|
126
|
+
*
|
|
127
|
+
* The callback will be invoked when metrics are collected,
|
|
128
|
+
* allowing real-time reporting of queue depths.
|
|
129
|
+
*
|
|
130
|
+
* @param callback - Function returning current queue depths
|
|
131
|
+
*/
|
|
132
|
+
setQueueDepthCallback(callback: QueueDepthCallback): void;
|
|
133
|
+
/**
|
|
134
|
+
* Register a circuit breaker state callback for monitoring.
|
|
135
|
+
*
|
|
136
|
+
* @param key - Unique identifier for the circuit breaker (e.g., "order:created-0")
|
|
137
|
+
* @param callback - Function returning current circuit breaker state
|
|
138
|
+
*/
|
|
139
|
+
registerCircuitBreakerStateCallback(key: string, callback: CircuitBreakerStateCallback): void;
|
|
140
|
+
/**
|
|
141
|
+
* Unregister a circuit breaker state callback.
|
|
142
|
+
*
|
|
143
|
+
* @param key - Unique identifier for the circuit breaker
|
|
144
|
+
*/
|
|
145
|
+
unregisterCircuitBreakerStateCallback(key: string): void;
|
|
146
|
+
/**
|
|
147
|
+
* Record circuit breaker state change.
|
|
148
|
+
*
|
|
149
|
+
* @param name - Name of the circuit breaker (usually event name)
|
|
150
|
+
* @param state - State as number (0=CLOSED, 1=HALF_OPEN, 2=OPEN)
|
|
151
|
+
*/
|
|
152
|
+
recordState(name: string, state: number): void;
|
|
153
|
+
/**
|
|
154
|
+
* Record circuit breaker state transition.
|
|
155
|
+
*
|
|
156
|
+
* @param name - Name of the circuit breaker
|
|
157
|
+
* @param fromState - Previous state
|
|
158
|
+
* @param toState - New state
|
|
159
|
+
*/
|
|
160
|
+
recordTransition(name: string, fromState: string, toState: string): void;
|
|
161
|
+
/**
|
|
162
|
+
* Record circuit breaker failure.
|
|
163
|
+
*
|
|
164
|
+
* @param name - Name of the circuit breaker
|
|
165
|
+
*/
|
|
166
|
+
recordFailure(name: string): void;
|
|
167
|
+
/**
|
|
168
|
+
* Record circuit breaker success.
|
|
169
|
+
*
|
|
170
|
+
* @param name - Name of the circuit breaker
|
|
171
|
+
*/
|
|
172
|
+
recordSuccess(name: string): void;
|
|
173
|
+
/**
|
|
174
|
+
* Record circuit breaker open duration.
|
|
175
|
+
*
|
|
176
|
+
* @param name - Name of the circuit breaker
|
|
177
|
+
* @param seconds - Duration in seconds
|
|
178
|
+
*/
|
|
179
|
+
recordOpenDuration(name: string, seconds: number): void;
|
|
180
|
+
/**
|
|
181
|
+
* Record circuit breaker failure.
|
|
182
|
+
*
|
|
183
|
+
* @param eventName - Name of the event
|
|
184
|
+
* @param listenerIndex - Index of the listener
|
|
185
|
+
*/
|
|
186
|
+
recordCircuitBreakerFailure(eventName: string, listenerIndex: number): void;
|
|
187
|
+
/**
|
|
188
|
+
* Record circuit breaker success.
|
|
189
|
+
*
|
|
190
|
+
* @param eventName - Name of the event
|
|
191
|
+
* @param listenerIndex - Index of the listener
|
|
192
|
+
*/
|
|
193
|
+
recordCircuitBreakerSuccess(eventName: string, listenerIndex: number): void;
|
|
194
|
+
/**
|
|
195
|
+
* Record circuit breaker state transition.
|
|
196
|
+
*
|
|
197
|
+
* @param eventName - Name of the event
|
|
198
|
+
* @param listenerIndex - Index of the listener
|
|
199
|
+
* @param fromState - Previous state (CLOSED, HALF_OPEN, OPEN)
|
|
200
|
+
* @param toState - New state (CLOSED, HALF_OPEN, OPEN)
|
|
201
|
+
*/
|
|
202
|
+
recordCircuitBreakerTransition(eventName: string, listenerIndex: number, fromState: string, toState: string): void;
|
|
203
|
+
/**
|
|
204
|
+
* Record circuit breaker open duration.
|
|
205
|
+
*
|
|
206
|
+
* @param eventName - Name of the event
|
|
207
|
+
* @param listenerIndex - Index of the listener
|
|
208
|
+
* @param durationSeconds - Duration in seconds
|
|
209
|
+
*/
|
|
210
|
+
recordCircuitBreakerOpenDuration(eventName: string, listenerIndex: number, durationSeconds: number): void;
|
|
211
|
+
/**
|
|
212
|
+
* Get the bucket boundaries for dispatch duration histogram.
|
|
213
|
+
*
|
|
214
|
+
* @returns Array of bucket boundaries in seconds
|
|
215
|
+
*/
|
|
216
|
+
getDispatchDurationBuckets(): number[];
|
|
217
|
+
/**
|
|
218
|
+
* Get the bucket boundaries for listener duration histogram.
|
|
219
|
+
*
|
|
220
|
+
* @returns Array of bucket boundaries in seconds
|
|
221
|
+
*/
|
|
222
|
+
getListenerDurationBuckets(): number[];
|
|
223
|
+
/**
|
|
224
|
+
* Get the OpenTelemetry Meter instance.
|
|
225
|
+
*
|
|
226
|
+
* @returns Meter instance
|
|
227
|
+
*/
|
|
228
|
+
getMeter(): Meter;
|
|
229
|
+
/**
|
|
230
|
+
* Get the metric name prefix.
|
|
231
|
+
*
|
|
232
|
+
* @returns Metric name prefix
|
|
233
|
+
*/
|
|
234
|
+
getPrefix(): string;
|
|
235
|
+
/**
|
|
236
|
+
* Get the bucket boundaries for circuit breaker open duration histogram.
|
|
237
|
+
*
|
|
238
|
+
* @returns Array of bucket boundaries in seconds
|
|
239
|
+
*/
|
|
240
|
+
getCircuitBreakerOpenDurationBuckets(): number[];
|
|
241
|
+
/**
|
|
242
|
+
* Get all registered circuit breaker state callback keys.
|
|
243
|
+
*
|
|
244
|
+
* @returns Array of registered keys
|
|
245
|
+
*/
|
|
246
|
+
getRegisteredCircuitBreakers(): string[];
|
|
247
|
+
/**
|
|
248
|
+
* Clear all circuit breaker state callbacks.
|
|
249
|
+
*/
|
|
250
|
+
clearCircuitBreakerCallbacks(): void;
|
|
251
|
+
/**
|
|
252
|
+
* Record a backpressure rejection event.
|
|
253
|
+
*
|
|
254
|
+
* @param eventName - Event name
|
|
255
|
+
* @param priority - Event priority
|
|
256
|
+
* @param reason - Rejection reason
|
|
257
|
+
*/
|
|
258
|
+
recordBackpressureRejection(eventName: string, priority: string, reason: string): void;
|
|
259
|
+
/**
|
|
260
|
+
* Record a backpressure state change event.
|
|
261
|
+
*
|
|
262
|
+
* @param state - New backpressure state (NORMAL, WARNING, CRITICAL, OVERFLOW)
|
|
263
|
+
*/
|
|
264
|
+
recordBackpressureState(state: string): void;
|
|
265
|
+
/**
|
|
266
|
+
* Record a backpressure degradation event.
|
|
267
|
+
*
|
|
268
|
+
* @param eventName - Event name
|
|
269
|
+
* @param fromPriority - Original priority
|
|
270
|
+
* @param toPriority - Degraded priority
|
|
271
|
+
*/
|
|
272
|
+
recordBackpressureDegradation(eventName: string, fromPriority: string, toPriority: string): void;
|
|
273
|
+
/**
|
|
274
|
+
* Record an event added to Dead Letter Queue.
|
|
275
|
+
*
|
|
276
|
+
* @param eventName - Event name
|
|
277
|
+
* @param source - Source of DLQ entry (retry_exhausted, circuit_breaker, backpressure_overflow)
|
|
278
|
+
*/
|
|
279
|
+
recordDLQEntry(eventName: string, source: string): void;
|
|
280
|
+
/**
|
|
281
|
+
* Set the callback for DLQ depth observable gauge.
|
|
282
|
+
*
|
|
283
|
+
* @param callback - Function returning current DLQ depth
|
|
284
|
+
*/
|
|
285
|
+
setDLQDepthCallback(callback: () => number): void;
|
|
286
|
+
/**
|
|
287
|
+
* Record a DLQ requeue attempt.
|
|
288
|
+
*
|
|
289
|
+
* @param eventName - Event name
|
|
290
|
+
* @param result - Result of requeue (success or failure)
|
|
291
|
+
*/
|
|
292
|
+
recordDLQRequeue(eventName: string, result: 'success' | 'failure'): void;
|
|
293
|
+
/**
|
|
294
|
+
* Record an event retry attempt.
|
|
295
|
+
*
|
|
296
|
+
* @param eventName - Event name
|
|
297
|
+
* @param attemptNumber - Attempt number
|
|
298
|
+
*/
|
|
299
|
+
recordRetryAttempt(eventName: string, attemptNumber: number): void;
|
|
300
|
+
/**
|
|
301
|
+
* Record a priority escalation event.
|
|
302
|
+
*
|
|
303
|
+
* @param eventName - Event name
|
|
304
|
+
* @param fromPriority - Original priority
|
|
305
|
+
* @param toPriority - Escalated priority
|
|
306
|
+
*/
|
|
307
|
+
recordPriorityEscalation(eventName: string, fromPriority: string, toPriority: string): void;
|
|
308
|
+
/**
|
|
309
|
+
* Record event deduplication (FS-102).
|
|
310
|
+
*
|
|
311
|
+
* @param eventName - Event name
|
|
312
|
+
* @param deduplicatedCount - Number of events after deduplication
|
|
313
|
+
* @param totalCount - Total number of events before deduplication
|
|
314
|
+
*/
|
|
315
|
+
recordDeduplication(eventName: string, deduplicatedCount: number, totalCount: number): void;
|
|
316
|
+
/**
|
|
317
|
+
* Record batch submission (FS-102).
|
|
318
|
+
*
|
|
319
|
+
* @param eventName - Event name
|
|
320
|
+
* @param batchSize - Size of submitted batch
|
|
321
|
+
* @param windowMs - Aggregation window size
|
|
322
|
+
*/
|
|
323
|
+
recordBatch(eventName: string, batchSize: number, windowMs: number): void;
|
|
324
|
+
/**
|
|
325
|
+
* Record window adjustment (FS-102).
|
|
326
|
+
*
|
|
327
|
+
* @param oldWindowMs - Previous window size
|
|
328
|
+
* @param newWindowMs - New window size
|
|
329
|
+
* @param reason - Adjustment reason
|
|
330
|
+
*/
|
|
331
|
+
recordWindowAdjustment(oldWindowMs: number, newWindowMs: number, reason: string): void;
|
|
332
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Observable Hook Manager
|
|
3
|
+
*
|
|
4
|
+
* Wraps HookManager with observability support (metrics and tracing).
|
|
5
|
+
*/
|
|
6
|
+
import { HookManager, type HookManagerConfig } from '../../HookManager';
|
|
7
|
+
import type { EventOptions } from '../EventOptions';
|
|
8
|
+
import { EventMetrics } from './EventMetrics';
|
|
9
|
+
import { EventTracer } from './EventTracer';
|
|
10
|
+
import { EventTracing } from './EventTracing';
|
|
11
|
+
/**
|
|
12
|
+
* Configuration for observability features.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface ObservabilityConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Enable observability features.
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* MetricsRegistry instance from @gravito/monitor.
|
|
23
|
+
* Required if metrics collection is enabled.
|
|
24
|
+
*/
|
|
25
|
+
metrics?: any;
|
|
26
|
+
/**
|
|
27
|
+
* Enable OpenTelemetry distributed tracing.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
tracing?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Prefix for metric names.
|
|
33
|
+
* @default 'gravito_event_'
|
|
34
|
+
*/
|
|
35
|
+
metricsPrefix?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Observable Hook Manager - extends HookManager with observability.
|
|
39
|
+
*
|
|
40
|
+
* Provides metrics and distributed tracing for event dispatch and listener execution.
|
|
41
|
+
* Maintains 100% backward compatibility with HookManager.
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare class ObservableHookManager extends HookManager {
|
|
46
|
+
private eventMetrics?;
|
|
47
|
+
private eventTracer?;
|
|
48
|
+
private eventTracing?;
|
|
49
|
+
private obsConfig;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new ObservableHookManager instance.
|
|
52
|
+
*
|
|
53
|
+
* @param config - HookManager configuration
|
|
54
|
+
* @param obsConfig - Observability configuration
|
|
55
|
+
*/
|
|
56
|
+
constructor(config?: HookManagerConfig, obsConfig?: ObservabilityConfig);
|
|
57
|
+
/**
|
|
58
|
+
* Run all registered actions asynchronously via priority queue with observability.
|
|
59
|
+
*
|
|
60
|
+
* This override adds metrics and tracing to the base implementation.
|
|
61
|
+
*
|
|
62
|
+
* @template TArgs - The type of arguments passed to the action.
|
|
63
|
+
* @param hook - The name of the hook.
|
|
64
|
+
* @param args - The arguments to pass to the callbacks.
|
|
65
|
+
* @param options - Event options for async dispatch.
|
|
66
|
+
*/
|
|
67
|
+
doActionAsync<TArgs = unknown>(hook: string, args: TArgs, options?: EventOptions): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Run all registered actions synchronously with observability.
|
|
70
|
+
*
|
|
71
|
+
* This override adds metrics and tracing to the base implementation.
|
|
72
|
+
*
|
|
73
|
+
* @template TArgs - The type of arguments passed to the action.
|
|
74
|
+
* @param hook - The name of the hook.
|
|
75
|
+
* @param args - The arguments to pass to the callbacks.
|
|
76
|
+
*/
|
|
77
|
+
doActionSync<TArgs = unknown>(hook: string, args: TArgs): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Get the EventMetrics instance.
|
|
80
|
+
*
|
|
81
|
+
* @returns EventMetrics instance if observability is enabled, undefined otherwise
|
|
82
|
+
*/
|
|
83
|
+
getMetrics(): EventMetrics | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Get the EventTracer instance.
|
|
86
|
+
*
|
|
87
|
+
* @returns EventTracer instance if tracing is enabled, undefined otherwise
|
|
88
|
+
*/
|
|
89
|
+
getTracer(): EventTracer | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Update observability configuration at runtime.
|
|
92
|
+
*
|
|
93
|
+
* @param config - New observability configuration
|
|
94
|
+
*/
|
|
95
|
+
setObservabilityConfig(config: Partial<ObservabilityConfig>): void;
|
|
96
|
+
/**
|
|
97
|
+
* Get the EventTracing instance.
|
|
98
|
+
*
|
|
99
|
+
* @returns EventTracing instance if tracing is enabled, undefined otherwise
|
|
100
|
+
*/
|
|
101
|
+
getEventTracing(): EventTracing | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Get current observability configuration.
|
|
104
|
+
*
|
|
105
|
+
* @returns Current observability configuration
|
|
106
|
+
*/
|
|
107
|
+
getObservabilityConfig(): ObservabilityConfig;
|
|
108
|
+
}
|