-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.zod.ts
More file actions
707 lines (598 loc) · 16.5 KB
/
metrics.zod.ts
File metadata and controls
707 lines (598 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Metrics Protocol - Performance and Operational Metrics
*
* Comprehensive metrics collection and monitoring:
* - Counter, Gauge, Histogram, Summary metric types
* - Time-series data collection
* - SLI/SLO definitions
* - Metric aggregation and export
* - Integration with monitoring systems (Prometheus, etc.)
*/
/**
* Metric Type Enum
* Standard Prometheus metric types
*/
import { lazySchema } from '../shared/lazy-schema';
export const MetricType = z.enum([
'counter', // Monotonically increasing value
'gauge', // Value that can go up and down
'histogram', // Observations bucketed by configurable ranges
'summary', // Observations with quantiles
]).describe('Metric type');
export type MetricType = z.infer<typeof MetricType>;
/**
* Metric Unit Enum
* Standard units for metrics
*/
export const MetricUnit = z.enum([
// Time units
'nanoseconds',
'microseconds',
'milliseconds',
'seconds',
'minutes',
'hours',
'days',
// Size units
'bytes',
'kilobytes',
'megabytes',
'gigabytes',
'terabytes',
// Rate units
'requests_per_second',
'events_per_second',
'bytes_per_second',
// Percentage
'percent',
'ratio',
// Count
'count',
'operations',
// Custom
'custom',
]).describe('Metric unit');
export type MetricUnit = z.infer<typeof MetricUnit>;
/**
* Metric Aggregation Type
*/
export const MetricAggregationType = z.enum([
'sum', // Sum of all values
'avg', // Average of all values
'min', // Minimum value
'max', // Maximum value
'count', // Count of observations
'p50', // 50th percentile (median)
'p75', // 75th percentile
'p90', // 90th percentile
'p95', // 95th percentile
'p99', // 99th percentile
'p999', // 99.9th percentile
'rate', // Rate of change
'stddev', // Standard deviation
]).describe('Metric aggregation type');
export type MetricAggregationType = z.infer<typeof MetricAggregationType>;
/**
* Histogram Bucket Configuration
*/
export const HistogramBucketConfigSchema = lazySchema(() => z.object({
/**
* Bucket type
*/
type: z.enum(['linear', 'exponential', 'explicit']).describe('Bucket type'),
/**
* Linear bucket configuration
*/
linear: z.object({
start: z.number().describe('Start value'),
width: z.number().positive().describe('Bucket width'),
count: z.number().int().positive().describe('Number of buckets'),
}).optional(),
/**
* Exponential bucket configuration
*/
exponential: z.object({
start: z.number().positive().describe('Start value'),
factor: z.number().positive().describe('Growth factor'),
count: z.number().int().positive().describe('Number of buckets'),
}).optional(),
/**
* Explicit bucket boundaries
*/
explicit: z.object({
boundaries: z.array(z.number()).describe('Bucket boundaries'),
}).optional(),
}).describe('Histogram bucket configuration'));
export type HistogramBucketConfig = z.infer<typeof HistogramBucketConfigSchema>;
/**
* Metric Labels Schema
* Key-value pairs for metric dimensions
*/
export const MetricLabelsSchema = lazySchema(() => z.record(z.string(), z.string()).describe('Metric labels'));
export type MetricLabels = z.infer<typeof MetricLabelsSchema>;
/**
* Metric Definition Schema
*/
export const MetricDefinitionSchema = lazySchema(() => z.object({
/**
* Metric name (snake_case)
*/
name: z.string()
.regex(/^[a-z_][a-z0-9_]*$/)
.describe('Metric name (snake_case)'),
/**
* Display label
*/
label: z.string().optional().describe('Display label'),
/**
* Metric type
*/
type: MetricType.describe('Metric type'),
/**
* Metric unit
*/
unit: MetricUnit.optional().describe('Metric unit'),
/**
* Description
*/
description: z.string().optional().describe('Metric description'),
/**
* Label names for this metric
*/
labelNames: z.array(z.string()).optional().default([]).describe('Label names'),
/**
* Histogram configuration (for histogram type)
*/
histogram: HistogramBucketConfigSchema.optional(),
/**
* Summary configuration (for summary type)
*/
summary: z.object({
/**
* Quantiles to track
*/
quantiles: z.array(z.number().min(0).max(1)).optional().default([0.5, 0.9, 0.99]),
/**
* Max age of observations in seconds
*/
maxAge: z.number().int().positive().optional().default(600),
/**
* Number of age buckets
*/
ageBuckets: z.number().int().positive().optional().default(5),
}).optional(),
/**
* Enabled flag
*/
enabled: z.boolean().optional().default(true),
}).describe('Metric definition'));
export type MetricDefinition = z.infer<typeof MetricDefinitionSchema>;
/**
* Metric Data Point Schema
* A single metric observation
*/
export const MetricDataPointSchema = lazySchema(() => z.object({
/**
* Metric name
*/
name: z.string().describe('Metric name'),
/**
* Metric type
*/
type: MetricType.describe('Metric type'),
/**
* Timestamp (ISO 8601)
*/
timestamp: z.string().datetime().describe('Observation timestamp'),
/**
* Value (for counter and gauge)
*/
value: z.number().optional().describe('Metric value'),
/**
* Labels
*/
labels: MetricLabelsSchema.optional().describe('Metric labels'),
/**
* Histogram data
*/
histogram: z.object({
count: z.number().int().nonnegative().describe('Total count'),
sum: z.number().describe('Sum of all values'),
buckets: z.array(z.object({
upperBound: z.number().describe('Upper bound of bucket'),
count: z.number().int().nonnegative().describe('Count in bucket'),
})).describe('Histogram buckets'),
}).optional(),
/**
* Summary data
*/
summary: z.object({
count: z.number().int().nonnegative().describe('Total count'),
sum: z.number().describe('Sum of all values'),
quantiles: z.array(z.object({
quantile: z.number().min(0).max(1).describe('Quantile (0-1)'),
value: z.number().describe('Quantile value'),
})).describe('Summary quantiles'),
}).optional(),
}).describe('Metric data point'));
export type MetricDataPoint = z.infer<typeof MetricDataPointSchema>;
/**
* Time Series Data Point Schema
*/
export const TimeSeriesDataPointSchema = lazySchema(() => z.object({
/**
* Timestamp (ISO 8601)
*/
timestamp: z.string().datetime().describe('Timestamp'),
/**
* Value
*/
value: z.number().describe('Value'),
/**
* Labels/tags
*/
labels: z.record(z.string(), z.string()).optional().describe('Labels'),
}).describe('Time series data point'));
export type TimeSeriesDataPoint = z.infer<typeof TimeSeriesDataPointSchema>;
/**
* Time Series Schema
*/
export const TimeSeriesSchema = lazySchema(() => z.object({
/**
* Series name
*/
name: z.string().describe('Series name'),
/**
* Series labels
*/
labels: z.record(z.string(), z.string()).optional().describe('Series labels'),
/**
* Data points
*/
dataPoints: z.array(TimeSeriesDataPointSchema).describe('Data points'),
/**
* Start time
*/
startTime: z.string().datetime().optional().describe('Start time'),
/**
* End time
*/
endTime: z.string().datetime().optional().describe('End time'),
}).describe('Time series'));
export type TimeSeries = z.infer<typeof TimeSeriesSchema>;
/**
* Metric Aggregation Configuration
*/
export const MetricAggregationConfigSchema = lazySchema(() => z.object({
/**
* Aggregation type
*/
type: MetricAggregationType.describe('Aggregation type'),
/**
* Time window for aggregation
*/
window: z.object({
/**
* Window size in seconds
*/
size: z.number().int().positive().describe('Window size in seconds'),
/**
* Sliding window (true) or tumbling window (false)
*/
sliding: z.boolean().optional().default(false),
/**
* Slide interval for sliding windows
*/
slideInterval: z.number().int().positive().optional(),
}).optional(),
/**
* Group by labels
*/
groupBy: z.array(z.string()).optional().describe('Group by label names'),
/**
* Filters
*/
filters: z.record(z.string(), z.unknown()).optional().describe('Filter criteria'),
}).describe('Metric aggregation configuration'));
export type MetricAggregationConfig = z.infer<typeof MetricAggregationConfigSchema>;
/**
* Service Level Indicator (SLI) Schema
*/
export const ServiceLevelIndicatorSchema = lazySchema(() => z.object({
/**
* SLI name
*/
name: z.string()
.regex(/^[a-z_][a-z0-9_]*$/)
.describe('SLI name (snake_case)'),
/**
* Display label
*/
label: z.string().describe('Display label'),
/**
* Description
*/
description: z.string().optional().describe('SLI description'),
/**
* Metric name this SLI is based on
*/
metric: z.string().describe('Base metric name'),
/**
* SLI type
*/
type: z.enum([
'availability', // Percentage of successful requests
'latency', // Response time percentile
'throughput', // Requests per second
'error_rate', // Error percentage
'saturation', // Resource utilization
'custom', // Custom calculation
]).describe('SLI type'),
/**
* Success criteria
*/
successCriteria: z.object({
/**
* Threshold value
*/
threshold: z.number().describe('Threshold value'),
/**
* Comparison operator
*/
operator: z.enum(['lt', 'lte', 'gt', 'gte', 'eq']).describe('Comparison operator'),
/**
* Percentile (for latency SLIs)
*/
percentile: z.number().min(0).max(1).optional().describe('Percentile (0-1)'),
}).describe('Success criteria'),
/**
* Measurement window
*/
window: z.object({
/**
* Window size in seconds
*/
size: z.number().int().positive().describe('Window size in seconds'),
/**
* Rolling window (true) or calendar-aligned (false)
*/
rolling: z.boolean().optional().default(true),
}).describe('Measurement window'),
/**
* Enabled flag
*/
enabled: z.boolean().optional().default(true),
}).describe('Service Level Indicator'));
export type ServiceLevelIndicator = z.infer<typeof ServiceLevelIndicatorSchema>;
/**
* Service Level Objective (SLO) Schema
*/
export const ServiceLevelObjectiveSchema = lazySchema(() => z.object({
/**
* SLO name
*/
name: z.string()
.regex(/^[a-z_][a-z0-9_]*$/)
.describe('SLO name (snake_case)'),
/**
* Display label
*/
label: z.string().describe('Display label'),
/**
* Description
*/
description: z.string().optional().describe('SLO description'),
/**
* SLI this SLO is based on
*/
sli: z.string().describe('SLI name'),
/**
* Target percentage (0-100)
*/
target: z.number().min(0).max(100).describe('Target percentage'),
/**
* Time period for SLO
*/
period: z.object({
/**
* Period type
*/
type: z.enum(['rolling', 'calendar']).describe('Period type'),
/**
* Duration in seconds (for rolling)
*/
duration: z.number().int().positive().optional().describe('Duration in seconds'),
/**
* Calendar period (for calendar)
*/
calendar: z.enum(['daily', 'weekly', 'monthly', 'quarterly', 'yearly']).optional(),
}).describe('Time period'),
/**
* Error budget configuration
*/
errorBudget: z.object({
/**
* Auto-calculated budget (1 - target)
*/
enabled: z.boolean().optional().default(true),
/**
* Alert when budget consumed percentage exceeds threshold
*/
alertThreshold: z.number().min(0).max(100).optional().default(80),
/**
* Burn rate alert windows
*/
burnRateWindows: z.array(z.object({
/**
* Window size in seconds
*/
window: z.number().int().positive().describe('Window size'),
/**
* Burn rate multiplier threshold
*/
threshold: z.number().positive().describe('Burn rate threshold'),
})).optional(),
}).optional(),
/**
* Alert configuration
*/
alerts: z.array(z.object({
/**
* Alert name
*/
name: z.string().describe('Alert name'),
/**
* Severity
*/
severity: z.enum(['info', 'warning', 'critical']).describe('Alert severity'),
/**
* Condition
*/
condition: z.object({
type: z.enum(['slo_breach', 'error_budget', 'burn_rate']).describe('Condition type'),
threshold: z.number().optional().describe('Threshold value'),
}).describe('Alert condition'),
})).optional().default([]),
/**
* Enabled flag
*/
enabled: z.boolean().optional().default(true),
}).describe('Service Level Objective'));
export type ServiceLevelObjective = z.infer<typeof ServiceLevelObjectiveSchema>;
/**
* Metric Export Configuration
*/
export const MetricExportConfigSchema = lazySchema(() => z.object({
/**
* Export type
*/
type: z.enum([
'prometheus', // Prometheus exposition format
'openmetrics', // OpenMetrics format
'graphite', // Graphite plaintext protocol
'statsd', // StatsD protocol
'influxdb', // InfluxDB line protocol
'datadog', // Datadog agent
'cloudwatch', // AWS CloudWatch
'stackdriver', // Google Cloud Monitoring
'azure_monitor', // Azure Monitor
'http', // HTTP push
'custom', // Custom exporter
]).describe('Export type'),
/**
* Endpoint configuration
*/
endpoint: z.string().optional().describe('Export endpoint'),
/**
* Export interval in seconds
*/
interval: z.number().int().positive().optional().default(60),
/**
* Batch configuration
*/
batch: z.object({
enabled: z.boolean().optional().default(true),
size: z.number().int().positive().optional().default(1000),
}).optional(),
/**
* Authentication
*/
auth: z.object({
type: z.enum(['none', 'basic', 'bearer', 'api_key']).describe('Auth type'),
username: z.string().optional(),
password: z.string().optional(),
token: z.string().optional(),
apiKey: z.string().optional(),
}).optional(),
/**
* Additional configuration
*/
config: z.record(z.string(), z.unknown()).optional().describe('Additional configuration'),
}).describe('Metric export configuration'));
export type MetricExportConfig = z.infer<typeof MetricExportConfigSchema>;
/**
* Metrics Configuration Schema
*/
export const MetricsConfigSchema = lazySchema(() => z.object({
/**
* Configuration name
*/
name: z.string()
.regex(/^[a-z_][a-z0-9_]*$/)
.max(64)
.describe('Configuration name (snake_case, max 64 chars)'),
/**
* Display label
*/
label: z.string().describe('Display label'),
/**
* Enable metrics collection
*/
enabled: z.boolean().optional().default(true),
/**
* Metric definitions
*/
metrics: z.array(MetricDefinitionSchema).optional().default([]),
/**
* Default labels applied to all metrics
*/
defaultLabels: MetricLabelsSchema.optional().default({}),
/**
* Aggregation configurations
*/
aggregations: z.array(MetricAggregationConfigSchema).optional().default([]),
/**
* Service Level Indicators
*/
slis: z.array(ServiceLevelIndicatorSchema).optional().default([]),
/**
* Service Level Objectives
*/
slos: z.array(ServiceLevelObjectiveSchema).optional().default([]),
/**
* Export configurations
*/
exports: z.array(MetricExportConfigSchema).optional().default([]),
/**
* Collection interval in seconds
*/
collectionInterval: z.number().int().positive().optional().default(15),
/**
* Retention configuration
*/
retention: z.object({
/**
* Retention period in seconds
*/
period: z.number().int().positive().optional().default(604800), // 7 days
/**
* Downsampling configuration
*/
downsampling: z.array(z.object({
/**
* After this duration, downsample to this resolution
*/
afterSeconds: z.number().int().positive().describe('Downsample after seconds'),
/**
* Resolution in seconds
*/
resolution: z.number().int().positive().describe('Downsampled resolution'),
})).optional(),
}).optional(),
/**
* Cardinality limits
*/
cardinalityLimits: z.object({
/**
* Maximum unique label combinations per metric
*/
maxLabelCombinations: z.number().int().positive().optional().default(10000),
/**
* Action when limit exceeded
*/
onLimitExceeded: z.enum(['drop', 'sample', 'alert']).optional().default('alert'),
}).optional(),
}).describe('Metrics configuration'));
export type MetricsConfig = z.infer<typeof MetricsConfigSchema>;