redup-bootstrap 0.3.1

Ядро платформы: инстанс службы, модули, жизненный цикл, здоровье и телеметрия.
Documentation
//! Настройки трассировки.

use redup_config::{Config, Report};
use redup_primitives::Address;

/// Трассировка: экспорт отрезков в коллектор.
#[derive(Debug, Clone, Default, PartialEq, Eq, Config)]
#[config(validate)]
pub struct TracingConfig {
    /// Адрес коллектора отрезков: протокол OTLP по gRPC, без шифрования. Порт укажите явно:
    /// стандартный порт OTLP — 4317. Нет значения — экспорт выключен. Для экспорта у платформы
    /// должна быть включена возможность `otlp`.
    collector: Option<Address>,
}

impl TracingConfig {
    /// Адрес коллектора отрезков, если экспорт включён.
    pub fn collector(&self) -> Option<&Address> {
        self.collector.as_ref()
    }

    fn validate(&self, report: &mut Report) {
        // Порт умолчания адреса службы — 4200; без явно заданного порта экспорт ушёл бы мимо
        // стандартного порта OTLP, а ошибка соединения проявилась бы только отсутствием отрезков.
        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}");
    }
}