use redup_config::{Config, Report};
use redup_primitives::Address;
#[derive(Debug, Clone, Default, PartialEq, Eq, Config)]
#[config(validate)]
pub struct TracingConfig {
collector: Option<Address>,
}
impl TracingConfig {
pub fn collector(&self) -> Option<&Address> {
self.collector.as_ref()
}
fn validate(&self, report: &mut Report) {
if self
.collector
.as_ref()
.is_some_and(|address| address.port() == SERVICE_PORT)
{
report.error(
"collector",
"The collector port must be set explicitly; the standard OTLP port is 4317.",
);
}
}
}
const SERVICE_PORT: u16 = 4200;
#[cfg(test)]
mod tests {
use redup_config::{Source, load};
use super::TracingConfig;
#[test]
fn export_is_off_without_a_section() {
assert_eq!(TracingConfig::default().collector(), None);
}
#[test]
fn collector_is_read_from_the_common_file() {
let directory = tempfile::tempdir().expect("a temporary directory");
std::fs::write(
directory.path().join("config.yaml"),
"tracing:\n collector: collector:4317\n",
)
.expect("the configuration file");
let loaded = load::<TracingConfig>(&Source::directory(directory.path()))
.expect("the configuration is accepted");
assert_eq!(
loaded.value.collector().map(|address| address.to_string()),
Some("collector:4317".to_owned())
);
}
#[test]
fn collector_without_a_port_is_rejected() {
let directory = tempfile::tempdir().expect("a temporary directory");
std::fs::write(
directory.path().join("config.yaml"),
"tracing:\n collector: collector\n",
)
.expect("the configuration file");
let problems = load::<TracingConfig>(&Source::directory(directory.path()))
.expect_err("the configuration is rejected");
let text = problems.to_string();
assert!(text.contains("4317"), "{text}");
}
}