scouter_settings/
database.rs1use 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}
19
20impl Default for DatabaseSettings {
21 fn default() -> Self {
22 let connection_uri = std::env::var("DATABASE_URI")
23 .unwrap_or("postgresql://postgres:postgres@localhost:5432/postgres".to_string());
24
25 let max_connections = std::env::var("MAX_POOL_SIZE")
26 .unwrap_or_else(|_| "200".to_string())
27 .parse::<u32>()
28 .unwrap();
29
30 let min_connections = std::env::var("MIN_POOL_SIZE")
31 .unwrap_or_else(|_| "20".to_string())
32 .parse::<u32>()
33 .unwrap();
34
35 let db_acquire_timeout_seconds = std::env::var("DB_ACQUIRE_TIMEOUT_SECONDS")
36 .unwrap_or_else(|_| "10".to_string())
37 .parse::<u64>()
38 .unwrap();
39
40 let db_idle_timeout_seconds = std::env::var("DB_IDLE_TIMEOUT_SECONDS")
41 .unwrap_or_else(|_| "300".to_string())
42 .parse::<u64>()
43 .unwrap();
44
45 let db_max_lifetime_seconds = std::env::var("DB_MAX_LIFETIME_SECONDS")
46 .unwrap_or_else(|_| "1800".to_string())
47 .parse::<u64>()
48 .unwrap();
49
50 let db_test_before_acquire = std::env::var("DB_TEST_BEFORE_ACQUIRE")
51 .unwrap_or_else(|_| "true".to_string())
52 .parse::<bool>()
53 .unwrap();
54
55 let retention_period = std::env::var("DATA_RETENTION_PERIOD")
56 .unwrap_or_else(|_| "30".to_string())
57 .parse::<i32>()
58 .unwrap();
59
60 let flush_interval = std::env::var("TRACE_FLUSH_INTERVAL_SECONDS")
61 .unwrap_or_else(|_| "15".to_string())
62 .parse::<i64>()
63 .map(Duration::seconds)
64 .unwrap();
65
66 let stale_threshold = std::env::var("TRACE_STALE_THRESHOLD_SECONDS")
67 .unwrap_or_else(|_| "30".to_string())
68 .parse::<i64>()
69 .map(Duration::seconds)
70 .unwrap();
71
72 let max_cache_size = std::env::var("TRACE_CACHE_MAX_SIZE")
73 .unwrap_or_else(|_| "10000".to_string())
74 .parse::<usize>()
75 .unwrap();
76
77 let entity_cache_size = std::env::var("ENTITY_CACHE_MAX_SIZE")
78 .unwrap_or_else(|_| "1000".to_string())
79 .parse::<u64>()
80 .unwrap();
81
82 Self {
83 connection_uri,
84 max_connections,
85 min_connections,
86 db_acquire_timeout_seconds,
87 db_idle_timeout_seconds,
88 db_max_lifetime_seconds,
89 db_test_before_acquire,
90 retention_period,
91 flush_interval,
92 stale_threshold,
93 max_cache_size,
94 entity_cache_size,
95 }
96 }
97}