Skip to content
Merged
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
40 changes: 38 additions & 2 deletions src/telemetry/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::models::TelemetryFrontendLog;
use crate::config::TelemetryConfig;
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use tokio::runtime::Handle;

const STATE_ACTIVE: u8 = 0;
const STATE_CLOSING: u8 = 1;
Expand All @@ -32,6 +33,7 @@ pub(super) struct TelemetryClient {
flush_lock: tokio::sync::Mutex<()>,
state: AtomicU8,
timer: Mutex<Option<tokio::task::JoinHandle<()>>>,
runtime_handle: Handle,
}

impl TelemetryClient {
Expand All @@ -50,6 +52,7 @@ impl TelemetryClient {
flush_lock: tokio::sync::Mutex::new(()),
state: AtomicU8::new(STATE_ACTIVE),
timer: Mutex::new(None),
runtime_handle: Handle::current(),
});
client.start_timer();
client
Expand All @@ -68,7 +71,7 @@ impl TelemetryClient {

if count >= self.batch_size {
let client = Arc::clone(self);
tokio::spawn(async move {
self.runtime_handle.spawn(async move {
client.flush_once_if_idle().await;
});
}
Expand Down Expand Up @@ -139,7 +142,7 @@ impl TelemetryClient {
fn start_timer(self: &Arc<Self>) {
let client = Arc::downgrade(self);
let interval = self.flush_interval;
let handle = tokio::spawn(async move {
let handle = self.runtime_handle.spawn(async move {
loop {
tokio::time::sleep(interval).await;
let Some(client) = client.upgrade() else {
Expand Down Expand Up @@ -192,6 +195,39 @@ mod tests {
client.close().await;
}

#[tokio::test]
async fn enqueue_from_non_runtime_thread_uses_client_runtime() {
let exporter = CapturingTelemetryExporter::default();
let config = TelemetryConfig {
enabled: true,
batch_size: 1,
flush_interval: Duration::from_secs(60),
..TelemetryConfig::default()
};
let client = TelemetryClient::new(&config, Arc::new(exporter.clone()));

let worker_client = Arc::clone(&client);
std::thread::spawn(move || {
worker_client.enqueue(sample_log("one"));
})
.join()
.expect("enqueue from plain thread should not panic");

tokio::time::timeout(Duration::from_secs(1), async {
loop {
if !exporter.requests().is_empty() {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("batch flush should complete");

assert_eq!(exporter.requests()[0].proto_logs.len(), 1);
client.close().await;
}

#[tokio::test]
async fn interval_triggers_flush_before_batch_size() {
let exporter = CapturingTelemetryExporter::default();
Expand Down
Loading