market-maker-rs 0.2.0

A Rust library implementing quantitative market making strategies, starting with the Avellaneda-Stoikov model
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
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
//! Prometheus metrics export for trading system monitoring.
//!
//! This module provides Prometheus-compatible metrics export for integration
//! with Grafana dashboards and alerting systems.
//!
//! # Feature Flag
//!
//! This module requires the `prometheus` feature flag:
//!
//! ```toml
//! [dependencies]
//! market-maker-rs = { version = "0.1", features = ["prometheus"] }
//! ```
//!
//! # Overview
//!
//! The module provides:
//!
//! - **PrometheusMetrics**: Registry with all trading metrics
//! - **MetricsServer**: HTTP server exposing `/metrics` endpoint
//! - **MetricsBridge**: Adapter to sync with `LiveMetrics`
//!
//! # Example
//!
//! ```rust,ignore
//! use market_maker_rs::analytics::prometheus_export::{PrometheusMetrics, MetricsServer};
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create metrics registry
//!     let metrics = Arc::new(PrometheusMetrics::new("marketmaker")?);
//!     
//!     // Start HTTP server on port 9090
//!     let server = MetricsServer::new(Arc::clone(&metrics), "0.0.0.0:9090");
//!     let handle = server.spawn();
//!     
//!     // Record metrics during trading
//!     metrics.inc_quotes();
//!     metrics.inc_orders_submitted();
//!     metrics.set_position(100.0);
//!     metrics.set_pnl(500.0, 50.0);
//!     
//!     // Server runs in background, metrics available at http://localhost:9090/metrics
//!     handle.await?;
//!     Ok(())
//! }
//! ```

use prometheus::{Counter, Encoder, Gauge, Histogram, HistogramOpts, Opts, Registry, TextEncoder};
use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::Arc;

use http_body_util::Full;
use hyper::body::Bytes;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;

use super::live_metrics::LiveMetrics;

/// Default histogram buckets for latency measurements in milliseconds.
const LATENCY_BUCKETS: &[f64] = &[
    0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0,
];

/// Default histogram buckets for spread measurements in basis points.
const SPREAD_BUCKETS: &[f64] = &[1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0];

/// Prometheus metrics registry for market making operations.
///
/// Contains all metric types needed for monitoring a trading system:
/// - Counters for event counts (quotes, orders, fills)
/// - Gauges for current values (position, PnL, spread)
/// - Histograms for distributions (latency, spread)
///
/// # Metric Naming Convention
///
/// All metrics follow the pattern: `{namespace}_{subsystem}_{name}_{unit}`
///
/// Examples:
/// - `marketmaker_orders_submitted_total`
/// - `marketmaker_latency_order_milliseconds`
/// - `marketmaker_position_current`
#[derive(Debug)]
pub struct PrometheusMetrics {
    registry: Registry,

    // Counters
    quotes_total: Counter,
    orders_submitted_total: Counter,
    orders_filled_total: Counter,
    orders_cancelled_total: Counter,
    orders_rejected_total: Counter,
    partial_fills_total: Counter,

    // Gauges
    open_orders: Gauge,
    position_current: Gauge,
    pnl_realized: Gauge,
    pnl_unrealized: Gauge,
    pnl_total: Gauge,
    spread_current: Gauge,

    // Histograms
    order_latency: Histogram,
    fill_latency: Histogram,
    spread_histogram: Histogram,
}

impl PrometheusMetrics {
    /// Creates a new Prometheus metrics registry.
    ///
    /// # Arguments
    ///
    /// * `namespace` - Prefix for all metric names (e.g., "marketmaker")
    ///
    /// # Errors
    ///
    /// Returns an error if metric registration fails.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use market_maker_rs::analytics::prometheus_export::PrometheusMetrics;
    ///
    /// let metrics = PrometheusMetrics::new("marketmaker")?;
    /// ```
    pub fn new(namespace: &str) -> Result<Self, prometheus::Error> {
        let registry = Registry::new();

        // Counters
        let quotes_total = Counter::with_opts(
            Opts::new("quotes_total", "Total number of quotes generated")
                .namespace(namespace)
                .subsystem("quotes"),
        )?;

        let orders_submitted_total = Counter::with_opts(
            Opts::new("submitted_total", "Total number of orders submitted")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        let orders_filled_total = Counter::with_opts(
            Opts::new("filled_total", "Total number of orders filled")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        let orders_cancelled_total = Counter::with_opts(
            Opts::new("cancelled_total", "Total number of orders cancelled")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        let orders_rejected_total = Counter::with_opts(
            Opts::new("rejected_total", "Total number of orders rejected")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        let partial_fills_total = Counter::with_opts(
            Opts::new("partial_fills_total", "Total number of partial fills")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        // Gauges
        let open_orders = Gauge::with_opts(
            Opts::new("open_orders", "Current number of open orders")
                .namespace(namespace)
                .subsystem("orders"),
        )?;

        let position_current = Gauge::with_opts(
            Opts::new("current", "Current position size")
                .namespace(namespace)
                .subsystem("position"),
        )?;

        let pnl_realized = Gauge::with_opts(
            Opts::new("realized", "Realized PnL")
                .namespace(namespace)
                .subsystem("pnl"),
        )?;

        let pnl_unrealized = Gauge::with_opts(
            Opts::new("unrealized", "Unrealized PnL")
                .namespace(namespace)
                .subsystem("pnl"),
        )?;

        let pnl_total = Gauge::with_opts(
            Opts::new("total", "Total PnL (realized + unrealized)")
                .namespace(namespace)
                .subsystem("pnl"),
        )?;

        let spread_current = Gauge::with_opts(
            Opts::new("current_bps", "Current spread in basis points")
                .namespace(namespace)
                .subsystem("spread"),
        )?;

        // Histograms
        let order_latency = Histogram::with_opts(
            HistogramOpts::new(
                "order_milliseconds",
                "Order submission latency in milliseconds",
            )
            .namespace(namespace)
            .subsystem("latency")
            .buckets(LATENCY_BUCKETS.to_vec()),
        )?;

        let fill_latency = Histogram::with_opts(
            HistogramOpts::new(
                "fill_milliseconds",
                "Fill notification latency in milliseconds",
            )
            .namespace(namespace)
            .subsystem("latency")
            .buckets(LATENCY_BUCKETS.to_vec()),
        )?;

        let spread_histogram = Histogram::with_opts(
            HistogramOpts::new("distribution_bps", "Spread distribution in basis points")
                .namespace(namespace)
                .subsystem("spread")
                .buckets(SPREAD_BUCKETS.to_vec()),
        )?;

        // Register all metrics
        registry.register(Box::new(quotes_total.clone()))?;
        registry.register(Box::new(orders_submitted_total.clone()))?;
        registry.register(Box::new(orders_filled_total.clone()))?;
        registry.register(Box::new(orders_cancelled_total.clone()))?;
        registry.register(Box::new(orders_rejected_total.clone()))?;
        registry.register(Box::new(partial_fills_total.clone()))?;
        registry.register(Box::new(open_orders.clone()))?;
        registry.register(Box::new(position_current.clone()))?;
        registry.register(Box::new(pnl_realized.clone()))?;
        registry.register(Box::new(pnl_unrealized.clone()))?;
        registry.register(Box::new(pnl_total.clone()))?;
        registry.register(Box::new(spread_current.clone()))?;
        registry.register(Box::new(order_latency.clone()))?;
        registry.register(Box::new(fill_latency.clone()))?;
        registry.register(Box::new(spread_histogram.clone()))?;

        Ok(Self {
            registry,
            quotes_total,
            orders_submitted_total,
            orders_filled_total,
            orders_cancelled_total,
            orders_rejected_total,
            partial_fills_total,
            open_orders,
            position_current,
            pnl_realized,
            pnl_unrealized,
            pnl_total,
            spread_current,
            order_latency,
            fill_latency,
            spread_histogram,
        })
    }

    // Counter increments

    /// Increments the quotes counter.
    pub fn inc_quotes(&self) {
        self.quotes_total.inc();
    }

    /// Increments the quotes counter by a specific amount.
    pub fn inc_quotes_by(&self, count: f64) {
        self.quotes_total.inc_by(count);
    }

    /// Increments the orders submitted counter.
    pub fn inc_orders_submitted(&self) {
        self.orders_submitted_total.inc();
    }

    /// Increments the orders filled counter.
    pub fn inc_orders_filled(&self) {
        self.orders_filled_total.inc();
    }

    /// Increments the orders cancelled counter.
    pub fn inc_orders_cancelled(&self) {
        self.orders_cancelled_total.inc();
    }

    /// Increments the orders rejected counter.
    pub fn inc_orders_rejected(&self) {
        self.orders_rejected_total.inc();
    }

    /// Increments the partial fills counter.
    pub fn inc_partial_fills(&self) {
        self.partial_fills_total.inc();
    }

    // Gauge updates

    /// Sets the current number of open orders.
    pub fn set_open_orders(&self, count: f64) {
        self.open_orders.set(count);
    }

    /// Sets the current position size.
    pub fn set_position(&self, position: f64) {
        self.position_current.set(position);
    }

    /// Sets the PnL values.
    ///
    /// # Arguments
    ///
    /// * `realized` - Realized PnL
    /// * `unrealized` - Unrealized PnL
    pub fn set_pnl(&self, realized: f64, unrealized: f64) {
        self.pnl_realized.set(realized);
        self.pnl_unrealized.set(unrealized);
        self.pnl_total.set(realized + unrealized);
    }

    /// Sets the current spread in basis points.
    pub fn set_spread(&self, spread_bps: f64) {
        self.spread_current.set(spread_bps);
    }

    // Histogram observations

    /// Records an order latency observation.
    ///
    /// # Arguments
    ///
    /// * `latency_ms` - Latency in milliseconds
    pub fn observe_order_latency(&self, latency_ms: f64) {
        self.order_latency.observe(latency_ms);
    }

    /// Records a fill latency observation.
    ///
    /// # Arguments
    ///
    /// * `latency_ms` - Latency in milliseconds
    pub fn observe_fill_latency(&self, latency_ms: f64) {
        self.fill_latency.observe(latency_ms);
    }

    /// Records a spread observation.
    ///
    /// # Arguments
    ///
    /// * `spread_bps` - Spread in basis points
    pub fn observe_spread(&self, spread_bps: f64) {
        self.spread_histogram.observe(spread_bps);
    }

    /// Returns a reference to the underlying registry.
    #[must_use]
    pub fn registry(&self) -> &Registry {
        &self.registry
    }

    /// Encodes all metrics to Prometheus text format.
    ///
    /// # Errors
    ///
    /// Returns an error if encoding fails.
    pub fn encode(&self) -> Result<String, prometheus::Error> {
        let encoder = TextEncoder::new();
        let metric_families = self.registry.gather();
        let mut buffer = Vec::new();
        encoder.encode(&metric_families, &mut buffer)?;
        Ok(String::from_utf8(buffer).unwrap_or_default())
    }

    /// Returns the current value of the quotes counter.
    #[must_use]
    pub fn get_quotes_total(&self) -> f64 {
        self.quotes_total.get()
    }

    /// Returns the current value of the orders submitted counter.
    #[must_use]
    pub fn get_orders_submitted_total(&self) -> f64 {
        self.orders_submitted_total.get()
    }

    /// Returns the current value of the orders filled counter.
    #[must_use]
    pub fn get_orders_filled_total(&self) -> f64 {
        self.orders_filled_total.get()
    }

    /// Returns the current value of the orders cancelled counter.
    #[must_use]
    pub fn get_orders_cancelled_total(&self) -> f64 {
        self.orders_cancelled_total.get()
    }

    /// Returns the current value of the orders rejected counter.
    #[must_use]
    pub fn get_orders_rejected_total(&self) -> f64 {
        self.orders_rejected_total.get()
    }

    /// Returns the current value of the partial fills counter.
    #[must_use]
    pub fn get_partial_fills_total(&self) -> f64 {
        self.partial_fills_total.get()
    }

    /// Returns the current number of open orders.
    #[must_use]
    pub fn get_open_orders(&self) -> f64 {
        self.open_orders.get()
    }

    /// Returns the current position.
    #[must_use]
    pub fn get_position(&self) -> f64 {
        self.position_current.get()
    }

    /// Returns the realized PnL.
    #[must_use]
    pub fn get_pnl_realized(&self) -> f64 {
        self.pnl_realized.get()
    }

    /// Returns the unrealized PnL.
    #[must_use]
    pub fn get_pnl_unrealized(&self) -> f64 {
        self.pnl_unrealized.get()
    }

    /// Returns the total PnL.
    #[must_use]
    pub fn get_pnl_total(&self) -> f64 {
        self.pnl_total.get()
    }

    /// Returns the current spread in basis points.
    #[must_use]
    pub fn get_spread(&self) -> f64 {
        self.spread_current.get()
    }
}

/// HTTP server for exposing Prometheus metrics endpoint.
///
/// Serves metrics at the `/metrics` endpoint in Prometheus text format.
///
/// # Example
///
/// ```rust,ignore
/// use market_maker_rs::analytics::prometheus_export::{PrometheusMetrics, MetricsServer};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let metrics = Arc::new(PrometheusMetrics::new("marketmaker")?);
///     let server = MetricsServer::new(Arc::clone(&metrics), "0.0.0.0:9090");
///     
///     // Run server (blocking)
///     server.run().await?;
///     Ok(())
/// }
/// ```
pub struct MetricsServer {
    metrics: Arc<PrometheusMetrics>,
    bind_address: String,
}

impl MetricsServer {
    /// Creates a new metrics server.
    ///
    /// # Arguments
    ///
    /// * `metrics` - Shared reference to the metrics registry
    /// * `bind_address` - Address to bind the HTTP server (e.g., "0.0.0.0:9090")
    #[must_use]
    pub fn new(metrics: Arc<PrometheusMetrics>, bind_address: &str) -> Self {
        Self {
            metrics,
            bind_address: bind_address.to_string(),
        }
    }

    /// Runs the HTTP server (blocking).
    ///
    /// # Errors
    ///
    /// Returns an error if the server fails to start or encounters a runtime error.
    pub async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let addr: SocketAddr = self.bind_address.parse()?;
        let listener = TcpListener::bind(addr).await?;

        loop {
            let (stream, _) = listener.accept().await?;
            let io = TokioIo::new(stream);
            let metrics = Arc::clone(&self.metrics);

            tokio::spawn(async move {
                let service = service_fn(move |req| {
                    let metrics = Arc::clone(&metrics);
                    async move { handle_request(req, metrics).await }
                });

                if let Err(err) = http1::Builder::new().serve_connection(io, service).await {
                    eprintln!("Error serving connection: {:?}", err);
                }
            });
        }
    }

    /// Spawns the HTTP server in a background task.
    ///
    /// Returns a join handle that can be used to await server completion.
    #[must_use]
    pub fn spawn(self) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            if let Err(e) = self.run().await {
                eprintln!("Metrics server error: {}", e);
            }
        })
    }

    /// Returns the bind address.
    #[must_use]
    pub fn bind_address(&self) -> &str {
        &self.bind_address
    }
}

/// Handles HTTP requests to the metrics server.
async fn handle_request(
    req: Request<hyper::body::Incoming>,
    metrics: Arc<PrometheusMetrics>,
) -> Result<Response<Full<Bytes>>, Infallible> {
    let response = match req.uri().path() {
        "/metrics" => match metrics.encode() {
            Ok(body) => Response::builder()
                .status(StatusCode::OK)
                .header("Content-Type", "text/plain; charset=utf-8")
                .body(Full::new(Bytes::from(body)))
                .unwrap_or_else(|_| {
                    Response::builder()
                        .status(StatusCode::INTERNAL_SERVER_ERROR)
                        .body(Full::new(Bytes::from("Failed to build response")))
                        .unwrap()
                }),
            Err(e) => Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(Full::new(Bytes::from(format!(
                    "Error encoding metrics: {}",
                    e
                ))))
                .unwrap(),
        },
        "/health" => Response::builder()
            .status(StatusCode::OK)
            .body(Full::new(Bytes::from("OK")))
            .unwrap(),
        "/" => Response::builder()
            .status(StatusCode::OK)
            .header("Content-Type", "text/html")
            .body(Full::new(Bytes::from(
                r#"<html>
<head><title>Market Maker Metrics</title></head>
<body>
<h1>Market Maker Metrics</h1>
<p><a href="/metrics">Metrics</a></p>
<p><a href="/health">Health</a></p>
</body>
</html>"#,
            )))
            .unwrap(),
        _ => Response::builder()
            .status(StatusCode::NOT_FOUND)
            .body(Full::new(Bytes::from("Not Found")))
            .unwrap(),
    };

    Ok(response)
}

/// Bridge adapter to sync `LiveMetrics` with `PrometheusMetrics`.
///
/// This adapter allows you to periodically sync the internal `LiveMetrics`
/// counters with Prometheus metrics for export.
///
/// # Example
///
/// ```rust,ignore
/// use market_maker_rs::analytics::{LiveMetrics, prometheus_export::{PrometheusMetrics, MetricsBridge}};
/// use std::sync::Arc;
///
/// let live_metrics = Arc::new(LiveMetrics::new(0));
/// let prom_metrics = Arc::new(PrometheusMetrics::new("marketmaker")?);
/// let bridge = MetricsBridge::new(Arc::clone(&live_metrics), Arc::clone(&prom_metrics));
///
/// // Sync metrics periodically
/// bridge.sync();
/// ```
pub struct MetricsBridge {
    live_metrics: Arc<LiveMetrics>,
    prom_metrics: Arc<PrometheusMetrics>,
}

impl MetricsBridge {
    /// Creates a new metrics bridge.
    ///
    /// # Arguments
    ///
    /// * `live_metrics` - Reference to the live metrics tracker
    /// * `prom_metrics` - Reference to the Prometheus metrics registry
    #[must_use]
    pub fn new(live_metrics: Arc<LiveMetrics>, prom_metrics: Arc<PrometheusMetrics>) -> Self {
        Self {
            live_metrics,
            prom_metrics,
        }
    }

    /// Syncs current values from `LiveMetrics` to `PrometheusMetrics`.
    ///
    /// Call this periodically to update Prometheus metrics with the latest values.
    pub fn sync(&self) {
        let snapshot = self.live_metrics.snapshot(0);

        // Sync counters (Prometheus counters can only increase, so we set to current total)
        // Note: This works because we're setting absolute values, not incrementing
        let quotes_diff = snapshot.quotes_generated as f64 - self.prom_metrics.get_quotes_total();
        if quotes_diff > 0.0 {
            self.prom_metrics.inc_quotes_by(quotes_diff);
        }

        // Sync gauges (these can be set directly)
        self.prom_metrics
            .set_open_orders(snapshot.open_orders as f64);
        self.prom_metrics
            .set_position(snapshot.current_position.to_string().parse().unwrap_or(0.0));
        self.prom_metrics.set_pnl(
            snapshot.realized_pnl.to_string().parse().unwrap_or(0.0),
            snapshot.unrealized_pnl.to_string().parse().unwrap_or(0.0),
        );
    }

    /// Returns a reference to the live metrics.
    #[must_use]
    pub fn live_metrics(&self) -> &Arc<LiveMetrics> {
        &self.live_metrics
    }

    /// Returns a reference to the Prometheus metrics.
    #[must_use]
    pub fn prom_metrics(&self) -> &Arc<PrometheusMetrics> {
        &self.prom_metrics
    }
}

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

    #[test]
    fn test_prometheus_metrics_new() {
        let metrics = PrometheusMetrics::new("test").unwrap();
        assert_eq!(metrics.get_quotes_total(), 0.0);
        assert_eq!(metrics.get_orders_submitted_total(), 0.0);
    }

    #[test]
    fn test_counter_increments() {
        let metrics = PrometheusMetrics::new("test").unwrap();

        metrics.inc_quotes();
        metrics.inc_quotes();
        assert_eq!(metrics.get_quotes_total(), 2.0);

        metrics.inc_orders_submitted();
        assert_eq!(metrics.get_orders_submitted_total(), 1.0);

        metrics.inc_orders_filled();
        assert_eq!(metrics.get_orders_filled_total(), 1.0);

        metrics.inc_orders_cancelled();
        assert_eq!(metrics.get_orders_cancelled_total(), 1.0);

        metrics.inc_orders_rejected();
        assert_eq!(metrics.get_orders_rejected_total(), 1.0);

        metrics.inc_partial_fills();
        assert_eq!(metrics.get_partial_fills_total(), 1.0);
    }

    #[test]
    fn test_gauge_updates() {
        let metrics = PrometheusMetrics::new("test").unwrap();

        metrics.set_open_orders(5.0);
        assert_eq!(metrics.get_open_orders(), 5.0);

        metrics.set_position(100.5);
        assert_eq!(metrics.get_position(), 100.5);

        metrics.set_pnl(1000.0, 500.0);
        assert_eq!(metrics.get_pnl_realized(), 1000.0);
        assert_eq!(metrics.get_pnl_unrealized(), 500.0);
        assert_eq!(metrics.get_pnl_total(), 1500.0);

        metrics.set_spread(10.5);
        assert_eq!(metrics.get_spread(), 10.5);
    }

    #[test]
    fn test_histogram_observations() {
        let metrics = PrometheusMetrics::new("test").unwrap();

        // These should not panic
        metrics.observe_order_latency(5.0);
        metrics.observe_fill_latency(10.0);
        metrics.observe_spread(15.0);
    }

    #[test]
    fn test_encode() {
        let metrics = PrometheusMetrics::new("test").unwrap();
        metrics.inc_quotes();
        metrics.set_position(100.0);

        let encoded = metrics.encode().unwrap();
        assert!(encoded.contains("test_quotes_quotes_total"));
        assert!(encoded.contains("test_position_current"));
    }

    #[test]
    fn test_metrics_server_new() {
        let metrics = Arc::new(PrometheusMetrics::new("test").unwrap());
        let server = MetricsServer::new(Arc::clone(&metrics), "127.0.0.1:9090");
        assert_eq!(server.bind_address(), "127.0.0.1:9090");
    }

    #[test]
    fn test_metrics_bridge() {
        let live_metrics = Arc::new(LiveMetrics::new(0));
        let prom_metrics = Arc::new(PrometheusMetrics::new("test").unwrap());
        let bridge = MetricsBridge::new(Arc::clone(&live_metrics), Arc::clone(&prom_metrics));

        // Record some activity
        live_metrics.record_quote(1);
        live_metrics.record_quote(2);
        live_metrics.update_position(crate::dec!(50.0));
        live_metrics.update_pnl(crate::dec!(100.0), crate::dec!(25.0));

        // Sync
        bridge.sync();

        // Verify Prometheus metrics updated
        assert_eq!(prom_metrics.get_quotes_total(), 2.0);
        assert_eq!(prom_metrics.get_position(), 50.0);
        assert_eq!(prom_metrics.get_pnl_realized(), 100.0);
        assert_eq!(prom_metrics.get_pnl_unrealized(), 25.0);
    }

    #[test]
    fn test_registry_access() {
        let metrics = PrometheusMetrics::new("test").unwrap();
        let registry = metrics.registry();
        let families = registry.gather();
        assert!(!families.is_empty());
    }

    #[tokio::test]
    async fn test_handle_request_metrics() {
        let metrics = Arc::new(PrometheusMetrics::new("test").unwrap());
        metrics.inc_quotes();

        // Create a mock request - we can't easily test this without a full HTTP setup
        // but we can verify the metrics encode correctly
        let encoded = metrics.encode().unwrap();
        assert!(encoded.contains("test_quotes_quotes_total 1"));
    }

    #[tokio::test]
    async fn test_metrics_server_spawn() {
        let metrics = Arc::new(PrometheusMetrics::new("test").unwrap());
        // Use port 0 to let OS assign an available port
        let server = MetricsServer::new(Arc::clone(&metrics), "127.0.0.1:0");

        // Just verify spawn doesn't panic - we can't easily test the actual server
        // without more complex setup
        assert_eq!(server.bind_address(), "127.0.0.1:0");
    }
}