-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracing.zod.ts
More file actions
697 lines (583 loc) · 16.3 KB
/
tracing.zod.ts
File metadata and controls
697 lines (583 loc) · 16.3 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Tracing Protocol - Distributed Tracing & Observability
*
* Comprehensive distributed tracing based on OpenTelemetry standards:
* - Trace context propagation
* - Span creation and management
* - Sampling strategies
* - Integration with tracing backends (Jaeger, Zipkin, etc.)
* - W3C Trace Context standard compliance
*/
/**
* Trace State Schema
* W3C Trace Context tracestate header
*/
export const TraceStateSchema = z.object({
/**
* Vendor-specific key-value pairs
*/
entries: z.record(z.string(), z.string()).describe('Trace state entries'),
}).describe('Trace state');
export type TraceState = z.infer<typeof TraceStateSchema>;
/**
* Trace Flags Enum
* W3C Trace Context trace flags
*/
export const TraceFlagsSchema = z.number().int().min(0).max(255).describe('Trace flags bitmap');
export type TraceFlags = z.infer<typeof TraceFlagsSchema>;
/**
* Trace Context Schema
* W3C Trace Context standard
*/
export const TraceContextSchema = z.object({
/**
* Trace ID (128-bit identifier, 32 hex chars)
*/
traceId: z.string()
.regex(/^[0-9a-f]{32}$/)
.describe('Trace ID (32 hex chars)'),
/**
* Span ID (64-bit identifier, 16 hex chars)
*/
spanId: z.string()
.regex(/^[0-9a-f]{16}$/)
.describe('Span ID (16 hex chars)'),
/**
* Trace flags (8-bit)
*/
traceFlags: TraceFlagsSchema.optional().default(1),
/**
* Trace state (vendor-specific)
*/
traceState: TraceStateSchema.optional(),
/**
* Parent span ID
*/
parentSpanId: z.string()
.regex(/^[0-9a-f]{16}$/)
.optional()
.describe('Parent span ID (16 hex chars)'),
/**
* Is sampled
*/
sampled: z.boolean().optional().default(true),
/**
* Remote context (from incoming request)
*/
remote: z.boolean().optional().default(false),
}).describe('Trace context (W3C Trace Context)');
export type TraceContext = z.infer<typeof TraceContextSchema>;
/**
* Span Kind Enum
* OpenTelemetry span kinds
*/
export const SpanKind = z.enum([
'internal', // Internal operation
'server', // Server-side request handling
'client', // Client-side request
'producer', // Message producer
'consumer', // Message consumer
]).describe('Span kind');
export type SpanKind = z.infer<typeof SpanKind>;
/**
* Span Status Enum
* OpenTelemetry span status
*/
export const SpanStatus = z.enum([
'unset', // Default status
'ok', // Successful operation
'error', // Error occurred
]).describe('Span status');
export type SpanStatus = z.infer<typeof SpanStatus>;
/**
* Span Attribute Value Schema
*/
export const SpanAttributeValueSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.array(z.string()),
z.array(z.number()),
z.array(z.boolean()),
]).describe('Span attribute value');
export type SpanAttributeValue = z.infer<typeof SpanAttributeValueSchema>;
/**
* Span Attributes Schema
* OpenTelemetry semantic conventions
*/
export const SpanAttributesSchema = z.record(z.string(), SpanAttributeValueSchema).describe('Span attributes');
export type SpanAttributes = z.infer<typeof SpanAttributesSchema>;
/**
* Span Event Schema
*/
export const SpanEventSchema = z.object({
/**
* Event name
*/
name: z.string().describe('Event name'),
/**
* Event timestamp (ISO 8601)
*/
timestamp: z.string().datetime().describe('Event timestamp'),
/**
* Event attributes
*/
attributes: SpanAttributesSchema.optional().describe('Event attributes'),
}).describe('Span event');
export type SpanEvent = z.infer<typeof SpanEventSchema>;
/**
* Span Link Schema
* Links to other spans
*/
export const SpanLinkSchema = z.object({
/**
* Linked trace context
*/
context: TraceContextSchema.describe('Linked trace context'),
/**
* Link attributes
*/
attributes: SpanAttributesSchema.optional().describe('Link attributes'),
}).describe('Span link');
export type SpanLink = z.infer<typeof SpanLinkSchema>;
/**
* Span Schema
* OpenTelemetry span representation
*/
export const SpanSchema = z.object({
/**
* Trace context
*/
context: TraceContextSchema.describe('Trace context'),
/**
* Span name
*/
name: z.string().describe('Span name'),
/**
* Span kind
*/
kind: SpanKind.optional().default('internal'),
/**
* Start time (ISO 8601)
*/
startTime: z.string().datetime().describe('Span start time'),
/**
* End time (ISO 8601)
*/
endTime: z.string().datetime().optional().describe('Span end time'),
/**
* Duration in milliseconds
*/
duration: z.number().nonnegative().optional().describe('Duration in milliseconds'),
/**
* Span status
*/
status: z.object({
code: SpanStatus.describe('Status code'),
message: z.string().optional().describe('Status message'),
}).optional(),
/**
* Span attributes
*/
attributes: SpanAttributesSchema.optional().default({}),
/**
* Span events
*/
events: z.array(SpanEventSchema).optional().default([]),
/**
* Span links
*/
links: z.array(SpanLinkSchema).optional().default([]),
/**
* Resource attributes
*/
resource: SpanAttributesSchema.optional().describe('Resource attributes'),
/**
* Instrumentation library
*/
instrumentationLibrary: z.object({
name: z.string().describe('Library name'),
version: z.string().optional().describe('Library version'),
}).optional(),
}).describe('OpenTelemetry span');
export type Span = z.infer<typeof SpanSchema>;
/**
* Sampling Decision Enum
*/
export const SamplingDecision = z.enum([
'drop', // Do not record or export
'record_only', // Record but do not export
'record_and_sample', // Record and export
]).describe('Sampling decision');
export type SamplingDecision = z.infer<typeof SamplingDecision>;
/**
* Sampling Strategy Type Enum
*/
export const SamplingStrategyType = z.enum([
'always_on', // Always sample
'always_off', // Never sample
'trace_id_ratio', // Sample based on trace ID ratio
'rate_limiting', // Rate-limited sampling
'parent_based', // Respect parent span sampling decision
'probability', // Probability-based sampling
'composite', // Combine multiple strategies
'custom', // Custom sampling logic
]).describe('Sampling strategy type');
export type SamplingStrategyType = z.infer<typeof SamplingStrategyType>;
/**
* Trace Sampling Configuration Schema
*/
export const TraceSamplingConfigSchema = z.object({
/**
* Sampling strategy type
*/
type: SamplingStrategyType.describe('Sampling strategy'),
/**
* Sample ratio (0.0 to 1.0) for trace_id_ratio and probability strategies
*/
ratio: z.number().min(0).max(1).optional().describe('Sample ratio (0-1)'),
/**
* Rate limit (traces per second) for rate_limiting strategy
*/
rateLimit: z.number().positive().optional().describe('Traces per second'),
/**
* Parent-based configuration
*/
parentBased: z.object({
/**
* Sampler to use when parent is sampled
*/
whenParentSampled: SamplingStrategyType.optional().default('always_on'),
/**
* Sampler to use when parent is not sampled
*/
whenParentNotSampled: SamplingStrategyType.optional().default('always_off'),
/**
* Sampler to use when there is no parent (root span)
*/
root: SamplingStrategyType.optional().default('trace_id_ratio'),
/**
* Root sampler ratio
*/
rootRatio: z.number().min(0).max(1).optional().default(0.1),
}).optional(),
/**
* Composite sampling (multiple strategies)
*/
composite: z.array(z.object({
strategy: SamplingStrategyType.describe('Strategy type'),
ratio: z.number().min(0).max(1).optional(),
condition: z.record(z.string(), z.unknown()).optional().describe('Condition for this strategy'),
})).optional(),
/**
* Sampling rules
*/
rules: z.array(z.object({
/**
* Rule name
*/
name: z.string().describe('Rule name'),
/**
* Match condition
*/
match: z.object({
/**
* Service name pattern
*/
service: z.string().optional(),
/**
* Span name pattern (regex)
*/
spanName: z.string().optional(),
/**
* Attribute filters
*/
attributes: z.record(z.string(), z.unknown()).optional(),
}).optional(),
/**
* Sampling decision for matching spans
*/
decision: SamplingDecision.describe('Sampling decision'),
/**
* Sample rate for this rule
*/
rate: z.number().min(0).max(1).optional(),
})).optional().default([]),
/**
* Custom sampler ID (for custom strategy)
*/
customSamplerId: z.string().optional().describe('Custom sampler identifier'),
}).describe('Trace sampling configuration');
export type TraceSamplingConfig = z.infer<typeof TraceSamplingConfigSchema>;
/**
* Trace Context Propagation Format Enum
*/
export const TracePropagationFormat = z.enum([
'w3c', // W3C Trace Context
'b3', // Zipkin B3 (single header)
'b3_multi', // Zipkin B3 (multi header)
'jaeger', // Jaeger propagation
'xray', // AWS X-Ray
'ottrace', // OpenTracing
'custom', // Custom format
]).describe('Trace propagation format');
export type TracePropagationFormat = z.infer<typeof TracePropagationFormat>;
/**
* Trace Context Propagation Schema
*/
export const TraceContextPropagationSchema = z.object({
/**
* Propagation formats (in priority order)
*/
formats: z.array(TracePropagationFormat).optional().default(['w3c']),
/**
* Extract context from incoming requests
*/
extract: z.boolean().optional().default(true),
/**
* Inject context into outgoing requests
*/
inject: z.boolean().optional().default(true),
/**
* Custom header mappings
*/
headers: z.object({
/**
* Trace ID header name
*/
traceId: z.string().optional(),
/**
* Span ID header name
*/
spanId: z.string().optional(),
/**
* Trace flags header name
*/
traceFlags: z.string().optional(),
/**
* Trace state header name
*/
traceState: z.string().optional(),
}).optional(),
/**
* Baggage propagation
*/
baggage: z.object({
/**
* Enable baggage propagation
*/
enabled: z.boolean().optional().default(true),
/**
* Maximum baggage size in bytes
*/
maxSize: z.number().int().positive().optional().default(8192),
/**
* Allowed baggage keys (whitelist)
*/
allowedKeys: z.array(z.string()).optional(),
}).optional(),
}).describe('Trace context propagation');
export type TraceContextPropagation = z.infer<typeof TraceContextPropagationSchema>;
/**
* OpenTelemetry Exporter Type Enum
*/
export const OtelExporterType = z.enum([
'otlp_http', // OTLP over HTTP
'otlp_grpc', // OTLP over gRPC
'jaeger', // Jaeger
'zipkin', // Zipkin
'console', // Console (for debugging)
'datadog', // Datadog
'honeycomb', // Honeycomb
'lightstep', // Lightstep
'newrelic', // New Relic
'custom', // Custom exporter
]).describe('OpenTelemetry exporter type');
export type OtelExporterType = z.infer<typeof OtelExporterType>;
/**
* OpenTelemetry Compatibility Schema
*/
export const OpenTelemetryCompatibilitySchema = z.object({
/**
* OpenTelemetry SDK version
*/
sdkVersion: z.string().optional().describe('OTel SDK version'),
/**
* Exporter configuration
*/
exporter: z.object({
/**
* Exporter type
*/
type: OtelExporterType.describe('Exporter type'),
/**
* Endpoint URL
*/
endpoint: z.string().url().optional().describe('Exporter endpoint'),
/**
* Protocol version
*/
protocol: z.string().optional().describe('Protocol version'),
/**
* Headers
*/
headers: z.record(z.string(), z.string()).optional().describe('HTTP headers'),
/**
* Timeout in milliseconds
*/
timeout: z.number().int().positive().optional().default(10000),
/**
* Compression
*/
compression: z.enum(['none', 'gzip']).optional().default('none'),
/**
* Batch configuration
*/
batch: z.object({
/**
* Maximum batch size
*/
maxBatchSize: z.number().int().positive().optional().default(512),
/**
* Maximum queue size
*/
maxQueueSize: z.number().int().positive().optional().default(2048),
/**
* Export timeout in milliseconds
*/
exportTimeout: z.number().int().positive().optional().default(30000),
/**
* Scheduled delay in milliseconds
*/
scheduledDelay: z.number().int().positive().optional().default(5000),
}).optional(),
}).describe('Exporter configuration'),
/**
* Resource attributes (service identification)
*/
resource: z.object({
/**
* Service name
*/
serviceName: z.string().describe('Service name'),
/**
* Service version
*/
serviceVersion: z.string().optional().describe('Service version'),
/**
* Service instance ID
*/
serviceInstanceId: z.string().optional().describe('Service instance ID'),
/**
* Service namespace
*/
serviceNamespace: z.string().optional().describe('Service namespace'),
/**
* Deployment environment
*/
deploymentEnvironment: z.string().optional().describe('Deployment environment'),
/**
* Additional resource attributes
*/
attributes: SpanAttributesSchema.optional().describe('Additional resource attributes'),
}).describe('Resource attributes'),
/**
* Instrumentation configuration
*/
instrumentation: z.object({
/**
* Auto-instrumentation enabled
*/
autoInstrumentation: z.boolean().optional().default(true),
/**
* Instrumentation libraries to enable
*/
libraries: z.array(z.string()).optional().describe('Enabled libraries'),
/**
* Instrumentation libraries to disable
*/
disabledLibraries: z.array(z.string()).optional().describe('Disabled libraries'),
}).optional(),
/**
* Semantic conventions version
*/
semanticConventionsVersion: z.string().optional().describe('Semantic conventions version'),
}).describe('OpenTelemetry compatibility configuration');
export type OpenTelemetryCompatibility = z.infer<typeof OpenTelemetryCompatibilitySchema>;
/**
* Tracing Configuration Schema
*/
export const TracingConfigSchema = 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 tracing
*/
enabled: z.boolean().optional().default(true),
/**
* Sampling configuration
*/
sampling: TraceSamplingConfigSchema.optional().default({ type: 'always_on', rules: [] }),
/**
* Context propagation
*/
propagation: TraceContextPropagationSchema.optional().default({ formats: ['w3c'], extract: true, inject: true }),
/**
* OpenTelemetry configuration
*/
openTelemetry: OpenTelemetryCompatibilitySchema.optional(),
/**
* Span limits
*/
spanLimits: z.object({
/**
* Maximum number of attributes per span
*/
maxAttributes: z.number().int().positive().optional().default(128),
/**
* Maximum number of events per span
*/
maxEvents: z.number().int().positive().optional().default(128),
/**
* Maximum number of links per span
*/
maxLinks: z.number().int().positive().optional().default(128),
/**
* Maximum attribute value length
*/
maxAttributeValueLength: z.number().int().positive().optional().default(4096),
}).optional(),
/**
* Trace ID generator
*/
traceIdGenerator: z.enum(['random', 'uuid', 'custom']).optional().default('random'),
/**
* Custom trace ID generator ID
*/
customTraceIdGeneratorId: z.string().optional().describe('Custom generator identifier'),
/**
* Performance configuration
*/
performance: z.object({
/**
* Async span export
*/
asyncExport: z.boolean().optional().default(true),
/**
* Background export interval in milliseconds
*/
exportInterval: z.number().int().positive().optional().default(5000),
}).optional(),
}).describe('Tracing configuration');
export type TracingConfig = z.infer<typeof TracingConfigSchema>;