rustfs_obs/telemetry/dial9/
config.rs1use super::state::dial9_runtime_state;
23use rustfs_config::{
24 DEFAULT_RUNTIME_DIAL9_ENABLED, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX, DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE,
25 DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR, DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT, ENV_RUNTIME_DIAL9_ENABLED,
26 ENV_RUNTIME_DIAL9_FILE_PREFIX, ENV_RUNTIME_DIAL9_MAX_FILE_SIZE, ENV_RUNTIME_DIAL9_OUTPUT_DIR,
27 ENV_RUNTIME_DIAL9_ROTATION_COUNT, ENV_RUNTIME_DIAL9_S3_BUCKET, ENV_RUNTIME_DIAL9_S3_PREFIX,
28};
29use rustfs_utils::{get_env_bool, get_env_opt_str, get_env_opt_u64, get_env_opt_usize, get_env_str};
30use std::path::PathBuf;
31use tracing::warn;
32
33use super::{EVENT_DIAL9_STATE, LOG_COMPONENT_OBS, LOG_SUBSYSTEM_DIAL9};
34
35pub const fn is_supported() -> bool {
41 cfg!(feature = "dial9")
42}
43
44pub fn is_configured() -> bool {
49 get_env_bool(ENV_RUNTIME_DIAL9_ENABLED, DEFAULT_RUNTIME_DIAL9_ENABLED)
50}
51
52pub fn is_enabled() -> bool {
54 is_supported() && is_configured()
55}
56
57fn sanitize_max_file_size(bytes: u64) -> u64 {
58 bytes.max(1)
59}
60
61fn sanitize_rotation_count(count: usize) -> usize {
62 count.max(1)
63}
64
65pub(super) fn total_rotation_size(max_file_size: u64, rotation_count: usize) -> u64 {
67 max_file_size.saturating_mul(u64::try_from(rotation_count).unwrap_or(u64::MAX))
68}
69
70#[derive(Debug, Clone)]
72pub struct Dial9Config {
73 pub enabled: bool,
75
76 pub output_dir: String,
78
79 pub file_prefix: String,
81
82 pub max_file_size: u64,
84
85 pub rotation_count: usize,
87}
88
89impl Default for Dial9Config {
90 fn default() -> Self {
91 Self {
92 enabled: DEFAULT_RUNTIME_DIAL9_ENABLED,
93 output_dir: DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR.to_string(),
94 file_prefix: DEFAULT_RUNTIME_DIAL9_FILE_PREFIX.to_string(),
95 max_file_size: DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE,
96 rotation_count: DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT,
97 }
98 }
99}
100
101impl Dial9Config {
102 pub fn from_env() -> Self {
105 let config = if is_enabled() {
106 Self::from_env_enabled()
107 } else {
108 Self::default()
109 };
110 dial9_runtime_state().record_config(&config);
111 config
112 }
113
114 fn from_env_enabled() -> Self {
117 let raw_max_file_size = get_env_opt_u64(ENV_RUNTIME_DIAL9_MAX_FILE_SIZE);
118 let raw_rotation_count = get_env_opt_usize(ENV_RUNTIME_DIAL9_ROTATION_COUNT);
119
120 if raw_max_file_size == Some(0) {
121 warn!(
122 event = EVENT_DIAL9_STATE,
123 component = LOG_COMPONENT_OBS,
124 subsystem = LOG_SUBSYSTEM_DIAL9,
125 result = "invalid_max_file_size",
126 fallback_bytes = 1_u64,
127 "dial9 state changed"
128 );
129 }
130 if raw_rotation_count == Some(0) {
131 warn!(
132 event = EVENT_DIAL9_STATE,
133 component = LOG_COMPONENT_OBS,
134 subsystem = LOG_SUBSYSTEM_DIAL9,
135 result = "invalid_rotation_count",
136 fallback_count = 1_usize,
137 "dial9 state changed"
138 );
139 }
140
141 warn_unusable_s3_upload(
144 get_env_opt_str(ENV_RUNTIME_DIAL9_S3_BUCKET)
145 .filter(|s| !s.is_empty())
146 .as_deref(),
147 get_env_opt_str(ENV_RUNTIME_DIAL9_S3_PREFIX)
148 .filter(|s| !s.is_empty())
149 .as_deref(),
150 );
151
152 Self {
153 enabled: true,
154 output_dir: get_env_str(ENV_RUNTIME_DIAL9_OUTPUT_DIR, DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR),
155 file_prefix: get_env_str(ENV_RUNTIME_DIAL9_FILE_PREFIX, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX),
156 max_file_size: sanitize_max_file_size(raw_max_file_size.unwrap_or(DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE)),
157 rotation_count: sanitize_rotation_count(raw_rotation_count.unwrap_or(DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT)),
158 }
159 }
160
161 pub fn base_path(&self) -> PathBuf {
163 PathBuf::from(&self.output_dir).join(&self.file_prefix)
164 }
165
166 pub fn total_disk_budget(&self) -> u64 {
168 total_rotation_size(self.max_file_size, self.rotation_count)
169 }
170}
171
172fn warn_unusable_s3_upload(s3_bucket: Option<&str>, s3_prefix: Option<&str>) {
182 if s3_bucket.is_none() && s3_prefix.is_none() {
183 return;
184 }
185
186 warn!(
187 event = EVENT_DIAL9_STATE,
188 component = LOG_COMPONENT_OBS,
189 subsystem = LOG_SUBSYSTEM_DIAL9,
190 result = "s3_upload_unsupported",
191 s3_bucket = s3_bucket.unwrap_or(""),
192 s3_prefix = s3_prefix.unwrap_or(""),
193 reason = "dial9 worker-s3 depends on a vulnerable rustls-webpki (RUSTSEC-2026-0098/0099/0104)",
194 remedy = "collect trace segments from the output directory instead",
195 "dial9 state changed"
196 );
197}
198
199#[cfg(test)]
200mod tests {
201 use super::*;
202
203 #[test]
204 fn sanitizers_reject_zero_values() {
205 assert_eq!(sanitize_max_file_size(0), 1);
206 assert_eq!(sanitize_rotation_count(0), 1);
207 }
208
209 #[test]
210 fn total_rotation_size_saturates() {
211 assert_eq!(total_rotation_size(u64::MAX, 2), u64::MAX);
212 }
213
214 #[test]
215 fn default_config_is_disabled() {
216 let config = Dial9Config::default();
217 assert!(!config.enabled);
218 assert_eq!(config.output_dir, DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR);
219 assert_eq!(config.file_prefix, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX);
220 assert_eq!(config.max_file_size, DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE);
221 assert_eq!(config.rotation_count, DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT);
222 }
223
224 #[test]
225 fn base_path_joins_prefix() {
226 let config = Dial9Config {
227 output_dir: "/tmp/telemetry".to_string(),
228 file_prefix: "rustfs".to_string(),
229 ..Default::default()
230 };
231 assert_eq!(config.base_path(), PathBuf::from("/tmp/telemetry/rustfs"));
232 }
233
234 #[test]
235 fn total_disk_budget_multiplies_file_size_by_rotation_count() {
236 let config = Dial9Config {
237 max_file_size: 100,
238 rotation_count: 10,
239 ..Default::default()
240 };
241 assert_eq!(config.total_disk_budget(), 1_000);
242 }
243
244 #[test]
245 fn is_enabled_requires_compile_time_support() {
246 if !is_supported() {
249 assert!(!is_enabled());
250 }
251 }
252}