@zucker-framework/monitoring 1.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/LICENSE +201 -0
- package/NOTICE +22 -0
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/index.d.mts +502 -0
- package/dist/index.d.ts +502 -0
- package/dist/index.js +1748 -0
- package/dist/index.mjs +1690 -0
- package/package.json +38 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
3
|
+
|
|
4
|
+
interface TrackedError {
|
|
5
|
+
id: string;
|
|
6
|
+
message: string;
|
|
7
|
+
stack?: string;
|
|
8
|
+
category: string;
|
|
9
|
+
count: number;
|
|
10
|
+
firstOccurrence: Date;
|
|
11
|
+
lastOccurrence: Date;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
declare class ErrorTracker {
|
|
15
|
+
private readonly logger;
|
|
16
|
+
private errors;
|
|
17
|
+
private counter;
|
|
18
|
+
track(error: Error, category?: string, metadata?: Record<string, unknown>): void;
|
|
19
|
+
getErrors(): TrackedError[];
|
|
20
|
+
getErrorsByCategory(category: string): TrackedError[];
|
|
21
|
+
getTopErrors(limit?: number): TrackedError[];
|
|
22
|
+
clear(): void;
|
|
23
|
+
clearCategory(category: string): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Vendor-neutral context attached to an error report.
|
|
28
|
+
*
|
|
29
|
+
* Applications should pass identifiers and low-cardinality diagnostics only.
|
|
30
|
+
* Product text, prompts, credentials, and other sensitive payloads must be
|
|
31
|
+
* removed by the application or provider adapter before transmission.
|
|
32
|
+
*/
|
|
33
|
+
interface ErrorContext {
|
|
34
|
+
requestId?: string;
|
|
35
|
+
userId?: string;
|
|
36
|
+
resourceId?: string;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
interface ErrorReporterUser {
|
|
40
|
+
id: string;
|
|
41
|
+
email?: string;
|
|
42
|
+
}
|
|
43
|
+
/** Stable Nest injection token, including when duplicate package copies exist. */
|
|
44
|
+
declare const ERROR_REPORTER: unique symbol;
|
|
45
|
+
/**
|
|
46
|
+
* Provider-neutral error reporting port.
|
|
47
|
+
*
|
|
48
|
+
* Sentry, Datadog, Rollbar, or an internal collector belongs behind this port;
|
|
49
|
+
* the framework intentionally does not depend on any vendor SDK.
|
|
50
|
+
*/
|
|
51
|
+
interface ErrorReporter {
|
|
52
|
+
captureException(error: unknown, context?: ErrorContext): void;
|
|
53
|
+
captureMessage(message: string, context?: ErrorContext): void;
|
|
54
|
+
setUser(user: ErrorReporterUser | null): void;
|
|
55
|
+
}
|
|
56
|
+
/** Safe default for disabled or unconfigured production integrations. */
|
|
57
|
+
declare class NoopErrorReporter implements ErrorReporter {
|
|
58
|
+
captureException(): void;
|
|
59
|
+
captureMessage(): void;
|
|
60
|
+
setUser(): void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare enum HealthStatus {
|
|
64
|
+
UP = "UP",
|
|
65
|
+
DOWN = "DOWN",
|
|
66
|
+
DEGRADED = "DEGRADED"
|
|
67
|
+
}
|
|
68
|
+
interface HealthCheck {
|
|
69
|
+
name: string;
|
|
70
|
+
status: HealthStatus;
|
|
71
|
+
details: Record<string, unknown>;
|
|
72
|
+
timestamp: Date;
|
|
73
|
+
}
|
|
74
|
+
interface HealthIndicator {
|
|
75
|
+
name: string;
|
|
76
|
+
check(): Promise<HealthCheck>;
|
|
77
|
+
}
|
|
78
|
+
declare class HealthCheckService {
|
|
79
|
+
private readonly logger;
|
|
80
|
+
private indicators;
|
|
81
|
+
registerIndicator(indicator: HealthIndicator): void;
|
|
82
|
+
unregisterIndicator(name: string): void;
|
|
83
|
+
checkAll(): Promise<{
|
|
84
|
+
status: HealthStatus;
|
|
85
|
+
checks: HealthCheck[];
|
|
86
|
+
timestamp: Date;
|
|
87
|
+
}>;
|
|
88
|
+
check(name: string): Promise<HealthCheck | undefined>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface MeasurementDefinition {
|
|
92
|
+
id: string;
|
|
93
|
+
name: string;
|
|
94
|
+
dimensions: string[];
|
|
95
|
+
}
|
|
96
|
+
interface MeasurementProvider {
|
|
97
|
+
getDefinitions(): MeasurementDefinition[];
|
|
98
|
+
getMeasurements(definitionId: string): Promise<Record<string, unknown>[]>;
|
|
99
|
+
}
|
|
100
|
+
interface MetricPoint {
|
|
101
|
+
name: string;
|
|
102
|
+
value: number;
|
|
103
|
+
tags: Record<string, string>;
|
|
104
|
+
timestamp: Date;
|
|
105
|
+
}
|
|
106
|
+
interface MetricsCollector {
|
|
107
|
+
increment(name: string, tags?: Record<string, string>): void;
|
|
108
|
+
gauge(name: string, value: number, tags?: Record<string, string>): void;
|
|
109
|
+
histogram(name: string, value: number, tags?: Record<string, string>): void;
|
|
110
|
+
getMetrics(): MetricPoint[];
|
|
111
|
+
}
|
|
112
|
+
declare class InMemoryMetricsCollector implements MetricsCollector {
|
|
113
|
+
private metrics;
|
|
114
|
+
private static sortedTagsKey;
|
|
115
|
+
increment(name: string, tags?: Record<string, string>): void;
|
|
116
|
+
gauge(name: string, value: number, tags?: Record<string, string>): void;
|
|
117
|
+
histogram(name: string, value: number, tags?: Record<string, string>): void;
|
|
118
|
+
getMetrics(): MetricPoint[];
|
|
119
|
+
clear(): void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface MetricLabels {
|
|
123
|
+
[key: string]: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 计数器 — 只增不减
|
|
127
|
+
*/
|
|
128
|
+
declare class Counter {
|
|
129
|
+
readonly name: string;
|
|
130
|
+
readonly description: string;
|
|
131
|
+
private values;
|
|
132
|
+
constructor(name: string, description: string);
|
|
133
|
+
increment(labels?: MetricLabels, amount?: number): void;
|
|
134
|
+
get(labels?: MetricLabels): number;
|
|
135
|
+
getAll(): Array<{
|
|
136
|
+
labels: MetricLabels;
|
|
137
|
+
value: number;
|
|
138
|
+
}>;
|
|
139
|
+
private labelsKey;
|
|
140
|
+
private parseKey;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 仪表 — 可增可减
|
|
144
|
+
*/
|
|
145
|
+
declare class Gauge {
|
|
146
|
+
readonly name: string;
|
|
147
|
+
readonly description: string;
|
|
148
|
+
private values;
|
|
149
|
+
constructor(name: string, description: string);
|
|
150
|
+
set(value: number, labels?: MetricLabels): void;
|
|
151
|
+
get(labels?: MetricLabels): number;
|
|
152
|
+
increment(labels?: MetricLabels, amount?: number): void;
|
|
153
|
+
decrement(labels?: MetricLabels, amount?: number): void;
|
|
154
|
+
getAll(): Array<{
|
|
155
|
+
labels: MetricLabels;
|
|
156
|
+
value: number;
|
|
157
|
+
}>;
|
|
158
|
+
private labelsKey;
|
|
159
|
+
private parseKey;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 直方图 — 观测值分布
|
|
163
|
+
*/
|
|
164
|
+
declare class Histogram {
|
|
165
|
+
readonly name: string;
|
|
166
|
+
readonly description: string;
|
|
167
|
+
readonly buckets: number[];
|
|
168
|
+
private readonly maxSamples;
|
|
169
|
+
private observations;
|
|
170
|
+
constructor(name: string, description: string, buckets?: number[], maxSamples?: number);
|
|
171
|
+
observe(value: number, labels?: MetricLabels): void;
|
|
172
|
+
getPercentile(percentile: number, labels?: MetricLabels): number;
|
|
173
|
+
getCount(labels?: MetricLabels): number;
|
|
174
|
+
getSum(labels?: MetricLabels): number;
|
|
175
|
+
getAll(): Array<{
|
|
176
|
+
labels: MetricLabels;
|
|
177
|
+
count: number;
|
|
178
|
+
sum: number;
|
|
179
|
+
bucketCounts: Record<string, number>;
|
|
180
|
+
}>;
|
|
181
|
+
private labelsKey;
|
|
182
|
+
private parseKey;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 计时器 — 专门记录耗时
|
|
186
|
+
*/
|
|
187
|
+
declare class Timer {
|
|
188
|
+
readonly name: string;
|
|
189
|
+
readonly description: string;
|
|
190
|
+
private histogram;
|
|
191
|
+
constructor(name: string, description: string);
|
|
192
|
+
/**
|
|
193
|
+
* 记录一次耗时(秒)
|
|
194
|
+
*/
|
|
195
|
+
record(durationSeconds: number, labels?: MetricLabels): void;
|
|
196
|
+
/**
|
|
197
|
+
* 开始计时,返回结束函数
|
|
198
|
+
*/
|
|
199
|
+
startTimer(labels?: MetricLabels): () => number;
|
|
200
|
+
getHistogram(): Histogram;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Summary — 计算分位数(类似 Prometheus summary 类型)
|
|
204
|
+
*/
|
|
205
|
+
declare class Summary {
|
|
206
|
+
readonly name: string;
|
|
207
|
+
readonly description: string;
|
|
208
|
+
readonly quantiles: number[];
|
|
209
|
+
/** 观测值最大保留时间(毫秒),默认 10 分钟 */
|
|
210
|
+
private readonly maxAgeMs;
|
|
211
|
+
private readonly maxSamples;
|
|
212
|
+
private observations;
|
|
213
|
+
constructor(name: string, description: string, quantiles?: number[],
|
|
214
|
+
/** 观测值最大保留时间(毫秒),默认 10 分钟 */
|
|
215
|
+
maxAgeMs?: number, maxSamples?: number);
|
|
216
|
+
observe(value: number, labels?: MetricLabels): void;
|
|
217
|
+
getQuantile(quantile: number, labels?: MetricLabels): number;
|
|
218
|
+
getCount(labels?: MetricLabels): number;
|
|
219
|
+
getSum(labels?: MetricLabels): number;
|
|
220
|
+
getAll(): Array<{
|
|
221
|
+
labels: MetricLabels;
|
|
222
|
+
count: number;
|
|
223
|
+
sum: number;
|
|
224
|
+
quantileValues: Record<string, number>;
|
|
225
|
+
}>;
|
|
226
|
+
private prune;
|
|
227
|
+
private labelsKey;
|
|
228
|
+
private parseKey;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 指标注册中心
|
|
232
|
+
*/
|
|
233
|
+
declare class MetricsRegistry {
|
|
234
|
+
private counters;
|
|
235
|
+
private gauges;
|
|
236
|
+
private histograms;
|
|
237
|
+
private timers;
|
|
238
|
+
private summaries;
|
|
239
|
+
counter(name: string, description?: string): Counter;
|
|
240
|
+
gauge(name: string, description?: string): Gauge;
|
|
241
|
+
histogram(name: string, description?: string, buckets?: number[]): Histogram;
|
|
242
|
+
timer(name: string, description?: string): Timer;
|
|
243
|
+
summary(name: string, description?: string, quantiles?: number[]): Summary;
|
|
244
|
+
getCounters(): Counter[];
|
|
245
|
+
getGauges(): Gauge[];
|
|
246
|
+
getHistograms(): Histogram[];
|
|
247
|
+
getTimers(): Timer[];
|
|
248
|
+
getSummaries(): Summary[];
|
|
249
|
+
/**
|
|
250
|
+
* 重置所有指标
|
|
251
|
+
*/
|
|
252
|
+
clear(): void;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
interface MetricsOptions {
|
|
256
|
+
/** 可选的注入 registry 实例 */
|
|
257
|
+
registry?: MetricsRegistry;
|
|
258
|
+
/** 静态标签,附加到每个指标 */
|
|
259
|
+
labels?: MetricLabels;
|
|
260
|
+
/** 是否记录错误次数(默认 true) */
|
|
261
|
+
trackErrors?: boolean;
|
|
262
|
+
/** 是否记录活跃请求数(默认 false) */
|
|
263
|
+
trackActiveRequests?: boolean;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* @Metrics 方法装饰器 — 自动记录调用次数、耗时、错误数
|
|
267
|
+
*
|
|
268
|
+
* @param name 指标名称前缀
|
|
269
|
+
* @param options 可选配置
|
|
270
|
+
*/
|
|
271
|
+
declare function Metrics(name: string, options?: MetricsOptions): MethodDecorator;
|
|
272
|
+
|
|
273
|
+
interface SystemMetricsOptions {
|
|
274
|
+
/** 采集间隔(毫秒),默认 15000 */
|
|
275
|
+
intervalMs?: number;
|
|
276
|
+
/** 是否启用,默认 true */
|
|
277
|
+
enabled?: boolean;
|
|
278
|
+
/** 采集前缀,默认无 */
|
|
279
|
+
prefix?: string;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 系统指标采集器:CPU、内存、进程 uptime、事件循环延迟、GC、活跃句柄
|
|
283
|
+
*/
|
|
284
|
+
declare class SystemMetricsCollector implements OnModuleInit, OnModuleDestroy {
|
|
285
|
+
private readonly registry;
|
|
286
|
+
private intervalHandle;
|
|
287
|
+
private options;
|
|
288
|
+
private lastCpuUsage;
|
|
289
|
+
private lastCpuUsageTime;
|
|
290
|
+
constructor(registry: MetricsRegistry);
|
|
291
|
+
configure(options: SystemMetricsOptions): void;
|
|
292
|
+
onModuleInit(): void;
|
|
293
|
+
onModuleDestroy(): void;
|
|
294
|
+
private metricName;
|
|
295
|
+
collect(): void;
|
|
296
|
+
private collectCpuMetrics;
|
|
297
|
+
private collectMemoryMetrics;
|
|
298
|
+
private collectProcessMetrics;
|
|
299
|
+
private collectUptimeMetrics;
|
|
300
|
+
private measureEventLoopDelay;
|
|
301
|
+
private collectHandleMetrics;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Render a registry without requiring an HTTP server or Nest controller. */
|
|
305
|
+
declare function renderPrometheusMetrics(registry: MetricsRegistry): string;
|
|
306
|
+
/**
|
|
307
|
+
* Prometheus 格式指标导出端点
|
|
308
|
+
*/
|
|
309
|
+
declare class PrometheusExporter {
|
|
310
|
+
private readonly registry;
|
|
311
|
+
constructor(registry: MetricsRegistry);
|
|
312
|
+
getMetrics(): string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
declare const METRICS_COLLECTOR: unique symbol;
|
|
316
|
+
declare class ZuckerMonitoringModule {
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
declare const HEALTH_OPTIONS: unique symbol;
|
|
320
|
+
interface HealthOptions {
|
|
321
|
+
/** 自定义 /health 路径前缀,默认 'health' */
|
|
322
|
+
prefix?: string;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* 健康检查控制器
|
|
326
|
+
*
|
|
327
|
+
* 提供以下端点:
|
|
328
|
+
* - GET /health — 完整健康检查
|
|
329
|
+
* - GET /health/live — K8s liveness 探针(仅检查进程存活)
|
|
330
|
+
* - GET /health/ready — K8s readiness 探针(检查所有依赖)
|
|
331
|
+
*/
|
|
332
|
+
declare class HealthController {
|
|
333
|
+
private readonly healthCheckService;
|
|
334
|
+
private readonly startedAtMs;
|
|
335
|
+
constructor(healthCheckService: HealthCheckService);
|
|
336
|
+
check(res: any): Promise<void>;
|
|
337
|
+
/**
|
|
338
|
+
* K8s liveness 探针 — 仅检查进程是否存活
|
|
339
|
+
*/
|
|
340
|
+
live(res: any): void;
|
|
341
|
+
/**
|
|
342
|
+
* K8s readiness 探针 — 检查所有依赖是否就绪
|
|
343
|
+
*/
|
|
344
|
+
ready(res: any): Promise<void>;
|
|
345
|
+
private formatChecks;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
interface ProcessUptimeSnapshot {
|
|
349
|
+
uptimeMs: number;
|
|
350
|
+
uptimeSeconds: number;
|
|
351
|
+
}
|
|
352
|
+
/** Return a non-negative process duration with explicit units. */
|
|
353
|
+
declare function getProcessUptime(startedAtMs: number, nowMs?: number): ProcessUptimeSnapshot;
|
|
354
|
+
|
|
355
|
+
type AlertSeverity = 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
|
|
356
|
+
type AlertStatus = 'ACTIVE' | 'ACKNOWLEDGED' | 'RESOLVED';
|
|
357
|
+
interface AlertRule {
|
|
358
|
+
id: string;
|
|
359
|
+
name: string;
|
|
360
|
+
metric: string;
|
|
361
|
+
condition: '>' | '>=' | '<' | '<=' | '==' | '!=';
|
|
362
|
+
threshold: number;
|
|
363
|
+
/** 持续违规秒数,超过后触发告警 */
|
|
364
|
+
duration: number;
|
|
365
|
+
severity: AlertSeverity;
|
|
366
|
+
enabled: boolean;
|
|
367
|
+
/** 通知渠道标识列表 */
|
|
368
|
+
notifyChannels?: string[];
|
|
369
|
+
}
|
|
370
|
+
interface AlertRecord {
|
|
371
|
+
id: string;
|
|
372
|
+
ruleId: string;
|
|
373
|
+
ruleName: string;
|
|
374
|
+
metric: string;
|
|
375
|
+
value: number;
|
|
376
|
+
threshold: number;
|
|
377
|
+
severity: AlertSeverity;
|
|
378
|
+
status: AlertStatus;
|
|
379
|
+
message: string;
|
|
380
|
+
triggeredAt: number;
|
|
381
|
+
acknowledgedAt?: number;
|
|
382
|
+
resolvedAt?: number;
|
|
383
|
+
acknowledgedBy?: string;
|
|
384
|
+
resolvedBy?: string;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
declare class AlertManagerService implements OnModuleInit {
|
|
388
|
+
private readonly eventEmitter?;
|
|
389
|
+
private readonly db?;
|
|
390
|
+
private readonly logger;
|
|
391
|
+
/** 已注册的告警规则 */
|
|
392
|
+
private readonly rules;
|
|
393
|
+
/** 所有告警记录(含历史) */
|
|
394
|
+
private readonly alerts;
|
|
395
|
+
/** 每条规则的持续违规状态 */
|
|
396
|
+
private readonly violations;
|
|
397
|
+
/** 活跃告警的 ruleId 索引(非 RESOLVED 状态的告警) */
|
|
398
|
+
private readonly activeRuleIds;
|
|
399
|
+
constructor(eventEmitter?: EventEmitter2, db?: any | undefined);
|
|
400
|
+
onModuleInit(): Promise<void>;
|
|
401
|
+
registerRule(rule: AlertRule): void;
|
|
402
|
+
removeRule(ruleId: string): boolean;
|
|
403
|
+
getRules(): AlertRule[];
|
|
404
|
+
getRule(ruleId: string): AlertRule | undefined;
|
|
405
|
+
/**
|
|
406
|
+
* 对一组指标快照评估所有已启用规则。
|
|
407
|
+
* 应由调度器(如 Cron / Interval)定期调用。
|
|
408
|
+
*/
|
|
409
|
+
evaluate(metrics: Record<string, number>): AlertRecord[];
|
|
410
|
+
getAlerts(): AlertRecord[];
|
|
411
|
+
getActiveAlerts(): AlertRecord[];
|
|
412
|
+
getAlert(alertId: string): AlertRecord | undefined;
|
|
413
|
+
acknowledgeAlert(alertId: string, userId: string): AlertRecord | undefined;
|
|
414
|
+
resolveAlert(alertId: string, userId: string): AlertRecord | undefined;
|
|
415
|
+
private checkCondition;
|
|
416
|
+
private hasActiveAlertForRule;
|
|
417
|
+
private createAlert;
|
|
418
|
+
private autoResolveForRule;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* AlertController — 告警管理接口
|
|
423
|
+
*
|
|
424
|
+
* 注意:本 Controller 不自带认证 Guard(monitoring 包不依赖 @zucker-framework/auth)。
|
|
425
|
+
* 应用层应通过以下方式之一保护这些端点:
|
|
426
|
+
* 1. 全局 APP_GUARD(推荐 — 在 main.ts 或 AppModule 中注册 JwtAuthGuard)
|
|
427
|
+
* 2. 在注册 ZuckerMonitoringModule 时,通过路由中间件添加认证
|
|
428
|
+
*/
|
|
429
|
+
declare class AlertController {
|
|
430
|
+
private readonly alertManager;
|
|
431
|
+
constructor(alertManager: AlertManagerService);
|
|
432
|
+
getAlerts(): AlertRecord[];
|
|
433
|
+
getRules(): AlertRule[];
|
|
434
|
+
createRule(body: AlertRule): AlertRule;
|
|
435
|
+
deleteRule(id: string): void;
|
|
436
|
+
acknowledgeAlert(id: string, userId: string): AlertRecord;
|
|
437
|
+
resolveAlert(id: string, userId: string): AlertRecord;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 请求级资源使用追踪
|
|
442
|
+
*
|
|
443
|
+
* 对标 Claude Code 的 cost-tracker.ts:
|
|
444
|
+
* 每次请求自动追踪数据库查询次数、缓存命中率、存储操作等。
|
|
445
|
+
*
|
|
446
|
+
* 用法:
|
|
447
|
+
* ```ts
|
|
448
|
+
* // 在中间件中初始化
|
|
449
|
+
* UsageTracker.runWithContext(async () => {
|
|
450
|
+
* // 在 service 中记录
|
|
451
|
+
* UsageTracker.track('db_query');
|
|
452
|
+
* UsageTracker.track('cache_hit');
|
|
453
|
+
*
|
|
454
|
+
* // 请求结束时获取汇总
|
|
455
|
+
* const summary = UsageTracker.getSummary();
|
|
456
|
+
* // → { db_query: 5, cache_hit: 2, cache_miss: 1, elapsed: 123 }
|
|
457
|
+
* });
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
interface UsageMetrics {
|
|
461
|
+
[key: string]: number;
|
|
462
|
+
}
|
|
463
|
+
interface UsageSummary {
|
|
464
|
+
/** 各项指标的计数 */
|
|
465
|
+
metrics: UsageMetrics;
|
|
466
|
+
/** 请求开始时间 */
|
|
467
|
+
startTime: number;
|
|
468
|
+
/** 已用时间(毫秒) */
|
|
469
|
+
elapsed: number;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* 轻量级请求级资源使用追踪器
|
|
473
|
+
*/
|
|
474
|
+
declare class UsageTracker {
|
|
475
|
+
private static readonly logger;
|
|
476
|
+
/**
|
|
477
|
+
* 在新的追踪上下文中执行函数
|
|
478
|
+
*/
|
|
479
|
+
static runWithContext<T>(fn: () => T): T;
|
|
480
|
+
/**
|
|
481
|
+
* 记录一次资源使用
|
|
482
|
+
* @param type 资源类型(如 'db_query', 'cache_hit', 'cache_miss', 'storage_read', 'api_call')
|
|
483
|
+
* @param amount 使用量(默认 1)
|
|
484
|
+
*/
|
|
485
|
+
static track(type: string, amount?: number): void;
|
|
486
|
+
/**
|
|
487
|
+
* 获取指定类型的当前计数
|
|
488
|
+
*/
|
|
489
|
+
static getCount(type: string): number;
|
|
490
|
+
/**
|
|
491
|
+
* 获取当前请求的使用汇总
|
|
492
|
+
*/
|
|
493
|
+
static getSummary(): UsageSummary | undefined;
|
|
494
|
+
/**
|
|
495
|
+
* 检查是否在追踪上下文中
|
|
496
|
+
*/
|
|
497
|
+
static isTracking(): boolean;
|
|
498
|
+
track(type: string, amount?: number): void;
|
|
499
|
+
getSummary(): UsageSummary | undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export { AlertController, AlertManagerService, type AlertRecord, type AlertRule, type AlertSeverity, type AlertStatus, Counter, ERROR_REPORTER, type ErrorContext, type ErrorReporter, type ErrorReporterUser, ErrorTracker, Gauge, HEALTH_OPTIONS, type HealthCheck, HealthCheckService, HealthController, type HealthIndicator, type HealthOptions, HealthStatus, Histogram, InMemoryMetricsCollector, METRICS_COLLECTOR, type MeasurementDefinition, type MeasurementProvider, type MetricLabels, type MetricPoint, Metrics, type MetricsCollector, type MetricsOptions, MetricsRegistry, NoopErrorReporter, type ProcessUptimeSnapshot, PrometheusExporter, Summary, SystemMetricsCollector, type SystemMetricsOptions, Timer, type TrackedError, type UsageMetrics, type UsageSummary, UsageTracker, ZuckerMonitoringModule, getProcessUptime, renderPrometheusMetrics };
|