servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Explicit process telemetry settings; construction has no global effects.

use tracing_subscriber::filter::{LevelFilter, Targets};

use crate::{Error, Result};

/// Output format for the process-owned stderr sink.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum LogFormat {
    /// One JSON object per event for log collectors.
    #[default]
    Json,
    /// Compact text with event fields and span context.
    Human {
        /// Enable ANSI styling. For terminal-only color, use `std::io::IsTerminal`.
        ansi: bool,
    },
}

/// Settings for the process-owned subscriber installed by [`crate::process::Bootstrap`].
/// No environment discovery occurs. The default logs INFO and above on all targets.
#[derive(Clone, Debug)]
pub struct TelemetryConfig {
    pub(super) log_format: LogFormat,
    pub(super) log_filter: Targets,
    #[cfg(feature = "otel")]
    pub(super) traces: Option<super::TraceConfig>,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            log_format: LogFormat::default(),
            log_filter: Targets::new().with_default(LevelFilter::INFO),
            #[cfg(feature = "otel")]
            traces: None,
        }
    }
}

impl TelemetryConfig {
    /// Select the stderr log format. JSON is the default.
    #[must_use]
    pub fn with_log_format(mut self, format: LogFormat) -> Self {
        self.log_format = format;
        self
    }

    /// Explicitly register bounded OTLP traces when this bootstrap is installed.
    /// Compiling `otel` or constructing this value starts no exporter.
    #[cfg(feature = "otel")]
    pub fn with_traces(mut self, traces: super::TraceConfig) -> Self {
        self.traces = Some(traces);
        self
    }

    /// Set comma-separated target-prefix levels, for example `info,orders::db=debug`.
    /// The longest matching prefix wins. A missing default level means INFO.
    /// `off` disables output. Span/field predicates and regexes are unsupported.
    /// Invalid input fails without echoing its contents; input is limited to 4 KiB.
    pub fn with_log_filter(mut self, directives: &str) -> Result<Self> {
        if directives.len() > 4096
            || directives.trim().is_empty()
            || directives.contains(['[', ']', '{', '}'])
        {
            return Err(Error::Telemetry("invalid log filter"));
        }
        let filter = directives
            .parse::<Targets>()
            .map_err(|_| Error::Telemetry("invalid log filter"))?;
        self.log_filter = if filter.default_level().is_some() {
            filter
        } else {
            filter.with_default(LevelFilter::INFO)
        };
        Ok(self)
    }
}

#[cfg(test)]
#[path = "config_test.rs"]
mod config_test;