Skip to main content

klauthed_observability/error/
observability_error.rs

1use klauthed_macros::DomainError;
2
3/// Errors raised while initializing telemetry.
4///
5/// The `DomainError` impl (category/code) is generated by the derive: every
6/// variant is `internal`, with codes `observability.<snake_variant>`.
7#[derive(Debug, DomainError)]
8#[domain(prefix = "observability", category = "internal")]
9#[non_exhaustive]
10pub enum ObservabilityError {
11    /// A global subscriber/recorder was already installed in this process.
12    AlreadyInitialized,
13    /// Building or installing the tracing subscriber failed.
14    Subscriber(String),
15    /// Installing the metrics recorder failed.
16    #[cfg(feature = "metrics")]
17    Metrics(String),
18    /// Building the OpenTelemetry exporter/pipeline failed.
19    #[cfg(feature = "otel")]
20    Otel(String),
21}
22
23impl std::fmt::Display for ObservabilityError {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            ObservabilityError::AlreadyInitialized => {
27                f.write_str("telemetry has already been initialized in this process")
28            }
29            ObservabilityError::Subscriber(msg) => write!(f, "subscriber init failed: {msg}"),
30            #[cfg(feature = "metrics")]
31            ObservabilityError::Metrics(msg) => write!(f, "metrics init failed: {msg}"),
32            #[cfg(feature = "otel")]
33            ObservabilityError::Otel(msg) => write!(f, "opentelemetry init failed: {msg}"),
34        }
35    }
36}
37
38impl std::error::Error for ObservabilityError {}