Skip to content

Commit

Permalink
Fix debug loop, add notes for #[tokio::test] (#552)
Browse files Browse the repository at this point in the history
We choose to break the loop at instrument level because debug info on `Accumulator` can be found via other structs and descriptors seem to be more important for instruments.
  • Loading branch information
TommyCpp authored May 18, 2021
1 parent 88e779d commit ebb950a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
8 changes: 6 additions & 2 deletions opentelemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@
//! to use batch span processors where the spans will be sent in batch, reducing the number of requests
//! and resource needed.
//!
//! Batch span processors need to run a background task to collect and send spans. Different runtime
//! needs different ways to handle the background task.
//! Batch span processors need to run a background task to collect and send spans. Different runtimes
//! need different ways to handle the background task. Using a `Runtime` that's not compatible with the
//! underlying runtime can cause deadlock.
//!
//! ### Tokio
//!
Expand All @@ -72,6 +73,9 @@
//! task with other tasks in the same runtime. Thus, users should enable `rt-tokio-current-thread` feature
//! to ask the background task be scheduled on a different runtime on a different thread.
//!
//! Note that by default `#[tokio::test]` uses `current_thread_scheduler` and should use `rt-tokio-current-thread`
//! feature.
//!
//! ## Related Crates
//!
//! In addition to `opentelemetry`, the [`open-telemetry/opentelemetry-rust`]
Expand Down
31 changes: 30 additions & 1 deletion opentelemetry/src/sdk/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,20 @@ impl sdk_api::SyncBoundInstrumentCore for Record {
}
}

#[derive(Debug)]
struct Instrument {
descriptor: Descriptor,
meter: Accumulator,
}

impl std::fmt::Debug for Instrument {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Instrument")
.field("descriptor", &self.descriptor)
.field("meter", &"Accumulator")
.finish()
}
}

impl sdk_api::InstrumentCore for Instrument {
fn descriptor(&self) -> &Descriptor {
&self.descriptor
Expand Down Expand Up @@ -579,3 +587,24 @@ impl sdk_api::MeterCore for Accumulator {
self.0.register_runner(runner)
}
}

#[cfg(test)]
mod tests {
use crate::metrics::MeterProvider;
use crate::sdk::export::metrics::ExportKindSelector;
use crate::sdk::metrics::controllers::pull;
use crate::sdk::metrics::selectors::simple::Selector;

// Prevent the debug message to get into loop
#[test]
fn test_debug_message() {
let controller = pull(
Box::new(Selector::Exact),
Box::new(ExportKindSelector::Delta),
)
.build();
let meter = controller.provider().meter("test", None);
let counter = meter.f64_counter("test").init();
println!("{:?}, {:?}, {:?}", controller, meter, counter);
}
}

0 comments on commit ebb950a

Please sign in to comment.