dwctl 8.38.1

The Doubleword Control Layer - A self-hostable observability and analytics platform for LLM applications
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//! GenAI metrics implementation following OpenTelemetry Semantic Conventions
//!
//! Implements the four key metrics for GenAI observability:
//! - gen_ai.server.request.duration
//! - gen_ai.server.time_to_first_token
//! - gen_ai.server.time_per_output_token
//! - gen_ai.client.token.usage

use async_trait::async_trait;
use prometheus::{HistogramOpts, HistogramVec, Registry};
use tracing::instrument;

use crate::{metrics::MetricsRecorder, request_logging::serializers::HttpAnalyticsRow};

/// GenAI metrics instruments using Prometheus
#[derive(Clone)]
pub struct GenAiMetrics {
    /// Total request duration (required)
    request_duration: HistogramVec,
    /// Time until first byte received (recommended, streaming only)
    time_to_first_token: HistogramVec,
    /// Average time per output token during decode (recommended)
    time_per_output_token: HistogramVec,
    /// Token usage - input and output (recommended)
    token_usage: HistogramVec,
    /// Reference to the Prometheus registry
    registry: Registry,
}

impl GenAiMetrics {
    /// Create new GenAI metrics instruments and register with Prometheus
    pub fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
        // Request duration histogram (required)
        // Buckets from OTel spec: 0.01s to 81.92s (exponential with factor 2)
        let duration_buckets = vec![
            0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, 20.48, 40.96, 81.92,
        ];
        let request_duration = HistogramVec::new(
            HistogramOpts::new("gen_ai_server_request_duration_seconds", "GenAI operation duration").buckets(duration_buckets),
            &[
                "gen_ai_operation_name",
                "gen_ai_provider_name",
                "gen_ai_request_model",
                "gen_ai_response_model",
                "server_address",
                "server_port",
                "error_type",
                "request_origin",
                "batch_sla",
            ],
        )?;
        registry.register(Box::new(request_duration.clone()))?;

        // Time to first token histogram (recommended)
        // Buckets from OTel spec: 0.001s to 10.0s
        let ttft_buckets = vec![
            0.001, 0.005, 0.01, 0.02, 0.04, 0.06, 0.08, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0,
        ];
        let time_to_first_token = HistogramVec::new(
            HistogramOpts::new(
                "gen_ai_server_time_to_first_token_seconds",
                "Time to generate first token for successful responses",
            )
            .buckets(ttft_buckets),
            &[
                "gen_ai_operation_name",
                "gen_ai_provider_name",
                "gen_ai_request_model",
                "gen_ai_response_model",
                "server_address",
                "server_port",
                "request_origin",
                "batch_sla",
            ],
        )?;
        registry.register(Box::new(time_to_first_token.clone()))?;

        // Time per output token histogram (recommended)
        // Buckets from OTel spec: 0.01s to 2.5s (exponential with factor 2)
        let tpot_buckets = vec![0.01, 0.025, 0.05, 0.075, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.5];
        let time_per_output_token = HistogramVec::new(
            HistogramOpts::new(
                "gen_ai_server_time_per_output_token_seconds",
                "Time per output token generated after the first token",
            )
            .buckets(tpot_buckets),
            &[
                "gen_ai_operation_name",
                "gen_ai_provider_name",
                "gen_ai_request_model",
                "gen_ai_response_model",
                "server_address",
                "server_port",
                "request_origin",
                "batch_sla",
            ],
        )?;
        registry.register(Box::new(time_per_output_token.clone()))?;

        // Token usage histogram (recommended)
        // Buckets from OTel spec: 1 to 67108864 tokens (exponential with factor 4)
        let token_buckets = vec![
            1.0, 4.0, 16.0, 64.0, 256.0, 1024.0, 4096.0, 16384.0, 65536.0, 262144.0, 1048576.0, 4194304.0, 16777216.0, 67108864.0,
        ];
        let token_usage = HistogramVec::new(
            HistogramOpts::new("gen_ai_client_token_usage", "Number of tokens used in prompt and completion").buckets(token_buckets),
            &[
                "gen_ai_operation_name",
                "gen_ai_provider_name",
                "gen_ai_request_model",
                "gen_ai_response_model",
                "gen_ai_token_type",
                "server_address",
                "server_port",
                "request_origin",
                "batch_sla",
            ],
        )?;
        registry.register(Box::new(token_usage.clone()))?;

        Ok(Self {
            request_duration,
            time_to_first_token,
            time_per_output_token,
            token_usage,
            registry: registry.clone(),
        })
    }

    /// Get reference to the Prometheus registry
    pub fn registry(&self) -> &Registry {
        &self.registry
    }

    /// Record request duration metric
    pub fn record_request_duration(&self, duration_seconds: f64, labels: &[&str]) {
        self.request_duration.with_label_values(labels).observe(duration_seconds);
    }

    /// Record time to first token (only for streaming requests)
    pub fn record_time_to_first_token(&self, ttfb_seconds: f64, labels: &[&str]) {
        self.time_to_first_token.with_label_values(labels).observe(ttfb_seconds);
    }

    /// Record time per output token (only when output tokens > 0)
    pub fn record_time_per_output_token(&self, time_per_token_seconds: f64, labels: &[&str]) {
        self.time_per_output_token.with_label_values(labels).observe(time_per_token_seconds);
    }

    /// Record token usage (called twice per request: once for input, once for output)
    pub fn record_token_usage(&self, token_count: f64, labels: &[&str]) {
        self.token_usage.with_label_values(labels).observe(token_count);
    }
}

#[async_trait]
impl MetricsRecorder for GenAiMetrics {
    #[instrument(skip_all, name = "dwctl.record_from_analytics")]
    async fn record_from_analytics(&self, row: &HttpAnalyticsRow) {
        // Extract operation from response_type
        let operation = match row.response_type.as_str() {
            "chat_completion" | "chat_completion_stream" => "chat",
            "completion" | "completion_stream" => "text_completion",
            "embeddings" | "base64_embeddings" => "embeddings",
            _ => "",
        };

        // Determine if this is a streaming request from response_type
        let is_streaming = row.response_type.ends_with("_stream");

        // Error type for failed requests
        let error_type = if row.status_code >= 400 {
            format!("{}", row.status_code)
        } else {
            String::new()
        };

        // Build labels from config
        let server_address = &row.server_address;
        let server_port = &row.server_port.to_string();

        let provider_name = row.provider_name.as_deref().unwrap_or("");
        let request_model = row.request_model.as_deref().unwrap_or("");
        let response_model = row.response_model.as_deref().unwrap_or("");
        let request_origin = &row.request_origin;
        let batch_sla = &row.batch_sla;

        // Record request duration (always)
        let duration_labels = vec![
            operation,
            provider_name,
            request_model,
            response_model,
            server_address,
            server_port,
            &error_type,
            request_origin,
            batch_sla,
        ];
        self.record_request_duration(row.duration_ms as f64 / 1000.0, &duration_labels);

        // Record time to first token (only for streaming)
        if is_streaming && let Some(ttfb_ms) = row.duration_to_first_byte_ms {
            let ttft_labels = vec![
                operation,
                provider_name,
                request_model,
                response_model,
                server_address,
                server_port,
                request_origin,
                batch_sla,
            ];
            self.record_time_to_first_token(ttfb_ms as f64 / 1000.0, &ttft_labels);
        }

        // Record time per output token (only if we have completion tokens and ttfb)
        if row.completion_tokens > 0
            && let Some(ttfb_ms) = row.duration_to_first_byte_ms
        {
            let time_after_first_token = (row.duration_ms - ttfb_ms) as f64 / 1000.0;
            let time_per_token = time_after_first_token / row.completion_tokens as f64;
            let tpot_labels = vec![
                operation,
                provider_name,
                request_model,
                response_model,
                server_address,
                server_port,
                request_origin,
                batch_sla,
            ];
            self.record_time_per_output_token(time_per_token, &tpot_labels);
        }

        // Record token usage (input tokens)
        if row.prompt_tokens > 0 {
            let input_labels = vec![
                operation,
                provider_name,
                request_model,
                response_model,
                "input",
                server_address,
                server_port,
                request_origin,
                batch_sla,
            ];
            self.record_token_usage(row.prompt_tokens as f64, &input_labels);
        }

        // Record token usage (output tokens)
        if row.completion_tokens > 0 {
            let output_labels = vec![
                operation,
                provider_name,
                request_model,
                response_model,
                "output",
                server_address,
                server_port,
                request_origin,
                batch_sla,
            ];
            self.record_token_usage(row.completion_tokens as f64, &output_labels);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::request_logging::serializers::HttpAnalyticsRow;
    use uuid::Uuid;

    /// Helper to find a label value in a Prometheus metric
    fn find_label(labels: &[prometheus::proto::LabelPair], name: &str) -> Option<String> {
        labels.iter().find(|l| l.name() == name).map(|l| l.value().to_string())
    }

    #[tokio::test]
    async fn test_record_streaming_chat_completion() {
        // Create isolated registry for this test
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        // Create test fixture for a streaming chat completion
        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 123,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/chat/completions".to_string(),
            request_model: Some("gpt-4".to_string()),
            response_model: Some("gpt-4-0613".to_string()),
            status_code: 200,
            duration_ms: 1500,
            duration_to_first_byte_ms: Some(200),
            prompt_tokens: 10,
            completion_tokens: 50,
            reasoning_tokens: 0,
            total_tokens: 60,
            response_type: "chat_completion_stream".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.openai.com".to_string(),
            server_port: 443,
            provider_name: Some("openai".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        // Call the function under test
        metrics.record_from_analytics(&row).await;

        // Gather metrics from registry
        let metric_families = registry.gather();

        // Verify request duration metric
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_histogram = duration_metric.get_metric().first().unwrap().get_histogram();
        assert_eq!(duration_histogram.get_sample_count(), 1, "Should record one duration sample");
        assert_eq!(duration_histogram.get_sample_sum(), 1.5, "Duration should be 1.5 seconds");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(find_label(duration_labels, "gen_ai_operation_name"), Some("chat".to_string()));
        assert_eq!(find_label(duration_labels, "gen_ai_provider_name"), Some("openai".to_string()));
        assert_eq!(find_label(duration_labels, "gen_ai_request_model"), Some("gpt-4".to_string()));
        assert_eq!(find_label(duration_labels, "gen_ai_response_model"), Some("gpt-4-0613".to_string()));
        assert_eq!(find_label(duration_labels, "server_address"), Some("api.openai.com".to_string()));
        assert_eq!(find_label(duration_labels, "server_port"), Some("443".to_string()));
        assert_eq!(find_label(duration_labels, "error_type"), Some("".to_string()));

        // Verify time to first token metric (only for streaming)
        let ttft_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_to_first_token_seconds")
            .expect("Should have time to first token metric");

        let ttft_histogram = ttft_metric.get_metric().first().unwrap().get_histogram();
        assert_eq!(ttft_histogram.get_sample_count(), 1, "Should record one TTFT sample");
        assert_eq!(ttft_histogram.get_sample_sum(), 0.2, "TTFT should be 0.2 seconds");

        // Verify time per output token metric
        let tpot_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_per_output_token_seconds")
            .expect("Should have time per output token metric");

        let tpot_histogram = tpot_metric.get_metric().first().unwrap().get_histogram();
        assert_eq!(tpot_histogram.get_sample_count(), 1, "Should record one TPOT sample");
        // Expected: (1500ms - 200ms) / 50 tokens = 1300ms / 50 = 26ms = 0.026s
        assert!(
            (tpot_histogram.get_sample_sum() - 0.026).abs() < 0.0001,
            "TPOT should be approximately 0.026 seconds, got {}",
            tpot_histogram.get_sample_sum()
        );

        // Verify token usage metrics (should have 2: input and output)
        let token_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_client_token_usage")
            .expect("Should have token usage metric");

        assert_eq!(token_metric.get_metric().len(), 2, "Should have 2 token metrics (input + output)");

        // Find input token metric
        let input_token = token_metric
            .get_metric()
            .iter()
            .find(|m| find_label(m.get_label(), "gen_ai_token_type") == Some("input".to_string()))
            .expect("Should have input token metric");
        assert_eq!(input_token.get_histogram().get_sample_sum(), 10.0, "Should record 10 input tokens");

        // Find output token metric
        let output_token = token_metric
            .get_metric()
            .iter()
            .find(|m| find_label(m.get_label(), "gen_ai_token_type") == Some("output".to_string()))
            .expect("Should have output token metric");
        assert_eq!(
            output_token.get_histogram().get_sample_sum(),
            50.0,
            "Should record 50 output tokens"
        );
    }

    #[tokio::test]
    async fn test_record_non_streaming_chat_completion() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 456,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/chat/completions".to_string(),
            request_model: Some("claude-3-sonnet".to_string()),
            response_model: Some("claude-3-sonnet-20240229".to_string()),
            status_code: 200,
            duration_ms: 2000,
            duration_to_first_byte_ms: Some(1800), // Non-streaming still has TTFB but close to total
            prompt_tokens: 20,
            completion_tokens: 100,
            reasoning_tokens: 0,
            total_tokens: 120,
            response_type: "chat_completion".to_string(), // NOT streaming
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.anthropic.com".to_string(),
            server_port: 443,
            provider_name: Some("anthropic".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Should have request duration
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");
        assert_eq!(duration_metric.get_metric().first().unwrap().get_histogram().get_sample_count(), 1);

        // Should NOT have time to first token (not streaming)
        let ttft_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_to_first_token_seconds");
        assert!(
            ttft_metric.is_none() || ttft_metric.unwrap().get_metric().is_empty(),
            "Non-streaming should not record TTFT"
        );

        // Should have time per output token (has completion tokens)
        let tpot_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_per_output_token_seconds")
            .expect("Should have TPOT metric");
        assert_eq!(tpot_metric.get_metric().first().unwrap().get_histogram().get_sample_count(), 1);
    }

    #[tokio::test]
    async fn test_record_embeddings() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 789,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/embeddings".to_string(),
            request_model: Some("text-embedding-ada-002".to_string()),
            response_model: Some("text-embedding-ada-002".to_string()),
            status_code: 200,
            duration_ms: 500,
            duration_to_first_byte_ms: Some(450),
            prompt_tokens: 100,
            completion_tokens: 0, // Embeddings don't have completion tokens
            reasoning_tokens: 0,
            total_tokens: 100,
            response_type: "embeddings".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.openai.com".to_string(),
            server_port: 443,
            provider_name: Some("openai".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Verify operation name is "embeddings"
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(
            find_label(duration_labels, "gen_ai_operation_name"),
            Some("embeddings".to_string()),
            "Operation should be embeddings"
        );

        // Should only have input tokens, not output
        let token_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_client_token_usage")
            .expect("Should have token usage metric");

        assert_eq!(token_metric.get_metric().len(), 1, "Should only have 1 token metric (input only)");

        let input_token = token_metric.get_metric().first().unwrap();
        assert_eq!(find_label(input_token.get_label(), "gen_ai_token_type"), Some("input".to_string()));
        assert_eq!(input_token.get_histogram().get_sample_sum(), 100.0);

        // Should NOT have time per output token (no completion tokens)
        let tpot_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_per_output_token_seconds");
        assert!(
            tpot_metric.is_none() || tpot_metric.unwrap().get_metric().is_empty(),
            "Embeddings should not record TPOT"
        );
    }

    #[tokio::test]
    async fn test_record_failed_request() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 999,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/chat/completions".to_string(),
            request_model: Some("gpt-4".to_string()),
            response_model: None, // May not have response model on error
            status_code: 429,     // Rate limit error
            duration_ms: 100,
            duration_to_first_byte_ms: Some(100),
            prompt_tokens: 0, // No tokens on error
            completion_tokens: 0,
            reasoning_tokens: 0,
            total_tokens: 0,
            response_type: "chat_completion".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.openai.com".to_string(),
            server_port: 443,
            provider_name: Some("openai".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Should record request duration with error_type
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(
            find_label(duration_labels, "error_type"),
            Some("429".to_string()),
            "Should record error type for failed requests"
        );

        // Should NOT record token usage (no tokens)
        let token_metric = metric_families.iter().find(|m| m.name() == "gen_ai_client_token_usage");
        assert!(
            token_metric.is_none() || token_metric.unwrap().get_metric().is_empty(),
            "Should not record tokens on error"
        );
    }

    #[tokio::test]
    async fn test_record_with_missing_optional_fields() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 111,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/completions".to_string(),
            request_model: None, // Missing model
            response_model: None,
            status_code: 200,
            duration_ms: 1000,
            duration_to_first_byte_ms: None, // Missing TTFB
            prompt_tokens: 10,
            completion_tokens: 20,
            reasoning_tokens: 0,
            total_tokens: 30,
            response_type: "completion".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "localhost".to_string(),
            server_port: 8080,
            provider_name: None, // Missing provider
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Should still record metrics with empty strings for missing fields
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(
            find_label(duration_labels, "gen_ai_operation_name"),
            Some("text_completion".to_string())
        );
        assert_eq!(find_label(duration_labels, "gen_ai_provider_name"), Some("".to_string()));
        assert_eq!(find_label(duration_labels, "gen_ai_request_model"), Some("".to_string()));

        // Should NOT record TPOT without duration_to_first_byte_ms
        let tpot_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_per_output_token_seconds");
        assert!(
            tpot_metric.is_none() || tpot_metric.unwrap().get_metric().is_empty(),
            "Should not record TPOT without TTFB"
        );
    }

    #[tokio::test]
    async fn test_record_zero_completion_tokens() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 222,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/chat/completions".to_string(),
            request_model: Some("gpt-4".to_string()),
            response_model: Some("gpt-4".to_string()),
            status_code: 200,
            duration_ms: 500,
            duration_to_first_byte_ms: Some(400),
            prompt_tokens: 50,
            completion_tokens: 0, // No output tokens
            reasoning_tokens: 0,
            total_tokens: 50,
            response_type: "chat_completion".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.openai.com".to_string(),
            server_port: 443,
            provider_name: Some("openai".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Should only record input tokens
        let token_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_client_token_usage")
            .expect("Should have token usage metric");

        assert_eq!(token_metric.get_metric().len(), 1, "Should only have input tokens");

        let input_token = token_metric.get_metric().first().unwrap();
        assert_eq!(find_label(input_token.get_label(), "gen_ai_token_type"), Some("input".to_string()));

        // Should NOT record TPOT with zero completion tokens
        let tpot_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_time_per_output_token_seconds");
        assert!(
            tpot_metric.is_none() || tpot_metric.unwrap().get_metric().is_empty(),
            "Should not record TPOT with zero completion tokens"
        );
    }

    #[tokio::test]
    async fn test_record_unknown_response_type() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 333,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/unknown".to_string(),
            request_model: Some("unknown-model".to_string()),
            response_model: Some("unknown-model".to_string()),
            status_code: 200,
            duration_ms: 100,
            duration_to_first_byte_ms: Some(50),
            prompt_tokens: 0,
            completion_tokens: 0,
            reasoning_tokens: 0,
            total_tokens: 0,
            response_type: "other".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.example.com".to_string(),
            server_port: 443,
            provider_name: Some("custom".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Should record with empty operation name
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(
            find_label(duration_labels, "gen_ai_operation_name"),
            Some("".to_string()),
            "Unknown response type should have empty operation"
        );
    }

    #[tokio::test]
    async fn test_record_different_status_codes() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        // Test various error codes
        for status_code in [400, 401, 500, 503] {
            let row = HttpAnalyticsRow {
                instance_id: Uuid::new_v4(),
                correlation_id: 444,
                timestamp: chrono::Utc::now(),
                method: "POST".to_string(),
                uri: "/v1/chat/completions".to_string(),
                request_model: Some("gpt-4".to_string()),
                response_model: None,
                status_code,
                duration_ms: 100,
                duration_to_first_byte_ms: Some(50),
                prompt_tokens: 0,
                completion_tokens: 0,
                reasoning_tokens: 0,
                total_tokens: 0,
                response_type: "chat_completion".to_string(),
                user_id: None,

                access_source: "api_key".to_string(),
                input_price_per_token: None,
                output_price_per_token: None,
                server_address: "api.openai.com".to_string(),
                server_port: 443,
                provider_name: Some("openai".to_string()),
                fusillade_batch_id: None,
                fusillade_request_id: None,
                custom_id: None,
                request_origin: "api".to_string(),
                batch_sla: String::new(),
                batch_request_source: String::new(),
            };

            metrics.record_from_analytics(&row).await;
        }

        let metric_families = registry.gather();
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        // Should have recorded 4 different error types
        assert_eq!(
            duration_metric.get_metric().len(),
            4,
            "Should have 4 metrics for different error codes"
        );
    }

    #[tokio::test]
    async fn test_base64_embeddings_response_type() {
        let registry = Registry::new();
        let metrics = GenAiMetrics::new(&registry).expect("Failed to create metrics");

        let row = HttpAnalyticsRow {
            instance_id: Uuid::new_v4(),
            correlation_id: 555,
            timestamp: chrono::Utc::now(),
            method: "POST".to_string(),
            uri: "/v1/embeddings".to_string(),
            request_model: Some("text-embedding-3-large".to_string()),
            response_model: Some("text-embedding-3-large".to_string()),
            status_code: 200,
            duration_ms: 300,
            duration_to_first_byte_ms: Some(250),
            prompt_tokens: 50,
            completion_tokens: 0,
            reasoning_tokens: 0,
            total_tokens: 50,
            response_type: "base64_embeddings".to_string(),
            user_id: None,

            access_source: "api_key".to_string(),
            input_price_per_token: None,
            output_price_per_token: None,
            server_address: "api.openai.com".to_string(),
            server_port: 443,
            provider_name: Some("openai".to_string()),
            fusillade_batch_id: None,
            fusillade_request_id: None,
            custom_id: None,
            request_origin: "api".to_string(),
            batch_sla: String::new(),
            batch_request_source: String::new(),
        };

        metrics.record_from_analytics(&row).await;
        let metric_families = registry.gather();

        // Verify operation name is "embeddings" for base64_embeddings
        let duration_metric = metric_families
            .iter()
            .find(|m| m.name() == "gen_ai_server_request_duration_seconds")
            .expect("Should have request duration metric");

        let duration_labels = duration_metric.get_metric().first().unwrap().get_label();
        assert_eq!(
            find_label(duration_labels, "gen_ai_operation_name"),
            Some("embeddings".to_string()),
            "base64_embeddings should map to embeddings operation"
        );
    }
}