From 66e06616db7769c9f4720b315987f9f1674f7e81 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 5 Nov 2024 14:05:51 -0700 Subject: [PATCH] Don't write temp files for telemetry logs (#20209) 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 --- 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(-) diff --git a/Cargo.lock b/Cargo.lock index 7159591322bb838870663c1749a77f74d756283c..d7b40c50f715a9764063d3e0556fe96ca5f8a6d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2432,7 +2432,6 @@ dependencies = [ "smol", "sysinfo", "telemetry_events", - "tempfile", "text", "thiserror", "time", diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 80e9a17ba8e421c1a5b136dfea4a5dd2edff312b..98920112972592f086e5c1641ccfdbf8ff5d460b 100644 --- a/crates/client/Cargo.toml +++ b/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 diff --git a/crates/client/src/telemetry.rs b/crates/client/src/telemetry.rs index 25f8709ff1f738b34724de0d99a32dfdb2e1bbf4..a061d19749aa2d59667f3c58f4d1039c089305c5 100644 --- a/crates/client/src/telemetry.rs +++ b/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, flush_events_task: Option>, - log_file: Option, + log_file: Option, is_staff: Option, first_event_date_time: Option>, 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 { - 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)?; diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 427e3ab9f63aca989c10801258d1e96f0615530b..41a5731be0db6991b35fb21f3941da962d422e7d 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -999,7 +999,7 @@ fn open_telemetry_log_file(workspace: &mut Workspace, cx: &mut ViewContext) -> Option { - 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() }