use super::state::dial9_runtime_state;
use rustfs_config::{
DEFAULT_RUNTIME_DIAL9_ENABLED, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX, DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE,
DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR, DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT, ENV_RUNTIME_DIAL9_ENABLED,
ENV_RUNTIME_DIAL9_FILE_PREFIX, ENV_RUNTIME_DIAL9_MAX_FILE_SIZE, ENV_RUNTIME_DIAL9_OUTPUT_DIR,
ENV_RUNTIME_DIAL9_ROTATION_COUNT, ENV_RUNTIME_DIAL9_S3_BUCKET, ENV_RUNTIME_DIAL9_S3_PREFIX,
};
use rustfs_utils::{get_env_bool, get_env_opt_str, get_env_opt_u64, get_env_opt_usize, get_env_str};
use std::path::PathBuf;
use tracing::warn;
use super::{EVENT_DIAL9_STATE, LOG_COMPONENT_OBS, LOG_SUBSYSTEM_DIAL9};
pub const fn is_supported() -> bool {
cfg!(feature = "dial9")
}
pub fn is_configured() -> bool {
get_env_bool(ENV_RUNTIME_DIAL9_ENABLED, DEFAULT_RUNTIME_DIAL9_ENABLED)
}
pub fn is_enabled() -> bool {
is_supported() && is_configured()
}
fn sanitize_max_file_size(bytes: u64) -> u64 {
bytes.max(1)
}
fn sanitize_rotation_count(count: usize) -> usize {
count.max(1)
}
pub(super) fn total_rotation_size(max_file_size: u64, rotation_count: usize) -> u64 {
max_file_size.saturating_mul(u64::try_from(rotation_count).unwrap_or(u64::MAX))
}
#[derive(Debug, Clone)]
pub struct Dial9Config {
pub enabled: bool,
pub output_dir: String,
pub file_prefix: String,
pub max_file_size: u64,
pub rotation_count: usize,
}
impl Default for Dial9Config {
fn default() -> Self {
Self {
enabled: DEFAULT_RUNTIME_DIAL9_ENABLED,
output_dir: DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR.to_string(),
file_prefix: DEFAULT_RUNTIME_DIAL9_FILE_PREFIX.to_string(),
max_file_size: DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE,
rotation_count: DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT,
}
}
}
impl Dial9Config {
pub fn from_env() -> Self {
let config = if is_enabled() {
Self::from_env_enabled()
} else {
Self::default()
};
dial9_runtime_state().record_config(&config);
config
}
fn from_env_enabled() -> Self {
let raw_max_file_size = get_env_opt_u64(ENV_RUNTIME_DIAL9_MAX_FILE_SIZE);
let raw_rotation_count = get_env_opt_usize(ENV_RUNTIME_DIAL9_ROTATION_COUNT);
if raw_max_file_size == Some(0) {
warn!(
event = EVENT_DIAL9_STATE,
component = LOG_COMPONENT_OBS,
subsystem = LOG_SUBSYSTEM_DIAL9,
result = "invalid_max_file_size",
fallback_bytes = 1_u64,
"dial9 state changed"
);
}
if raw_rotation_count == Some(0) {
warn!(
event = EVENT_DIAL9_STATE,
component = LOG_COMPONENT_OBS,
subsystem = LOG_SUBSYSTEM_DIAL9,
result = "invalid_rotation_count",
fallback_count = 1_usize,
"dial9 state changed"
);
}
warn_unusable_s3_upload(
get_env_opt_str(ENV_RUNTIME_DIAL9_S3_BUCKET)
.filter(|s| !s.is_empty())
.as_deref(),
get_env_opt_str(ENV_RUNTIME_DIAL9_S3_PREFIX)
.filter(|s| !s.is_empty())
.as_deref(),
);
Self {
enabled: true,
output_dir: get_env_str(ENV_RUNTIME_DIAL9_OUTPUT_DIR, DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR),
file_prefix: get_env_str(ENV_RUNTIME_DIAL9_FILE_PREFIX, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX),
max_file_size: sanitize_max_file_size(raw_max_file_size.unwrap_or(DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE)),
rotation_count: sanitize_rotation_count(raw_rotation_count.unwrap_or(DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT)),
}
}
pub fn base_path(&self) -> PathBuf {
PathBuf::from(&self.output_dir).join(&self.file_prefix)
}
pub fn total_disk_budget(&self) -> u64 {
total_rotation_size(self.max_file_size, self.rotation_count)
}
}
fn warn_unusable_s3_upload(s3_bucket: Option<&str>, s3_prefix: Option<&str>) {
if s3_bucket.is_none() && s3_prefix.is_none() {
return;
}
warn!(
event = EVENT_DIAL9_STATE,
component = LOG_COMPONENT_OBS,
subsystem = LOG_SUBSYSTEM_DIAL9,
result = "s3_upload_unsupported",
s3_bucket = s3_bucket.unwrap_or(""),
s3_prefix = s3_prefix.unwrap_or(""),
reason = "dial9 worker-s3 depends on a vulnerable rustls-webpki (RUSTSEC-2026-0098/0099/0104)",
remedy = "collect trace segments from the output directory instead",
"dial9 state changed"
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanitizers_reject_zero_values() {
assert_eq!(sanitize_max_file_size(0), 1);
assert_eq!(sanitize_rotation_count(0), 1);
}
#[test]
fn total_rotation_size_saturates() {
assert_eq!(total_rotation_size(u64::MAX, 2), u64::MAX);
}
#[test]
fn default_config_is_disabled() {
let config = Dial9Config::default();
assert!(!config.enabled);
assert_eq!(config.output_dir, DEFAULT_RUNTIME_DIAL9_OUTPUT_DIR);
assert_eq!(config.file_prefix, DEFAULT_RUNTIME_DIAL9_FILE_PREFIX);
assert_eq!(config.max_file_size, DEFAULT_RUNTIME_DIAL9_MAX_FILE_SIZE);
assert_eq!(config.rotation_count, DEFAULT_RUNTIME_DIAL9_ROTATION_COUNT);
}
#[test]
fn base_path_joins_prefix() {
let config = Dial9Config {
output_dir: "/tmp/telemetry".to_string(),
file_prefix: "rustfs".to_string(),
..Default::default()
};
assert_eq!(config.base_path(), PathBuf::from("/tmp/telemetry/rustfs"));
}
#[test]
fn total_disk_budget_multiplies_file_size_by_rotation_count() {
let config = Dial9Config {
max_file_size: 100,
rotation_count: 10,
..Default::default()
};
assert_eq!(config.total_disk_budget(), 1_000);
}
#[test]
fn is_enabled_requires_compile_time_support() {
if !is_supported() {
assert!(!is_enabled());
}
}
}