浏览代码

wip: basic telemetry

Jonathan Kelley 4 周之前
父节点
当前提交
d63150534a
共有 2 个文件被更改,包括 30 次插入9 次删除
  1. 26 9
      packages/cli/src/telemetry.rs
  2. 4 0
      packages/cli/src/workspace.rs

+ 26 - 9
packages/cli/src/telemetry.rs

@@ -4,25 +4,42 @@ use std::{
     sync::{LazyLock, Mutex, OnceLock},
 };
 
-use crate::Result;
+use crate::{Result, Workspace};
 use dioxus_cli_telemetry::TelemetryEvent;
 use dioxus_dx_wire_format::StructuredOutput;
 use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
 
 static TELEMETRY_TX: OnceLock<UnboundedSender<TelemetryEvent>> = OnceLock::new();
+static TELEMETRY_RX: OnceLock<Mutex<UnboundedReceiver<TelemetryEvent>>> = OnceLock::new();
 
 /// The main entrypoint for the log collector.
-/// We don't collect every log from the user's session since that can fill up quickly.
-/// Instead,
+///
+/// As the app runs, we simply fire off messages into the TelemetryTx handle.
+///
+/// Once the session is over, or the tx is flushed manually, we then log to a file.
+/// This prevents any performance issues from building up during long sesssion.
+/// For `dx serve`, we asyncronously flush after full rebuilds are *completed*.
 pub fn main(app: impl Future<Output = Result<StructuredOutput>>) {
-    let rt = tokio::runtime::Runtime::new().unwrap();
-    let _guard = rt.enter();
+    // let rt = tokio::runtime::Runtime::new().unwrap();
+    // let _guard = rt.enter();
 
-    let writer_thread = tokio::spawn(async move {
-        // while let Some(msg) = msg
-    });
+    // let res = rt.block_on(tokio::spawn(async move {}));
+    manually_flush();
+}
+
+/// Manually flush the telemetry queue so not as
+pub fn manually_flush() -> tokio::task::JoinHandle<()> {
+    tokio::task::spawn(async move {
+        let mut log_file = std::fs::File::options()
+            .append(true)
+            .open(Workspace::telemetry_file())
+            .unwrap();
 
-    let res = rt.block_on(tokio::spawn(async move {}));
+        let mut rx = TELEMETRY_RX.get().unwrap().lock().unwrap();
+        while let Ok(Some(msg)) = rx.try_next() {
+            _ = serde_json::to_writer(&mut log_file, &msg);
+        }
+    })
 }
 
 /// Set the backtrace, and then initiate a rollup upload of any pending logs.

+ 4 - 0
packages/cli/src/workspace.rs

@@ -439,6 +439,10 @@ impl Workspace {
             .map(|f| f.join("dioxus/"))
             .unwrap_or_else(|| dirs::home_dir().unwrap().join(".dioxus"))
     }
+
+    pub(crate) fn telemetry_file() -> PathBuf {
+        Self::dioxus_home_dir().join("telemetry.log")
+    }
 }
 
 impl std::fmt::Debug for Workspace {