Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Mutex<mpsc::Sender<PendingDelay>>>,
}

impl crate::runtime::Runtime for GatedRuntime {
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static,
{
std::thread::spawn(move || futures_executor::block_on(future));
}

fn delay(&self, duration: Duration) -> impl Future<Output = ()> + 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<RT>(runtime: RT)
where
RT: crate::runtime::Runtime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ impl<R: RuntimeChannel> BatchSpanProcessor<R> {
// 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();

Expand Down
Loading