Files
foxhunt/examples/prometheus_integration_demo.rs
jgrusewski 6093eac7bf 🔧 Tonic 0.14 Upgrade: Auto-generated and build system changes
Wave 64-65 cleanup: Proto regeneration and build system updates from Tonic 0.12→0.14 upgrade

Files updated:
- Cargo.lock: Dependency resolution for Tonic 0.14.2
- All build.rs: Updated for tonic-prost-build
- Proto files: Regenerated with tonic-prost 0.14
- Examples/tests: Updated for new gRPC API

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-03 07:34:26 +02:00

279 lines
9.4 KiB
Rust

//! Prometheus Metrics Integration Demonstration
//!
//! This example demonstrates how to use the comprehensive Prometheus metrics
//! integration in the Foxhunt HFT trading system.
use std::time::Duration;
use tokio::time::sleep;
use tracing::{info, warn};
// Import Foxhunt modules (assuming they're accessible)
// use trading_engine::prelude::*; // REMOVED - prelude does not exist
// Prometheus metrics functions (from our implementation)
use lazy_static::lazy_static;
use prometheus::{register_counter, register_gauge, register_histogram, Counter, Gauge, Histogram};
// Example metrics (simplified versions of what we implemented)
lazy_static! {
static ref DEMO_ORDERS_COUNTER: Counter =
register_counter!("demo_orders_total", "Demo orders processed")
.expect("Failed to register demo orders counter");
static ref DEMO_LATENCY_HISTOGRAM: Histogram =
register_histogram!("demo_latency_microseconds", "Demo latency measurements")
.expect("Failed to register demo latency histogram");
static ref DEMO_PNL_GAUGE: Gauge = register_gauge!("demo_pnl_usd", "Demo P&L in USD")
.expect("Failed to register demo P&L gauge");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt::init();
info!("🚀 Starting Foxhunt Prometheus Metrics Integration Demo");
// Start metrics server in background
let metrics_server_handle = tokio::spawn(async {
info!("📊 Starting Prometheus metrics server on http://localhost:9090");
// In the real implementation, this would be:
// start_metrics_server().await.expect("Failed to start metrics server");
// For demo purposes, simulate a metrics server
loop {
sleep(Duration::from_secs(30)).await;
info!("📈 Metrics server heartbeat - serving metrics on /metrics endpoint");
}
});
// Simulate trading operations with metrics collection
let trading_simulation_handle = tokio::spawn(async {
simulate_trading_operations().await;
});
// Simulate ML operations with metrics
let ml_simulation_handle = tokio::spawn(async {
simulate_ml_operations().await;
});
// Simulate risk management with metrics
let risk_simulation_handle = tokio::spawn(async {
simulate_risk_management().await;
});
info!("🎯 All systems started. Metrics available at:");
info!(" • Main metrics: http://localhost:9090/metrics");
info!(" • Health check: http://localhost:9090/health");
info!(" • Web interface: http://localhost:9090/");
// Run for demonstration period
tokio::time::timeout(Duration::from_secs(60), async {
tokio::try_join!(
metrics_server_handle,
trading_simulation_handle,
ml_simulation_handle,
risk_simulation_handle
)
.ok();
})
.await
.ok();
info!("🏁 Demo completed. In production, metrics would be continuously collected.");
// Print final metrics summary
print_metrics_summary();
Ok(())
}
/// Simulate trading operations with comprehensive metrics collection
async fn simulate_trading_operations() {
info!("💼 Starting trading operations simulation");
let mut order_count = 0u64;
let mut total_pnl = 0.0f64;
for i in 0..20 {
let start_time = std::time::Instant::now();
// Simulate order creation and submission
let symbol = match i % 3 {
0 => "BTCUSD",
1 => "ETHUSD",
_ => "ADAUSD",
};
let price = 50000.0 + (i as f64 * 100.0);
let quantity = 0.1 + (i as f64 * 0.01);
// Record order metrics
DEMO_ORDERS_COUNTER.inc();
order_count += 1;
// Simulate order processing latency (5-50 microseconds)
let processing_latency = 5.0 + (i as f64 * 2.5);
DEMO_LATENCY_HISTOGRAM.observe(processing_latency);
// Simulate execution and P&L impact
let pnl_impact = (quantity * price * 0.001) * if i % 2 == 0 { 1.0 } else { -0.5 };
total_pnl += pnl_impact;
DEMO_PNL_GAUGE.set(total_pnl);
let elapsed = start_time.elapsed().as_micros() as f64;
info!(
"📋 Order {}: {} {:.3} {} @ ${:.2} | Latency: {:.1}μs | P&L: ${:.2}",
order_count,
if i % 2 == 0 { "BUY" } else { "SELL" },
quantity,
symbol,
price,
elapsed,
total_pnl
);
// Simulate realistic order frequency (200 orders/second peak)
sleep(Duration::from_millis(50 + (i % 5) * 10)).await;
}
info!(
"✅ Trading simulation completed: {} orders, ${:.2} P&L",
order_count, total_pnl
);
}
/// Simulate ML operations with metrics
async fn simulate_ml_operations() {
info!("🧠 Starting ML operations simulation");
// Simulate model loading
sleep(Duration::from_millis(100)).await;
info!("📥 ML models loaded (GPU: enabled)");
for i in 0..15 {
let start_time = std::time::Instant::now();
// Simulate ML inference
let symbol = match i % 4 {
0 => "BTCUSD",
1 => "ETHUSD",
2 => "BNBUSD",
_ => "SOLUSD",
};
// Simulate inference latency (10-100 microseconds)
let _inference_latency = 10.0 + (i as f64 * 5.0);
// Simulate prediction confidence (70-95%)
let confidence = 0.70 + (i as f64 * 0.015);
// Simulate model drift score (0-10%)
let drift_score = (i as f64 * 0.5) / 100.0;
let elapsed = start_time.elapsed().as_micros() as f64;
info!(
"🎯 ML Prediction {}: {} | Confidence: {:.1}% | Drift: {:.2}% | Latency: {:.1}μs",
i + 1,
symbol,
confidence * 100.0,
drift_score * 100.0,
elapsed
);
// Alert on high drift
if drift_score > 0.05 {
warn!("⚠️ High model drift detected: {:.2}%", drift_score * 100.0);
}
// Simulate ML inference frequency
sleep(Duration::from_millis(80)).await;
}
info!("✅ ML simulation completed: 15 predictions generated");
}
/// Simulate risk management operations with metrics
async fn simulate_risk_management() {
info!("🛡️ Starting risk management simulation");
let mut portfolio_value = 1_000_000.0f64; // $1M starting portfolio
let mut var_95; // VaR will be calculated in loop
let mut concentration_score = 800.0f64; // HHI score
for i in 0..12 {
let start_time = std::time::Instant::now();
// Simulate portfolio value changes
let market_movement = (i as f64 - 6.0) * 5_000.0; // +/- market movement
portfolio_value += market_movement;
// Simulate VaR calculation
var_95 = portfolio_value * 0.05 * (1.0 + (i as f64 * 0.01));
// Simulate concentration risk changes
concentration_score += (i as f64 * 25.0) - 150.0;
concentration_score = concentration_score.max(100.0).min(2000.0);
// Simulate risk calculation latency
let _risk_calc_latency = 15.0 + (i as f64 * 3.0);
let elapsed = start_time.elapsed().as_micros() as f64;
info!("⚖️ Risk Update {}: Portfolio: ${:.0} | VaR(95%): ${:.0} | HHI: {:.0} | Latency: {:.1}μs",
i + 1, portfolio_value, var_95, concentration_score, elapsed);
// Alert on concentration risk
if concentration_score > 1500.0 {
warn!(
"⚠️ High concentration risk: HHI {:.0} (limit: 1500)",
concentration_score
);
}
// Alert on large VaR
if var_95 > 75_000.0 {
warn!("⚠️ High VaR exposure: ${:.0} (limit: $75K)", var_95);
}
sleep(Duration::from_millis(200)).await;
}
info!("✅ Risk management simulation completed");
}
/// Print metrics summary (in real implementation, this would be handled by Prometheus)
fn print_metrics_summary() {
info!("\n📊 METRICS SUMMARY");
info!("═══════════════════");
// In the real implementation, these would come from the actual metrics
info!("🔢 Total Orders: {}", DEMO_ORDERS_COUNTER.get());
info!("💰 Current P&L: ${:.2}", DEMO_PNL_GAUGE.get());
info!("\n📈 Available at Prometheus endpoints:");
info!(" • foxhunt_orders_total - Total orders processed");
info!(" • foxhunt_latency_microseconds - System latency distribution");
info!(" • foxhunt_position_value_usd - Current position values");
info!(" • foxhunt_ml_predictions_total - ML predictions generated");
info!(" • foxhunt_risk_breaches_total - Risk limit breaches");
info!(" • foxhunt_concentration_risk_score - Portfolio concentration (HHI)");
info!(" • foxhunt_ml_inference_latency_microseconds - ML inference timing");
info!(" • foxhunt_throughput_ops_per_second - System throughput");
info!("\n🔗 Integration Commands:");
info!(" # Test metrics endpoint");
info!(" curl http://localhost:9090/metrics");
info!(" ");
info!(" # View in browser");
info!(" open http://localhost:9090/");
info!(" ");
info!(" # Prometheus configuration");
info!(" echo 'scrape_configs:");
info!(" - job_name: foxhunt-trading");
info!(" static_configs:");
info!(" - targets: [\"localhost:9090\"]' >> prometheus.yml");
}