mirror of
https://github.com/openai/codex.git
synced 2026-11-23 15:18:14 +00:00
Flush analytics before in-process app server shutdown (#34831)
## Why The in-process app server can shut down while analytics for a completed turn are still queued, preventing the completed-turn and accepted-line events from being delivered. ## What changed - Add a bounded analytics queue flush that waits for all preceding facts to be processed. - Flush analytics after the in-process runtime drains its outbound work and before acknowledging shutdown. - Extend the in-process shutdown timeout budgets to cover runtime draining and the best-effort analytics flush. ## Testing - Verify that the flush waits for preceding facts and is a no-op when analytics is disabled. - Verify that shutdown waits for delayed analytics delivery, including the completed-turn and accepted-line events produced by a file change. GitOrigin-RevId: 57ca24e535e5e2f3f3202b2e5498f075676c910a
This commit is contained in:
1 parent
b5748e6e3c
commit
88f1cd9664
6 files changed
+435
-8
No files matched your search
@@ -50,14 +50,22 @@ use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
const ANALYTICS_EVENTS_QUEUE_SIZE: usize = 256;
|
||||
const ANALYTICS_EVENTS_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
// Covers two sequential POSTs plus queue/barrier scheduling; additional queued sends remain best-effort.
|
||||
const ANALYTICS_EVENTS_FLUSH_TIMEOUT: Duration = Duration::from_secs(25);
|
||||
const ANALYTICS_EVENT_DEDUPE_MAX_KEYS: usize = 4096;
|
||||
|
||||
pub(crate) enum AnalyticsEventsQueueMessage {
|
||||
Fact(Box<AnalyticsFact>),
|
||||
Flush(oneshot::Sender<()>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct AnalyticsEventsQueue {
|
||||
pub(crate) sender: mpsc::Sender<AnalyticsFact>,
|
||||
pub(crate) sender: mpsc::Sender<AnalyticsEventsQueueMessage>,
|
||||
pub(crate) app_used_emitted_keys: Arc<Mutex<HashSet<(String, String)>>>,
|
||||
pub(crate) plugin_used_emitted_keys: Arc<Mutex<HashSet<(String, String)>>>,
|
||||
}
|
||||
@@ -128,6 +136,13 @@ impl AnalyticsEventsQueue {
|
||||
tokio::spawn(async move {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
while let Some(input) = receiver.recv().await {
|
||||
let input = match input {
|
||||
AnalyticsEventsQueueMessage::Fact(input) => *input,
|
||||
AnalyticsEventsQueueMessage::Flush(done_tx) => {
|
||||
let _ = done_tx.send(());
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let mut events = Vec::new();
|
||||
reducer.ingest(input, &mut events).await;
|
||||
send_track_events(&auth_manager, &destination, events).await;
|
||||
@@ -141,7 +156,11 @@ impl AnalyticsEventsQueue {
|
||||
}
|
||||
|
||||
fn try_send(&self, input: AnalyticsFact) {
|
||||
if self.sender.try_send(input).is_err() {
|
||||
if self
|
||||
.sender
|
||||
.try_send(AnalyticsEventsQueueMessage::Fact(Box::new(input)))
|
||||
.is_err()
|
||||
{
|
||||
//TODO: add a metric for this
|
||||
tracing::warn!("dropping analytics events: queue is full");
|
||||
}
|
||||
@@ -206,6 +225,29 @@ impl AnalyticsEventsClient {
|
||||
Self { queue: None }
|
||||
}
|
||||
|
||||
pub async fn flush(&self) {
|
||||
let Some(queue) = self.queue.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let (done_tx, done_rx) = oneshot::channel();
|
||||
let flushed = tokio::time::timeout(ANALYTICS_EVENTS_FLUSH_TIMEOUT, async {
|
||||
if queue
|
||||
.sender
|
||||
.send(AnalyticsEventsQueueMessage::Flush(done_tx))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
done_rx.await.is_ok()
|
||||
})
|
||||
.await;
|
||||
|
||||
if !matches!(flushed, Ok(true)) {
|
||||
tracing::warn!("timed out or failed while flushing analytics events");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn track_skill_invocations(
|
||||
&self,
|
||||
tracking: TrackEventsContext,
|
||||
|
||||
Reference in new issue
Block a user