Don't write temp files for telemetry logs (#20209)

Conrad Irwin created

This still keeps a telemetry.log for the current session, but not one
file per load of zed.

Closes: #20045

Release Notes:

- Fixed a bug where Zed would create a new temporary file on each boot
for telemetry logs

Change summary

Cargo.lock                     |  1 -
crates/client/Cargo.toml       |  1 -
crates/client/src/telemetry.rs | 19 +++++++------------
crates/zed/src/zed.rs          |  2 +-
4 files changed, 8 insertions(+), 15 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -2432,7 +2432,6 @@ dependencies = [
  "smol",
  "sysinfo",
  "telemetry_events",
- "tempfile",
  "text",
  "thiserror",
  "time",

crates/client/Cargo.toml 🔗

@@ -44,7 +44,6 @@ sha2.workspace = true
 smol.workspace = true
 sysinfo.workspace = true
 telemetry_events.workspace = true
-tempfile.workspace = true
 text.workspace = true
 thiserror.workspace = true
 time.workspace = true

crates/client/src/telemetry.rs 🔗

@@ -13,6 +13,7 @@ use parking_lot::Mutex;
 use release_channel::ReleaseChannel;
 use settings::{Settings, SettingsStore};
 use sha2::{Digest, Sha256};
+use std::fs::File;
 use std::io::Write;
 use std::{env, mem, path::PathBuf, sync::Arc, time::Duration};
 use sysinfo::{CpuRefreshKind, Pid, ProcessRefreshKind, RefreshKind, System};
@@ -21,10 +22,7 @@ use telemetry_events::{
     EventRequestBody, EventWrapper, ExtensionEvent, InlineCompletionEvent, MemoryEvent, ReplEvent,
     SettingEvent,
 };
-use tempfile::NamedTempFile;
-#[cfg(not(debug_assertions))]
-use util::ResultExt;
-use util::TryFutureExt;
+use util::{ResultExt, TryFutureExt};
 use worktree::{UpdatedEntriesSet, WorktreeId};
 
 use self::event_coalescer::EventCoalescer;
@@ -46,7 +44,7 @@ struct TelemetryState {
     architecture: &'static str,
     events_queue: Vec<EventWrapper>,
     flush_events_task: Option<Task<()>>,
-    log_file: Option<NamedTempFile>,
+    log_file: Option<File>,
     is_staff: Option<bool>,
     first_event_date_time: Option<DateTime<Utc>>,
     event_coalescer: EventCoalescer,
@@ -223,15 +221,13 @@ impl Telemetry {
             os_name: os_name(),
             app_version: release_channel::AppVersion::global(cx).to_string(),
         }));
+        Self::log_file_path();
 
-        #[cfg(not(debug_assertions))]
         cx.background_executor()
             .spawn({
                 let state = state.clone();
                 async move {
-                    if let Some(tempfile) =
-                        NamedTempFile::new_in(paths::logs_dir().as_path()).log_err()
-                    {
+                    if let Some(tempfile) = File::create(Self::log_file_path()).log_err() {
                         state.lock().log_file = Some(tempfile);
                     }
                 }
@@ -280,8 +276,8 @@ impl Telemetry {
         Task::ready(())
     }
 
-    pub fn log_file_path(&self) -> Option<PathBuf> {
-        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
+    pub fn log_file_path() -> PathBuf {
+        paths::logs_dir().join("telemetry.log")
     }
 
     pub fn start(
@@ -645,7 +641,6 @@ impl Telemetry {
                     let mut json_bytes = Vec::new();
 
                     if let Some(file) = &mut this.state.lock().log_file {
-                        let file = file.as_file_mut();
                         for event in &mut events {
                             json_bytes.clear();
                             serde_json::to_writer(&mut json_bytes, event)?;

crates/zed/src/zed.rs 🔗

@@ -999,7 +999,7 @@ fn open_telemetry_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Works
         let app_state = workspace.app_state().clone();
         cx.spawn(|workspace, mut cx| async move {
             async fn fetch_log_string(app_state: &Arc<AppState>) -> Option<String> {
-                let path = app_state.client.telemetry().log_file_path()?;
+                let path = client::telemetry::Telemetry::log_file_path();
                 app_state.fs.load(&path).await.log_err()
             }