diff --git a/opentelemetry/src/global/error_handler.rs b/opentelemetry/src/global/error_handler.rs deleted file mode 100644 index 3a717154bf..0000000000 --- a/opentelemetry/src/global/error_handler.rs +++ /dev/null @@ -1,86 +0,0 @@ -use std::sync::PoisonError; -use std::sync::RwLock; - -#[cfg(feature = "logs")] -use crate::logs::LogError; -#[cfg(feature = "metrics")] -use crate::metrics::MetricError; -use crate::propagation::PropagationError; -#[cfg(feature = "trace")] -use crate::trace::TraceError; -use once_cell::sync::Lazy; - -static GLOBAL_ERROR_HANDLER: Lazy>> = Lazy::new(|| RwLock::new(None)); - -/// Wrapper for error from both tracing and metrics part of open telemetry. -#[derive(thiserror::Error, Debug)] -#[non_exhaustive] -pub enum Error { - #[cfg(feature = "trace")] - #[cfg_attr(docsrs, doc(cfg(feature = "trace")))] - #[error(transparent)] - /// Failed to export traces. - Trace(#[from] TraceError), - #[cfg(feature = "metrics")] - #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] - #[error(transparent)] - /// An issue raised by the metrics module. - Metric(#[from] MetricError), - - #[cfg(feature = "logs")] - #[cfg_attr(docsrs, doc(cfg(feature = "logs")))] - #[error(transparent)] - /// Failed to export logs. - Log(#[from] LogError), - - #[error(transparent)] - /// Error happens when injecting and extracting information using propagators. - Propagation(#[from] PropagationError), - - #[error("{0}")] - /// Other types of failures not covered by the variants above. - Other(String), -} - -impl From> for Error { - fn from(err: PoisonError) -> Self { - Error::Other(err.to_string()) - } -} - -struct ErrorHandler(Box); - -/// Handle error using the globally configured error handler. -/// -/// Writes to stderr if unset. -pub fn handle_error>(err: T) { - match GLOBAL_ERROR_HANDLER.read() { - Ok(handler) if handler.is_some() => (handler.as_ref().unwrap().0)(err.into()), - _ => match err.into() { - #[cfg(feature = "metrics")] - #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] - Error::Metric(err) => eprintln!("OpenTelemetry metrics error occurred. {}", err), - #[cfg(feature = "trace")] - #[cfg_attr(docsrs, doc(cfg(feature = "trace")))] - Error::Trace(err) => eprintln!("OpenTelemetry trace error occurred. {}", err), - #[cfg(feature = "logs")] - #[cfg_attr(docsrs, doc(cfg(feature = "logs")))] - Error::Log(err) => eprintln!("OpenTelemetry log error occurred. {}", err), - Error::Propagation(err) => { - eprintln!("OpenTelemetry propagation error occurred. {}", err) - } - Error::Other(err_msg) => eprintln!("OpenTelemetry error occurred. {}", err_msg), - }, - } -} - -/// Set global error handler. -pub fn set_error_handler(f: F) -> std::result::Result<(), Error> -where - F: Fn(Error) + Send + Sync + 'static, -{ - GLOBAL_ERROR_HANDLER - .write() - .map(|mut handler| *handler = Some(ErrorHandler(Box::new(f)))) - .map_err(Into::into) -} diff --git a/opentelemetry/src/global/internal_logging.rs b/opentelemetry/src/global/internal_logging.rs index 4391485619..401504847b 100644 --- a/opentelemetry/src/global/internal_logging.rs +++ b/opentelemetry/src/global/internal_logging.rs @@ -1,4 +1,14 @@ #![allow(unused_macros)] + +#[cfg(feature = "logs")] +use crate::logs::LogError; +#[cfg(feature = "metrics")] +use crate::metrics::MetricError; +use crate::propagation::PropagationError; +#[cfg(feature = "trace")] +use crate::trace::TraceError; +use std::sync::PoisonError; + /// /// **Note**: These macros (`otel_info!`, `otel_warn!`, `otel_debug!`, and `otel_error!`) are intended to be used /// **internally within OpenTelemetry code** or for **custom exporters and processors**. They are not designed @@ -156,3 +166,39 @@ macro_rules! otel_error { } }; } + +/// Wrapper for error from tracing, log, and metrics part of open telemetry. +#[derive(thiserror::Error, Debug)] +#[non_exhaustive] +pub enum Error { + #[cfg(feature = "trace")] + #[cfg_attr(docsrs, doc(cfg(feature = "trace")))] + #[error(transparent)] + /// Failed to export traces. + Trace(#[from] TraceError), + #[cfg(feature = "metrics")] + #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] + #[error(transparent)] + /// An issue raised by the metrics module. + Metric(#[from] MetricError), + + #[cfg(feature = "logs")] + #[cfg_attr(docsrs, doc(cfg(feature = "logs")))] + #[error(transparent)] + /// Failed to export logs. + Log(#[from] LogError), + + #[error(transparent)] + /// Error happens when injecting and extracting information using propagators. + Propagation(#[from] PropagationError), + + #[error("{0}")] + /// Other types of failures not covered by the variants above. + Other(String), +} + +impl From> for Error { + fn from(err: PoisonError) -> Self { + Error::Other(err.to_string()) + } +} diff --git a/opentelemetry/src/global/mod.rs b/opentelemetry/src/global/mod.rs index 4fada1123a..180f721ce1 100644 --- a/opentelemetry/src/global/mod.rs +++ b/opentelemetry/src/global/mod.rs @@ -130,7 +130,6 @@ //! [`MeterProvider`]: crate::metrics::MeterProvider //! [`set_meter_provider`]: crate::global::set_meter_provider -mod error_handler; mod internal_logging; #[cfg(feature = "metrics")] mod metrics; @@ -139,7 +138,7 @@ mod propagation; #[cfg(feature = "trace")] mod trace; -pub use error_handler::{handle_error, set_error_handler, Error}; +pub use internal_logging::Error; #[cfg(feature = "metrics")] #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] pub use metrics::*;