Skip to main content

a2a_protocol_server/otel/telemetry/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! [`TelemetryError`]. Split out of `mod.rs` at the 500-line ratchet.
5
6/// Why [`Telemetry`](super::Telemetry) could not be built, or lost telemetry.
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum TelemetryError {
10    /// A variable, or a value given in code, this crate cannot act on.
11    Config {
12        /// The variable or setting.
13        variable: String,
14        /// Its value.
15        value: String,
16        /// What is wrong with it.
17        reason: String,
18    },
19    /// `opentelemetry-otlp` refused to build an exporter.
20    Exporter {
21        /// `traces`, `metrics` or `logs`.
22        signal: &'static str,
23        /// Its error.
24        source: opentelemetry_otlp::ExporterBuildError,
25    },
26    /// The TLS configuration could not be built.
27    Tls(String),
28    /// The export thread could not be started.
29    Runtime(std::io::Error),
30    /// A flush or shutdown failed, per signal.
31    Export(Vec<String>),
32}
33
34impl TelemetryError {
35    pub(super) fn config(variable: &str, value: &str, reason: impl Into<String>) -> Self {
36        Self::Config {
37            variable: variable.to_owned(),
38            value: value.to_owned(),
39            reason: reason.into(),
40        }
41    }
42}
43
44impl std::fmt::Display for TelemetryError {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            Self::Config {
48                variable,
49                value,
50                reason,
51            } if value.is_empty() => {
52                write!(f, "{variable}: {reason}")
53            }
54            Self::Config {
55                variable,
56                value,
57                reason,
58            } => write!(f, "{variable}={value:?}: {reason}"),
59            Self::Exporter { signal, source } => write!(f, "{signal} exporter: {source}"),
60            Self::Tls(reason) => write!(f, "OTLP TLS: {reason}"),
61            Self::Runtime(e) => write!(f, "OTLP export thread: {e}"),
62            Self::Export(failed) => write!(f, "OTLP export failed: {}", failed.join("; ")),
63        }
64    }
65}
66
67impl std::error::Error for TelemetryError {
68    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69        match self {
70            Self::Exporter { source, .. } => Some(source),
71            Self::Runtime(e) => Some(e),
72            _ => None,
73        }
74    }
75}