From f35e0ece3491c65865cfb3a366827cfa18c91349 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Oct 2024 11:56:11 -0700 Subject: [PATCH 01/12] remove global error handler --- opentelemetry/src/global/error_handler.rs | 86 -------------------- opentelemetry/src/global/internal_logging.rs | 46 +++++++++++ opentelemetry/src/global/mod.rs | 3 +- 3 files changed, 47 insertions(+), 88 deletions(-) delete mode 100644 opentelemetry/src/global/error_handler.rs 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::*; From dc1994b1cc30c6f051d03f4c7bd3e63290475425 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Oct 2024 13:00:04 -0700 Subject: [PATCH 02/12] bring back error_handler.rs, and remove the error_handler from there --- opentelemetry/src/global/error_handler.rs | 46 ++++++++++++++++++++ opentelemetry/src/global/internal_logging.rs | 46 -------------------- opentelemetry/src/global/mod.rs | 3 +- 3 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 opentelemetry/src/global/error_handler.rs diff --git a/opentelemetry/src/global/error_handler.rs b/opentelemetry/src/global/error_handler.rs new file mode 100644 index 0000000000..76ad4160b8 --- /dev/null +++ b/opentelemetry/src/global/error_handler.rs @@ -0,0 +1,46 @@ +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; + +/// 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()) + } +} diff --git a/opentelemetry/src/global/internal_logging.rs b/opentelemetry/src/global/internal_logging.rs index 401504847b..4391485619 100644 --- a/opentelemetry/src/global/internal_logging.rs +++ b/opentelemetry/src/global/internal_logging.rs @@ -1,14 +1,4 @@ #![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 @@ -166,39 +156,3 @@ 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 180f721ce1..5f27b0e08b 100644 --- a/opentelemetry/src/global/mod.rs +++ b/opentelemetry/src/global/mod.rs @@ -130,6 +130,7 @@ //! [`MeterProvider`]: crate::metrics::MeterProvider //! [`set_meter_provider`]: crate::global::set_meter_provider +mod error_handler; mod internal_logging; #[cfg(feature = "metrics")] mod metrics; @@ -137,8 +138,8 @@ mod metrics; mod propagation; #[cfg(feature = "trace")] mod trace; +pub use error_handler::Error; -pub use internal_logging::Error; #[cfg(feature = "metrics")] #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] pub use metrics::*; From 3ad57b5d32e31248b54538cbe02c27f8c3c9f799 Mon Sep 17 00:00:00 2001 From: Lalit Date: Sat, 2 Nov 2024 21:25:55 -0700 Subject: [PATCH 03/12] fix self diagnostic example to not use error handler --- examples/self-diagnostics/src/main.rs | 64 +++++++++++------------ opentelemetry/src/global/error_handler.rs | 1 - 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index a16204c3fe..1cfd5dc81e 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -1,14 +1,13 @@ -use opentelemetry::global::{self, set_error_handler, Error as OtelError}; +use opentelemetry::global::{self, Error as OtelError}; use opentelemetry::KeyValue; use opentelemetry_appender_tracing::layer; use opentelemetry_otlp::{LogExporter, MetricExporter, WithExportConfig}; use opentelemetry_sdk::metrics::PeriodicReader; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +use tracing_subscriber::filter::EnvFilter; use tracing_subscriber::fmt; use tracing_subscriber::prelude::*; use std::error::Error; -use tracing::error; use once_cell::sync::Lazy; use std::collections::HashSet; @@ -29,27 +28,13 @@ impl ErrorState { fn mark_as_seen(&self, err: &OtelError) -> bool { let mut seen_errors = self.seen_errors.lock().unwrap(); - seen_errors.insert(err.to_string()) + let error_message = err.to_string(); + !seen_errors.insert(error_message) // Returns false if already present } } static GLOBAL_ERROR_STATE: Lazy> = Lazy::new(|| Arc::new(ErrorState::new())); -fn custom_error_handler(err: OtelError) { - if GLOBAL_ERROR_STATE.mark_as_seen(&err) { - // log error not already seen - match err { - OtelError::Metric(err) => error!("OpenTelemetry metrics error occurred: {}", err), - OtelError::Trace(err) => error!("OpenTelemetry trace error occurred: {}", err), - OtelError::Log(err) => error!("OpenTelemetry log error occurred: {}", err), - OtelError::Propagation(err) => { - error!("OpenTelemetry propagation error occurred: {}", err) - } - OtelError::Other(err_msg) => error!("OpenTelemetry error occurred: {}", err_msg), - _ => error!("OpenTelemetry error occurred: {:?}", err), - } - } -} fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { let exporter = LogExporter::builder() @@ -77,18 +62,34 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { .add_directive("tonic=error".parse().unwrap()) .add_directive("reqwest=error".parse().unwrap()); - // Configuring the formatting layer specifically for OpenTelemetry internal logs. - // These logs starts with "opentelemetry" prefix in target. This allows specific logs - // from the OpenTelemetry-related components to be filtered and handled separately - // from the application logs - + // Filter for logs with "opentelemetry_" target prefix to use eprintln, + // only if they haven't been logged before. let opentelemetry_filter = tracing_subscriber::filter::filter_fn(|metadata| { - metadata.target().starts_with("opentelemetry") + if metadata.target().starts_with("opentelemetry_") { + // Ignore debug level logs + if metadata.level() == &tracing::Level::DEBUG { + return false; + } + let err = OtelError::Other(metadata.target().to_string()); // Convert target to an OtelError variant + if !GLOBAL_ERROR_STATE.mark_as_seen(&err) { + eprintln!( + "[{}] - {}: {}", + metadata.level(), + metadata.target(), + metadata.fields() + ); + } + false // Prevent these logs from propagating further + } else { + true // Allow other logs to continue + } }); - let fmt_opentelemetry_layer = fmt::layer() - .with_filter(LevelFilter::DEBUG) - .with_filter(opentelemetry_filter); + // Layer for OpenTelemetry internal logs + let fmt_opentelemetry_layer = fmt::layer().with_filter(opentelemetry_filter); + + // Layer for application logs, excluding OpenTelemetry internal logs + let fmt_application_layer = fmt::layer().with_filter(filter); // Configures the appender tracing layer, filtering out OpenTelemetry internal logs // to prevent infinite logging loops. @@ -102,7 +103,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { tracing_subscriber::registry() .with(fmt_opentelemetry_layer) - .with(fmt::layer().with_filter(filter)) + .with(fmt_application_layer) .with(otel_layer) .init(); provider @@ -130,11 +131,6 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider { #[tokio::main] async fn main() -> Result<(), Box> { - // Set the custom error handler - if let Err(err) = set_error_handler(custom_error_handler) { - eprintln!("Failed to set custom error handler: {}", err); - } - let logger_provider = init_logger_provider(); // Initialize the MeterProvider with the stdout Exporter. diff --git a/opentelemetry/src/global/error_handler.rs b/opentelemetry/src/global/error_handler.rs index 76ad4160b8..391fa25f63 100644 --- a/opentelemetry/src/global/error_handler.rs +++ b/opentelemetry/src/global/error_handler.rs @@ -1,5 +1,4 @@ use std::sync::PoisonError; -use std::sync::RwLock; #[cfg(feature = "logs")] use crate::logs::LogError; From f1dfc15bdaeae86791b79e8303591d93b199c14a Mon Sep 17 00:00:00 2001 From: Lalit Date: Sat, 2 Nov 2024 21:27:59 -0700 Subject: [PATCH 04/12] format --- examples/self-diagnostics/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index 1cfd5dc81e..4dd250ca1d 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -35,7 +35,6 @@ impl ErrorState { static GLOBAL_ERROR_STATE: Lazy> = Lazy::new(|| Arc::new(ErrorState::new())); - fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { let exporter = LogExporter::builder() .with_http() From 3f883bf21132685a20adc6e286e2b3f403d2babb Mon Sep 17 00:00:00 2001 From: Lalit Date: Sun, 3 Nov 2024 11:51:01 -0800 Subject: [PATCH 05/12] add changelog, and migration guide --- opentelemetry/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/opentelemetry/CHANGELOG.md b/opentelemetry/CHANGELOG.md index feb62933ae..bfc9035bdb 100644 --- a/opentelemetry/CHANGELOG.md +++ b/opentelemetry/CHANGELOG.md @@ -33,6 +33,12 @@ let counter = meter.u64_counter("my_counter").build(); - Replaced `global::meter_with_version` with `global::meter_with_scope` - Added `global::tracer_with_scope` +- **Breaking change**: [#2260](https://github.com/open-telemetry/opentelemetry-rust/pull/2260) + - Removed `global::set_error_handler` and `global::handle_error`. + - `global::handle_error` usage inside the opentelemetry crates has been replaced with `global::otel_info`, `otel_warn`, `otel_debug` and `otel_error` macros based on the severity of the internal logs. + - The default behavior of `global::handle_error` was to log the error using `eprintln!`. With otel macro, user now need to configure the tokio tracing layer to capture these logs. + - Refer to this PR description for migration guide. Also refer to [self-diagnostics](https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples/self-diagnostics) example on how to configure the tracing layer for internal logs. + ## v0.26.0 Released 2024-Sep-30 From d387d0945e728b86f86097d8517198bb54a40c66 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 4 Nov 2024 17:42:12 -0800 Subject: [PATCH 06/12] fix example --- examples/self-diagnostics/src/main.rs | 80 ++++++++++----------------- 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index 4dd250ca1d..c2c5753092 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -3,8 +3,6 @@ use opentelemetry::KeyValue; use opentelemetry_appender_tracing::layer; use opentelemetry_otlp::{LogExporter, MetricExporter, WithExportConfig}; use opentelemetry_sdk::metrics::PeriodicReader; -use tracing_subscriber::filter::EnvFilter; -use tracing_subscriber::fmt; use tracing_subscriber::prelude::*; use std::error::Error; @@ -48,62 +46,42 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { let cloned_provider = provider.clone(); - // Add a tracing filter to filter events from crates used by opentelemetry-otlp. - // The filter levels are set as follows: - // - Allow `info` level and above by default. - // - Restrict `hyper`, `tonic`, and `reqwest` to `error` level logs only. - // This ensures events generated from these crates within the OTLP Exporter are not looped back, - // thus preventing infinite event generation. - // Note: This will also drop events from these crates used outside the OTLP Exporter. - // For more details, see: https://github.com/open-telemetry/opentelemetry-rust/issues/761 - let filter = EnvFilter::new("info") - .add_directive("hyper=error".parse().unwrap()) - .add_directive("tonic=error".parse().unwrap()) - .add_directive("reqwest=error".parse().unwrap()); - - // Filter for logs with "opentelemetry_" target prefix to use eprintln, - // only if they haven't been logged before. - let opentelemetry_filter = tracing_subscriber::filter::filter_fn(|metadata| { - if metadata.target().starts_with("opentelemetry_") { - // Ignore debug level logs - if metadata.level() == &tracing::Level::DEBUG { - return false; - } - let err = OtelError::Other(metadata.target().to_string()); // Convert target to an OtelError variant - if !GLOBAL_ERROR_STATE.mark_as_seen(&err) { - eprintln!( - "[{}] - {}: {}", - metadata.level(), - metadata.target(), - metadata.fields() - ); - } - false // Prevent these logs from propagating further - } else { - true // Allow other logs to continue - } + // Specialized filter for specific targets + let specialized_filter = tracing_subscriber::filter::filter_fn(|metadata| { + let target = metadata.target(); + + // Only allow ERROR logs from specific targets + (target.starts_with("hyper") + || target.starts_with("hyper_util") + || target.starts_with("tonic") + || target.starts_with("tower") + || target.starts_with("reqwest") + || target.starts_with("opentelemetry_")) + && metadata.level() == &tracing::Level::ERROR }); - - // Layer for OpenTelemetry internal logs - let fmt_opentelemetry_layer = fmt::layer().with_filter(opentelemetry_filter); - - // Layer for application logs, excluding OpenTelemetry internal logs - let fmt_application_layer = fmt::layer().with_filter(filter); - - // Configures the appender tracing layer, filtering out OpenTelemetry internal logs - // to prevent infinite logging loops. - - let non_opentelemetry_filter = tracing_subscriber::filter::filter_fn(|metadata| { - !metadata.target().starts_with("opentelemetry") + // Configure fmt::Layer to print detailed log information, including structured fields + let fmt_opentelemetry_layer = + tracing_subscriber::fmt::layer().with_filter(specialized_filter.clone()); + + // Application filter to exclude specific targets entirely, regardless of level + let application_filter = tracing_subscriber::filter::filter_fn(|metadata| { + let target = metadata.target(); + + // Exclude logs from specific targets for the application layer + !(target.starts_with("hyper") + || target.starts_with("hyper_util") + || target.starts_with("tonic") + || target.starts_with("tower") + || target.starts_with("reqwest") + || target.starts_with("opentelemetry")) }); - let otel_layer = layer::OpenTelemetryTracingBridge::new(&cloned_provider) - .with_filter(non_opentelemetry_filter.clone()); + let fmt_application_layer = layer::OpenTelemetryTracingBridge::new(&cloned_provider) + .with_filter(application_filter.clone()); tracing_subscriber::registry() .with(fmt_opentelemetry_layer) .with(fmt_application_layer) - .with(otel_layer) .init(); provider } From f766af5cae59a42c795cbbbca6ffe9398c89f9a1 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 4 Nov 2024 17:43:14 -0800 Subject: [PATCH 07/12] add hyper as target --- examples/self-diagnostics/src/main.rs | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index c2c5753092..c36219ad30 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -13,26 +13,6 @@ use std::sync::{Arc, Mutex}; use std::sync::mpsc::channel; -struct ErrorState { - seen_errors: Mutex>, -} - -impl ErrorState { - fn new() -> Self { - ErrorState { - seen_errors: Mutex::new(HashSet::new()), - } - } - - fn mark_as_seen(&self, err: &OtelError) -> bool { - let mut seen_errors = self.seen_errors.lock().unwrap(); - let error_message = err.to_string(); - !seen_errors.insert(error_message) // Returns false if already present - } -} - -static GLOBAL_ERROR_STATE: Lazy> = Lazy::new(|| Arc::new(ErrorState::new())); - fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { let exporter = LogExporter::builder() .with_http() @@ -53,6 +33,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { // Only allow ERROR logs from specific targets (target.starts_with("hyper") || target.starts_with("hyper_util") + || target.starts_with("hyper") || target.starts_with("tonic") || target.starts_with("tower") || target.starts_with("reqwest") @@ -70,6 +51,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { // Exclude logs from specific targets for the application layer !(target.starts_with("hyper") || target.starts_with("hyper_util") + || target.starts_with("hyper") || target.starts_with("tonic") || target.starts_with("tower") || target.starts_with("reqwest") From 317db42324a42ebf39172457130e7e622f73aa5e Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 4 Nov 2024 17:51:54 -0800 Subject: [PATCH 08/12] add comment --- examples/self-diagnostics/src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index c36219ad30..616f5dbe04 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -26,7 +26,9 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { let cloned_provider = provider.clone(); - // Specialized filter for specific targets + // Specialized filter to process + // - ERROR logs from specific targets + // - ERROR logs generated internally. let specialized_filter = tracing_subscriber::filter::filter_fn(|metadata| { let target = metadata.target(); From 95be7ffe7933e187dd832452c926e2ce73b77092 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 4 Nov 2024 17:55:55 -0800 Subject: [PATCH 09/12] better naming of layers and filters --- examples/self-diagnostics/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index 616f5dbe04..10527c3a27 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -29,7 +29,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { // Specialized filter to process // - ERROR logs from specific targets // - ERROR logs generated internally. - let specialized_filter = tracing_subscriber::filter::filter_fn(|metadata| { + let internal_and_dependency_filter = tracing_subscriber::filter::filter_fn(|metadata| { let target = metadata.target(); // Only allow ERROR logs from specific targets @@ -43,8 +43,8 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { && metadata.level() == &tracing::Level::ERROR }); // Configure fmt::Layer to print detailed log information, including structured fields - let fmt_opentelemetry_layer = - tracing_subscriber::fmt::layer().with_filter(specialized_filter.clone()); + let fmt_internal_and_dependency_layer = + tracing_subscriber::fmt::layer().with_filter(internal_and_dependency_filter.clone()); // Application filter to exclude specific targets entirely, regardless of level let application_filter = tracing_subscriber::filter::filter_fn(|metadata| { @@ -60,12 +60,12 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { || target.starts_with("opentelemetry")) }); - let fmt_application_layer = layer::OpenTelemetryTracingBridge::new(&cloned_provider) + let application_layer = layer::OpenTelemetryTracingBridge::new(&cloned_provider) .with_filter(application_filter.clone()); tracing_subscriber::registry() - .with(fmt_opentelemetry_layer) - .with(fmt_application_layer) + .with(fmt_internal_and_dependency_layer) + .with(application_filter) .init(); provider } From 7c25d3ee74553f60677bea95cd7d4397b170b03d Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 4 Nov 2024 17:56:26 -0800 Subject: [PATCH 10/12] better naming of layers and filters --- examples/self-diagnostics/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index 10527c3a27..3fa3920399 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -65,7 +65,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { tracing_subscriber::registry() .with(fmt_internal_and_dependency_layer) - .with(application_filter) + .with(application_layer) .init(); provider } From 4a8ddfbd48cbcaf6634e8f6ddd3bb4669244feed Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 4 Nov 2024 18:04:07 -0800 Subject: [PATCH 11/12] Update examples/self-diagnostics/src/main.rs Co-authored-by: Cijo Thomas --- examples/self-diagnostics/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs index 3fa3920399..6a4ebe94fc 100644 --- a/examples/self-diagnostics/src/main.rs +++ b/examples/self-diagnostics/src/main.rs @@ -39,7 +39,7 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider { || target.starts_with("tonic") || target.starts_with("tower") || target.starts_with("reqwest") - || target.starts_with("opentelemetry_")) + || target.starts_with("opentelemetry")) && metadata.level() == &tracing::Level::ERROR }); // Configure fmt::Layer to print detailed log information, including structured fields From c61db830014a82b0016f81e1ef6c51e424997b6b Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 4 Nov 2024 18:04:40 -0800 Subject: [PATCH 12/12] Update opentelemetry/CHANGELOG.md Co-authored-by: Cijo Thomas --- opentelemetry/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry/CHANGELOG.md b/opentelemetry/CHANGELOG.md index bfc9035bdb..2b181cc8ad 100644 --- a/opentelemetry/CHANGELOG.md +++ b/opentelemetry/CHANGELOG.md @@ -36,7 +36,7 @@ let counter = meter.u64_counter("my_counter").build(); - **Breaking change**: [#2260](https://github.com/open-telemetry/opentelemetry-rust/pull/2260) - Removed `global::set_error_handler` and `global::handle_error`. - `global::handle_error` usage inside the opentelemetry crates has been replaced with `global::otel_info`, `otel_warn`, `otel_debug` and `otel_error` macros based on the severity of the internal logs. - - The default behavior of `global::handle_error` was to log the error using `eprintln!`. With otel macro, user now need to configure the tokio tracing layer to capture these logs. + - The default behavior of `global::handle_error` was to log the error using `eprintln!`. With otel macro, the internal logs get emitted via `tracing` macros of matching severity. Users now need to configure the `tracing` layer to capture these logs. - Refer to this PR description for migration guide. Also refer to [self-diagnostics](https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples/self-diagnostics) example on how to configure the tracing layer for internal logs. ## v0.26.0