Skip to main content

scouter_settings/
database.rs

1use chrono::Duration;
2use serde::Serialize;
3
4#[derive(Debug, Clone, Serialize)]
5pub struct DatabaseSettings {
6    pub connection_uri: String,
7    pub max_connections: u32,
8    pub min_connections: u32,
9    pub db_acquire_timeout_seconds: u64,
10    pub db_idle_timeout_seconds: u64,
11    pub db_max_lifetime_seconds: u64,
12    pub db_test_before_acquire: bool,
13    pub retention_period: i32,
14    pub flush_interval: Duration,
15    pub stale_threshold: Duration,
16    pub max_cache_size: usize,
17    pub entity_cache_size: u64,
18    pub trace_retention_period: i32,
19}
20
21impl Default for DatabaseSettings {
22    fn default() -> Self {
23        let connection_uri = std::env::var("DATABASE_URI")
24            .unwrap_or("postgresql://postgres:postgres@localhost:5432/postgres".to_string());
25
26        let max_connections = std::env::var("MAX_POOL_SIZE")
27            .unwrap_or_else(|_| "200".to_string())
28            .parse::<u32>()
29            .unwrap();
30
31        let min_connections = std::env::var("MIN_POOL_SIZE")
32            .unwrap_or_else(|_| "20".to_string())
33            .parse::<u32>()
34            .unwrap();
35
36        let db_acquire_timeout_seconds = std::env::var("DB_ACQUIRE_TIMEOUT_SECONDS")
37            .unwrap_or_else(|_| "10".to_string())
38            .parse::<u64>()
39            .unwrap();
40
41        let db_idle_timeout_seconds = std::env::var("DB_IDLE_TIMEOUT_SECONDS")
42            .unwrap_or_else(|_| "300".to_string())
43            .parse::<u64>()
44            .unwrap();
45
46        let db_max_lifetime_seconds = std::env::var("DB_MAX_LIFETIME_SECONDS")
47            .unwrap_or_else(|_| "1800".to_string())
48            .parse::<u64>()
49            .unwrap();
50
51        let db_test_before_acquire = std::env::var("DB_TEST_BEFORE_ACQUIRE")
52            .unwrap_or_else(|_| "true".to_string())
53            .parse::<bool>()
54            .unwrap();
55
56        let retention_period = std::env::var("DATA_RETENTION_PERIOD")
57            .unwrap_or_else(|_| "30".to_string())
58            .parse::<i32>()
59            .unwrap();
60
61        let flush_interval = std::env::var("TRACE_FLUSH_INTERVAL_SECONDS")
62            .unwrap_or_else(|_| "15".to_string())
63            .parse::<i64>()
64            .map(Duration::seconds)
65            .unwrap();
66
67        let stale_threshold = std::env::var("TRACE_STALE_THRESHOLD_SECONDS")
68            .unwrap_or_else(|_| "10".to_string())
69            .parse::<i64>()
70            .map(Duration::seconds)
71            .unwrap();
72
73        let max_cache_size = std::env::var("TRACE_CACHE_MAX_SIZE")
74            .unwrap_or_else(|_| "10000".to_string())
75            .parse::<usize>()
76            .unwrap();
77
78        let entity_cache_size = std::env::var("ENTITY_CACHE_MAX_SIZE")
79            .unwrap_or_else(|_| "1000".to_string())
80            .parse::<u64>()
81            .unwrap();
82
83        let trace_retention_period = std::env::var("TRACE_DATA_RETENTION_PERIOD")
84            .unwrap_or_else(|_| "365".to_string())
85            .parse::<i32>()
86            .unwrap();
87
88        Self {
89            connection_uri,
90            max_connections,
91            min_connections,
92            db_acquire_timeout_seconds,
93            db_idle_timeout_seconds,
94            db_max_lifetime_seconds,
95            db_test_before_acquire,
96            retention_period,
97            flush_interval,
98            stale_threshold,
99            max_cache_size,
100            entity_cache_size,
101            trace_retention_period,
102        }
103    }
104}