Add #![deny(clippy::unwrap_used, clippy::expect_used)] to api_gateway and backtesting_service crate roots. The ml and common crates already had these deny attributes. Production code fixes: - api_gateway: replace expect with unwrap_or for cache stats and LRU eviction - api_gateway: add #[allow] on NonZeroU32 constants and Prometheus metrics - backtesting_service: replace expect/unwrap with ok_or_else/? for OHLCV bar bounds checks and chrono timestamp resampling - backtesting_service: add #[allow] on Prometheus Lazy metric statics Test code: cfg_attr(test, allow) at crate root for both crates. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
66 lines
2.1 KiB
Rust
66 lines
2.1 KiB
Rust
//! Simple Prometheus metrics for backtesting service
|
|
//!
|
|
//! Provides basic service metrics using the global Prometheus registry
|
|
|
|
use once_cell::sync::Lazy;
|
|
use prometheus::{register_counter, register_gauge, Counter, Gauge};
|
|
|
|
// SAFETY: Prometheus metric registration panics only if duplicate names exist,
|
|
// which is a fatal misconfiguration that should crash at startup.
|
|
|
|
/// Service uptime in seconds
|
|
#[allow(clippy::expect_used)]
|
|
pub static SERVICE_UPTIME: Lazy<Gauge> = Lazy::new(|| {
|
|
register_gauge!(
|
|
"backtesting_service_uptime_seconds",
|
|
"Service uptime in seconds"
|
|
)
|
|
.expect("Failed to register SERVICE_UPTIME metric - this is a critical initialization error")
|
|
});
|
|
|
|
/// Total number of backtests started
|
|
#[allow(clippy::expect_used)]
|
|
pub static BACKTESTS_STARTED: Lazy<Counter> = Lazy::new(|| {
|
|
register_counter!(
|
|
"backtesting_backtests_started_total",
|
|
"Total number of backtests started"
|
|
)
|
|
.expect("Failed to register BACKTESTS_STARTED metric - this is a critical initialization error")
|
|
});
|
|
|
|
/// Total number of backtests completed
|
|
#[allow(clippy::expect_used)]
|
|
pub static BACKTESTS_COMPLETED: Lazy<Counter> = Lazy::new(|| {
|
|
register_counter!(
|
|
"backtesting_backtests_completed_total",
|
|
"Total number of backtests completed"
|
|
)
|
|
.expect(
|
|
"Failed to register BACKTESTS_COMPLETED metric - this is a critical initialization error",
|
|
)
|
|
});
|
|
|
|
/// Total number of backtest errors
|
|
#[allow(clippy::expect_used)]
|
|
pub static BACKTEST_ERRORS: Lazy<Counter> = Lazy::new(|| {
|
|
register_counter!(
|
|
"backtesting_errors_total",
|
|
"Total number of backtest errors"
|
|
)
|
|
.expect("Failed to register BACKTEST_ERRORS metric - this is a critical initialization error")
|
|
});
|
|
|
|
/// Initialize metrics (registers them with global registry)
|
|
pub fn init_metrics() {
|
|
// Force lazy initialization by accessing each metric
|
|
let _ = &*SERVICE_UPTIME;
|
|
let _ = &*BACKTESTS_STARTED;
|
|
let _ = &*BACKTESTS_COMPLETED;
|
|
let _ = &*BACKTEST_ERRORS;
|
|
}
|
|
|
|
/// Update service uptime metric
|
|
pub fn update_uptime(start_time: std::time::Instant) {
|
|
SERVICE_UPTIME.set(start_time.elapsed().as_secs_f64());
|
|
}
|