From c3c1a7759f7f945ff1ab35711b0a6cd68af2724a Mon Sep 17 00:00:00 2001 From: sapnasingh4852-source Date: Thu, 3 Dec 2026 14:04:39 +0530 Subject: [PATCH] fix(sdk): export after one interval in async processors The periodic ticker built by to_interval_stream already delays before its first tick, so the extra .skip(1) made the first metrics collection and batch flush wait two intervals. Remove the skip from the async PeriodicReader, BatchSpanProcessor and BatchLogProcessor so they match the thread-based processors, and add a regression test that fails if the skip is restored. --- opentelemetry-sdk/CHANGELOG.md | 6 ++ .../logs/log_processor_with_async_runtime.rs | 1 - .../periodic_reader_with_async_runtime.rs | 81 ++++++++++++++++++- .../span_processor_with_async_runtime.rs | 1 - 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 09c0ee80cc..0862f640be 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -2,6 +2,12 @@ ## vNext +- Fixed the experimental async-runtime `PeriodicReader`, `BatchSpanProcessor` + and `BatchLogProcessor` waiting two intervals before their first collection or + flush. The ticker already delays before firing, so the extra skipped tick is + removed and the first export happens after one interval, matching the + thread-based processors. + ([#3897](https://github.com/open-telemetry/opentelemetry-rust/issues/3897)) - Improved metrics measurement performance by speeding up attribute set lookups (faster `KeyValue` equality and boxed attribute keys). ([#3787](https://github.com/open-telemetry/opentelemetry-rust/pull/3787)) diff --git a/opentelemetry-sdk/src/logs/log_processor_with_async_runtime.rs b/opentelemetry-sdk/src/logs/log_processor_with_async_runtime.rs index 748edece73..916c76ec0f 100644 --- a/opentelemetry-sdk/src/logs/log_processor_with_async_runtime.rs +++ b/opentelemetry-sdk/src/logs/log_processor_with_async_runtime.rs @@ -130,7 +130,6 @@ impl BatchLogProcessor { // Timer will take a reference to the current runtime, so its important we do this within the // runtime.spawn() let ticker = to_interval_stream(inner_runtime.clone(), config.scheduled_delay) - .skip(1) // The ticker is fired immediately, so we should skip the first one to align with the interval. .map(|_| BatchMessage::Flush(None)); let timeout_runtime = inner_runtime.clone(); diff --git a/opentelemetry-sdk/src/metrics/periodic_reader_with_async_runtime.rs b/opentelemetry-sdk/src/metrics/periodic_reader_with_async_runtime.rs index e04af3277b..048f6a2ac5 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader_with_async_runtime.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader_with_async_runtime.rs @@ -112,9 +112,8 @@ where let runtime = self.runtime.clone(); let reader = reader.clone(); self.runtime.spawn(async move { - let ticker = to_interval_stream(runtime.clone(), self.interval) - .skip(1) // The ticker is fired immediately, so we should skip the first one to align with the interval. - .map(|_| Message::Export); + let ticker = + to_interval_stream(runtime.clone(), self.interval).map(|_| Message::Export); let messages = Box::pin(stream::select(message_receiver, ticker)); PeriodicReaderWorker { reader, @@ -528,6 +527,82 @@ mod tests { ); } + #[test] + #[cfg(feature = "rt-tokio")] + fn first_collection_happens_after_one_interval() { + use std::future::Future; + use std::sync::{Arc, Mutex}; + use tokio::sync::oneshot; + + type PendingDelay = (Duration, oneshot::Sender<()>); + + // A runtime whose delays are released by the test, one at a time, so + // the number of intervals waited before the first collection can be + // asserted without sleeping. + #[derive(Clone)] + struct GatedRuntime { + pending: Arc>>, + } + + impl crate::runtime::Runtime for GatedRuntime { + fn spawn(&self, future: F) + where + F: Future + Send + 'static, + { + std::thread::spawn(move || futures_executor::block_on(future)); + } + + fn delay(&self, duration: Duration) -> impl Future + Send + 'static { + let pending = Arc::clone(&self.pending); + async move { + let (release, released) = oneshot::channel(); + let sent = pending.lock().unwrap().send((duration, release)); + if sent.is_ok() { + let _ = released.await; + } + } + } + } + + let interval = Duration::from_secs(60); + let (pending, waits) = mpsc::channel(); + let runtime = GatedRuntime { + pending: Arc::new(Mutex::new(pending)), + }; + let exporter = InMemoryMetricExporter::default(); + let reader = PeriodicReader::builder(exporter, runtime) + .with_interval(interval) + .build(); + let (sender, collections) = mpsc::channel(); + let meter_provider = SdkMeterProvider::builder().with_reader(reader).build(); + let meter = meter_provider.meter("test"); + let _counter = meter + .u64_observable_counter("testcounter") + .with_callback(move |_| { + let _ = sender.send(()); + }) + .build(); + + // The worker waits one interval before it can collect. + let (waited, release) = waits + .recv_timeout(Duration::from_secs(5)) + .expect("the worker should wait before the first collection"); + assert_eq!(waited, interval); + assert!( + collections.try_recv().is_err(), + "nothing should be collected before the interval elapses" + ); + + // Releasing the first interval is enough: the first tick must not be + // skipped, or the first collection would move to two intervals. + release + .send(()) + .expect("the worker should still be running"); + collections + .recv_timeout(Duration::from_secs(5)) + .expect("the first interval should trigger a collection"); + } + fn collection_triggered_by_interval_helper(runtime: RT) where RT: crate::runtime::Runtime, diff --git a/opentelemetry-sdk/src/trace/span_processor_with_async_runtime.rs b/opentelemetry-sdk/src/trace/span_processor_with_async_runtime.rs index c54207fa61..0bcec3a16d 100644 --- a/opentelemetry-sdk/src/trace/span_processor_with_async_runtime.rs +++ b/opentelemetry-sdk/src/trace/span_processor_with_async_runtime.rs @@ -382,7 +382,6 @@ impl BatchSpanProcessor { // Timer will take a reference to the current runtime, so its important we do this within the // runtime.spawn() let ticker = to_interval_stream(inner_runtime.clone(), config.scheduled_delay) - .skip(1) // The ticker is fired immediately, so we should skip the first one to align with the interval. .map(|_| BatchMessage::Flush(None)); let timeout_runtime = inner_runtime.clone();