Skip to main content

fraiseql_server/config/
metrics.rs

1//! Metrics configuration with SLO tracking.
2
3use serde::Deserialize;
4
5/// Prometheus metrics endpoint and SLO tracking configuration.
6#[derive(Debug, Clone, Deserialize)]
7pub struct MetricsConfig {
8    /// Whether to expose the metrics endpoint.  Default: `true`.
9    #[serde(default = "default_enabled")]
10    pub enabled: bool,
11
12    /// URL path for the metrics endpoint.  Default: `"/metrics"`.
13    #[serde(default = "default_path")]
14    pub path: String,
15
16    /// Output format (`"prometheus"` is the only currently supported value).  Default:
17    /// `"prometheus"`.
18    #[serde(default = "default_format")]
19    pub format: String,
20
21    /// SLO configuration
22    #[serde(default)]
23    pub slos: SloConfig,
24}
25
26impl Default for MetricsConfig {
27    fn default() -> Self {
28        Self {
29            enabled: default_enabled(),
30            path:    default_path(),
31            format:  default_format(),
32            slos:    SloConfig::default(),
33        }
34    }
35}
36
37const fn default_enabled() -> bool {
38    true
39}
40fn default_path() -> String {
41    "/metrics".to_string()
42}
43fn default_format() -> String {
44    "prometheus".to_string()
45}
46
47/// Service Level Objective (SLO) targets for latency, availability, and error rate.
48#[derive(Debug, Clone, Deserialize, Default)]
49pub struct SloConfig {
50    /// Target latency percentiles to track
51    #[serde(default = "default_latency_percentiles")]
52    pub latency_percentiles: Vec<f64>,
53
54    /// Latency SLO targets (p99 < Xms)
55    #[serde(default)]
56    pub latency_targets: LatencyTargets,
57
58    /// Availability SLO target (e.g., 0.999 for 99.9%)
59    #[serde(default = "default_availability_target")]
60    pub availability_target: f64,
61
62    /// Error rate SLO target (e.g., 0.01 for 1%)
63    #[serde(default = "default_error_rate_target")]
64    pub error_rate_target: f64,
65}
66
67fn default_latency_percentiles() -> Vec<f64> {
68    vec![0.5, 0.9, 0.95, 0.99]
69}
70const fn default_availability_target() -> f64 {
71    0.999
72}
73const fn default_error_rate_target() -> f64 {
74    0.01
75}
76
77/// SLO latency targets in milliseconds (p99) per operation type.
78#[derive(Debug, Clone, Deserialize, Default)]
79pub struct LatencyTargets {
80    /// GraphQL query p99 latency target in milliseconds.  Default: `100`.
81    #[serde(default = "default_graphql_latency")]
82    pub graphql_p99_ms: u64,
83
84    /// Webhook handler p99 latency target in milliseconds.  Default: `500`.
85    #[serde(default = "default_webhook_latency")]
86    pub webhook_p99_ms: u64,
87
88    /// Authentication endpoint p99 latency target in milliseconds.  Default: `10`.
89    #[serde(default = "default_auth_latency")]
90    pub auth_p99_ms: u64,
91
92    /// File upload p99 latency target in milliseconds.  Default: `2000`.
93    #[serde(default = "default_file_upload_latency")]
94    pub file_upload_p99_ms: u64,
95}
96
97const fn default_graphql_latency() -> u64 {
98    100
99}
100const fn default_webhook_latency() -> u64 {
101    500
102}
103const fn default_auth_latency() -> u64 {
104    10
105}
106const fn default_file_upload_latency() -> u64 {
107    2000
108}