litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
Documentation
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
//! Monitoring and observability configuration
//!
//! Unified configuration for metrics, tracing, health checks, and logging.

use super::*;
use serde::{Deserialize, Serialize};

/// Monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MonitoringConfig {
    /// Metrics configuration
    #[serde(default)]
    pub metrics: MetricsConfig,
    /// Tracing configuration
    #[serde(default)]
    pub tracing: TracingConfig,
    /// Health check configuration
    #[serde(default)]
    pub health: HealthConfig,
    /// Logging configuration
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub logging: Option<LoggingConfig>,
}

impl MonitoringConfig {
    /// Merge monitoring configurations
    pub fn merge(mut self, other: Self) -> Self {
        self.metrics = self.metrics.merge(other.metrics);
        self.tracing = self.tracing.merge(other.tracing);
        self.health = self.health.merge(other.health);
        if other.logging.is_some() {
            self.logging = other.logging;
        }
        self
    }
}

/// Metrics configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// Enable metrics
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Metrics port
    #[serde(default = "default_metrics_port")]
    pub port: u16,
    /// Metrics path / endpoint
    #[serde(default = "default_metrics_path")]
    pub path: String,
    /// Collection interval in seconds
    #[serde(default = "default_interval_seconds")]
    pub interval_seconds: u64,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            port: default_metrics_port(),
            path: default_metrics_path(),
            interval_seconds: default_interval_seconds(),
        }
    }
}

impl MetricsConfig {
    /// Merge metrics configurations
    pub fn merge(mut self, other: Self) -> Self {
        if !other.enabled {
            self.enabled = other.enabled;
        }
        if other.port != default_metrics_port() {
            self.port = other.port;
        }
        if other.path != default_metrics_path() {
            self.path = other.path;
        }
        if other.interval_seconds != default_interval_seconds() {
            self.interval_seconds = other.interval_seconds;
        }
        self
    }
}

/// Tracing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingConfig {
    /// Enable tracing
    #[serde(default)]
    pub enabled: bool,
    /// Tracing endpoint (e.g. OpenTelemetry collector URL)
    pub endpoint: Option<String>,
    /// Service name
    #[serde(default = "default_service_name")]
    pub service_name: String,
    /// Sampling rate (0.0–1.0)
    #[serde(default = "default_sampling_rate")]
    pub sampling_rate: f64,
    /// Jaeger-specific configuration
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jaeger: Option<JaegerConfig>,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            endpoint: None,
            service_name: default_service_name(),
            sampling_rate: default_sampling_rate(),
            jaeger: None,
        }
    }
}

impl TracingConfig {
    /// Merge tracing configurations
    pub fn merge(mut self, other: Self) -> Self {
        if other.enabled {
            self.enabled = other.enabled;
        }
        if other.endpoint.is_some() {
            self.endpoint = other.endpoint;
        }
        if other.service_name != default_service_name() {
            self.service_name = other.service_name;
        }
        if (other.sampling_rate - default_sampling_rate()).abs() > f64::EPSILON {
            self.sampling_rate = other.sampling_rate;
        }
        if other.jaeger.is_some() {
            self.jaeger = other.jaeger;
        }
        self
    }
}

/// Jaeger configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JaegerConfig {
    /// Agent endpoint
    pub agent_endpoint: String,
    /// Service name
    pub service_name: String,
}

/// Health check configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthConfig {
    /// Health check path
    #[serde(default = "default_health_path")]
    pub path: String,
    /// Enable detailed health checks
    #[serde(default = "default_true")]
    pub detailed: bool,
}

impl Default for HealthConfig {
    fn default() -> Self {
        Self {
            path: default_health_path(),
            detailed: true,
        }
    }
}

impl HealthConfig {
    /// Merge health configurations
    pub fn merge(mut self, other: Self) -> Self {
        if other.path != default_health_path() {
            self.path = other.path;
        }
        if !other.detailed {
            self.detailed = other.detailed;
        }
        self
    }
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Log level
    #[serde(default = "default_log_level")]
    pub level: String,
    /// Output format
    #[serde(default = "default_log_format")]
    pub format: LogFormat,
    /// Output targets
    #[serde(default)]
    pub outputs: Vec<LogOutput>,
}

/// Log format
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
    Text,
    Json,
    Structured,
}

/// Log output target
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum LogOutput {
    #[serde(rename = "console")]
    Console,
    #[serde(rename = "file")]
    File { path: String },
    #[serde(rename = "syslog")]
    Syslog { facility: String },
}

fn default_interval_seconds() -> u64 {
    15
}

fn default_sampling_rate() -> f64 {
    0.1
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_log_format() -> LogFormat {
    LogFormat::Json
}

#[cfg(test)]
mod tests {
    use super::*;

    // ==================== MetricsConfig Tests ====================

    #[test]
    fn test_metrics_config_default() {
        let config = MetricsConfig::default();
        assert!(config.enabled);
        assert_eq!(config.port, 9090);
        assert_eq!(config.path, "/metrics");
        assert_eq!(config.interval_seconds, 15);
    }

    #[test]
    fn test_metrics_config_structure() {
        let config = MetricsConfig {
            enabled: false,
            port: 8080,
            path: "/prometheus".to_string(),
            interval_seconds: 30,
        };
        assert!(!config.enabled);
        assert_eq!(config.port, 8080);
        assert_eq!(config.path, "/prometheus");
        assert_eq!(config.interval_seconds, 30);
    }

    #[test]
    fn test_metrics_config_serialization() {
        let config = MetricsConfig {
            enabled: true,
            port: 9100,
            path: "/stats".to_string(),
            interval_seconds: 60,
        };
        let json = serde_json::to_value(&config).unwrap();
        assert_eq!(json["enabled"], true);
        assert_eq!(json["port"], 9100);
        assert_eq!(json["path"], "/stats");
        assert_eq!(json["interval_seconds"], 60);
    }

    #[test]
    fn test_metrics_config_deserialization() {
        let json = r#"{"enabled": false, "port": 3000, "path": "/api/metrics"}"#;
        let config: MetricsConfig = serde_json::from_str(json).unwrap();
        assert!(!config.enabled);
        assert_eq!(config.port, 3000);
        assert_eq!(config.interval_seconds, 15); // default
    }

    #[test]
    fn test_metrics_config_merge_disabled() {
        let base = MetricsConfig::default();
        let other = MetricsConfig {
            enabled: false,
            ..MetricsConfig::default()
        };
        let merged = base.merge(other);
        assert!(!merged.enabled);
    }

    #[test]
    fn test_metrics_config_merge_port() {
        let base = MetricsConfig::default();
        let other = MetricsConfig {
            port: 8888,
            ..MetricsConfig::default()
        };
        let merged = base.merge(other);
        assert_eq!(merged.port, 8888);
    }

    #[test]
    fn test_metrics_config_merge_interval() {
        let base = MetricsConfig::default();
        let other = MetricsConfig {
            interval_seconds: 60,
            ..MetricsConfig::default()
        };
        let merged = base.merge(other);
        assert_eq!(merged.interval_seconds, 60);
    }

    #[test]
    fn test_metrics_config_clone() {
        let config = MetricsConfig::default();
        let cloned = config.clone();
        assert_eq!(config.enabled, cloned.enabled);
        assert_eq!(config.port, cloned.port);
    }

    // ==================== TracingConfig Tests ====================

    #[test]
    fn test_tracing_config_default() {
        let config = TracingConfig::default();
        assert!(!config.enabled);
        assert!(config.endpoint.is_none());
        assert_eq!(config.service_name, "litellm-rs");
        assert!((config.sampling_rate - 0.1).abs() < f64::EPSILON);
        assert!(config.jaeger.is_none());
    }

    #[test]
    fn test_tracing_config_structure() {
        let config = TracingConfig {
            enabled: true,
            endpoint: Some("http://jaeger:14268".to_string()),
            service_name: "my-gateway".to_string(),
            sampling_rate: 0.5,
            jaeger: None,
        };
        assert!(config.enabled);
        assert_eq!(config.endpoint, Some("http://jaeger:14268".to_string()));
    }

    #[test]
    fn test_tracing_config_with_jaeger() {
        let config = TracingConfig {
            enabled: true,
            endpoint: None,
            service_name: "gw".to_string(),
            sampling_rate: 1.0,
            jaeger: Some(JaegerConfig {
                agent_endpoint: "localhost:6831".to_string(),
                service_name: "my-service".to_string(),
            }),
        };
        let jaeger = config.jaeger.unwrap();
        assert_eq!(jaeger.agent_endpoint, "localhost:6831");
    }

    #[test]
    fn test_tracing_config_serialization() {
        let config = TracingConfig {
            enabled: true,
            endpoint: Some("http://otel:4317".to_string()),
            service_name: "api-gateway".to_string(),
            sampling_rate: 0.25,
            jaeger: None,
        };
        let json = serde_json::to_value(&config).unwrap();
        assert_eq!(json["enabled"], true);
        assert_eq!(json["endpoint"], "http://otel:4317");
    }

    #[test]
    fn test_tracing_config_deserialization() {
        let json =
            r#"{"enabled": true, "endpoint": "http://zipkin:9411", "service_name": "tracer"}"#;
        let config: TracingConfig = serde_json::from_str(json).unwrap();
        assert!(config.enabled);
        assert_eq!(config.service_name, "tracer");
        // sampling_rate defaults to 0.1
        assert!((config.sampling_rate - 0.1).abs() < f64::EPSILON);
    }

    #[test]
    fn test_tracing_config_merge_enabled() {
        let base = TracingConfig::default();
        let other = TracingConfig {
            enabled: true,
            ..TracingConfig::default()
        };
        let merged = base.merge(other);
        assert!(merged.enabled);
    }

    #[test]
    fn test_tracing_config_merge_endpoint() {
        let base = TracingConfig::default();
        let other = TracingConfig {
            endpoint: Some("http://collector:4317".to_string()),
            ..TracingConfig::default()
        };
        let merged = base.merge(other);
        assert_eq!(merged.endpoint, Some("http://collector:4317".to_string()));
    }

    #[test]
    fn test_tracing_config_merge_sampling_rate() {
        let base = TracingConfig::default();
        let other = TracingConfig {
            sampling_rate: 0.5,
            ..TracingConfig::default()
        };
        let merged = base.merge(other);
        assert!((merged.sampling_rate - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_tracing_config_clone() {
        let config = TracingConfig::default();
        let cloned = config.clone();
        assert_eq!(config.enabled, cloned.enabled);
        assert_eq!(config.service_name, cloned.service_name);
    }

    // ==================== HealthConfig Tests ====================

    #[test]
    fn test_health_config_default() {
        let config = HealthConfig::default();
        assert_eq!(config.path, "/health");
        assert!(config.detailed);
    }

    #[test]
    fn test_health_config_structure() {
        let config = HealthConfig {
            path: "/api/health".to_string(),
            detailed: false,
        };
        assert_eq!(config.path, "/api/health");
        assert!(!config.detailed);
    }

    #[test]
    fn test_health_config_serialization() {
        let config = HealthConfig {
            path: "/status".to_string(),
            detailed: true,
        };
        let json = serde_json::to_value(&config).unwrap();
        assert_eq!(json["path"], "/status");
        assert_eq!(json["detailed"], true);
    }

    #[test]
    fn test_health_config_deserialization() {
        let json = r#"{"path": "/ready", "detailed": false}"#;
        let config: HealthConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.path, "/ready");
        assert!(!config.detailed);
    }

    #[test]
    fn test_health_config_merge_path() {
        let base = HealthConfig::default();
        let other = HealthConfig {
            path: "/healthz".to_string(),
            detailed: true,
        };
        let merged = base.merge(other);
        assert_eq!(merged.path, "/healthz");
    }

    #[test]
    fn test_health_config_merge_detailed() {
        let base = HealthConfig::default();
        let other = HealthConfig {
            path: "/health".to_string(),
            detailed: false,
        };
        let merged = base.merge(other);
        assert!(!merged.detailed);
    }

    #[test]
    fn test_health_config_clone() {
        let config = HealthConfig::default();
        let cloned = config.clone();
        assert_eq!(config.path, cloned.path);
        assert_eq!(config.detailed, cloned.detailed);
    }

    // ==================== LoggingConfig Tests ====================

    #[test]
    fn test_log_format_serialization() {
        assert_eq!(serde_json::to_string(&LogFormat::Text).unwrap(), "\"text\"");
        assert_eq!(serde_json::to_string(&LogFormat::Json).unwrap(), "\"json\"");
        assert_eq!(
            serde_json::to_string(&LogFormat::Structured).unwrap(),
            "\"structured\""
        );
    }

    #[test]
    fn test_log_output_console() {
        let json = r#"{"type": "console"}"#;
        let output: LogOutput = serde_json::from_str(json).unwrap();
        assert!(matches!(output, LogOutput::Console));
    }

    #[test]
    fn test_log_output_file() {
        let json = r#"{"type": "file", "path": "/tmp/test.log"}"#;
        let output: LogOutput = serde_json::from_str(json).unwrap();
        match output {
            LogOutput::File { path } => assert_eq!(path, "/tmp/test.log"),
            _ => panic!("Expected File"),
        }
    }

    // ==================== MonitoringConfig Tests ====================

    #[test]
    fn test_monitoring_config_default() {
        let config = MonitoringConfig::default();
        assert!(config.metrics.enabled);
        assert!(!config.tracing.enabled);
        assert!(config.health.detailed);
        assert!(config.logging.is_none());
    }

    #[test]
    fn test_monitoring_config_with_logging() {
        let config = MonitoringConfig {
            logging: Some(LoggingConfig {
                level: "debug".to_string(),
                format: LogFormat::Json,
                outputs: vec![LogOutput::Console],
            }),
            ..MonitoringConfig::default()
        };
        assert!(config.logging.is_some());
        assert_eq!(config.logging.unwrap().level, "debug");
    }

    #[test]
    fn test_monitoring_config_serialization() {
        let config = MonitoringConfig::default();
        let json = serde_json::to_value(&config).unwrap();
        assert!(json["metrics"].is_object());
        assert!(json["tracing"].is_object());
        assert!(json["health"].is_object());
    }

    #[test]
    fn test_monitoring_config_merge() {
        let base = MonitoringConfig::default();
        let other = MonitoringConfig {
            metrics: MetricsConfig {
                enabled: false,
                ..MetricsConfig::default()
            },
            ..MonitoringConfig::default()
        };
        let merged = base.merge(other);
        assert!(!merged.metrics.enabled);
    }

    #[test]
    fn test_monitoring_config_merge_logging() {
        let base = MonitoringConfig::default();
        let other = MonitoringConfig {
            logging: Some(LoggingConfig {
                level: "warn".to_string(),
                format: LogFormat::Text,
                outputs: vec![],
            }),
            ..MonitoringConfig::default()
        };
        let merged = base.merge(other);
        assert!(merged.logging.is_some());
    }

    #[test]
    fn test_monitoring_config_clone() {
        let config = MonitoringConfig::default();
        let cloned = config.clone();
        assert_eq!(config.metrics.enabled, cloned.metrics.enabled);
    }
}