From dbf986d37a6fb768fc502b0d191d979f0c5b3f0c Mon Sep 17 00:00:00 2001 From: Adam Wolff Date: Wed, 9 Oct 2024 07:47:59 -0700 Subject: [PATCH] telemetry: Refactor telemetry request into separate method (#18890) Refactor telemetry request into separate method to make it easier to override in a fork. Release Notes: - N/A --------- Co-authored-by: Marshall Bowers --- crates/client/src/telemetry.rs | 52 ++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/crates/client/src/telemetry.rs b/crates/client/src/telemetry.rs index 1dc3cedee2..ba03255d54 100644 --- a/crates/client/src/telemetry.rs +++ b/crates/client/src/telemetry.rs @@ -1,12 +1,13 @@ mod event_coalescer; use crate::{ChannelId, TelemetrySettings}; +use anyhow::Result; use chrono::{DateTime, Utc}; use clock::SystemClock; use collections::{HashMap, HashSet}; use futures::Future; use gpui::{AppContext, BackgroundExecutor, Task}; -use http_client::{self, HttpClient, HttpClientWithUrl, Method}; +use http_client::{self, AsyncBody, HttpClient, HttpClientWithUrl, Method, Request}; use once_cell::sync::Lazy; use parking_lot::Mutex; use release_channel::ReleaseChannel; @@ -597,6 +598,29 @@ impl Telemetry { self.state.lock().is_staff } + fn build_request( + self: &Arc, + // We take in the JSON bytes buffer so we can reuse the existing allocation. + mut json_bytes: Vec, + event_request: EventRequestBody, + ) -> Result> { + json_bytes.clear(); + serde_json::to_writer(&mut json_bytes, &event_request)?; + + let checksum = calculate_json_checksum(&json_bytes).unwrap_or("".to_string()); + + Ok(Request::builder() + .method(Method::POST) + .uri( + self.http_client + .build_zed_api_url("/telemetry/events", &[])? + .as_ref(), + ) + .header("Content-Type", "application/json") + .header("x-zed-checksum", checksum) + .body(json_bytes.into())?) + } + pub fn flush_events(self: &Arc) { let mut state = self.state.lock(); state.first_event_date_time = None; @@ -623,10 +647,10 @@ impl Telemetry { } } - { + let request_body = { let state = this.state.lock(); - let request_body = EventRequestBody { + EventRequestBody { system_id: state.system_id.as_deref().map(Into::into), installation_id: state.installation_id.as_deref().map(Into::into), session_id: state.session_id.clone(), @@ -639,25 +663,11 @@ impl Telemetry { release_channel: state.release_channel.map(Into::into), events, - }; - json_bytes.clear(); - serde_json::to_writer(&mut json_bytes, &request_body)?; - } + } + }; - let checksum = calculate_json_checksum(&json_bytes).unwrap_or("".to_string()); - - let request = http_client::Request::builder() - .method(Method::POST) - .uri( - this.http_client - .build_zed_api_url("/telemetry/events", &[])? - .as_ref(), - ) - .header("Content-Type", "application/json") - .header("x-zed-checksum", checksum) - .body(json_bytes.into()); - - let response = this.http_client.send(request?).await?; + let request = this.build_request(json_bytes, request_body)?; + let response = this.http_client.send(request).await?; if response.status() != 200 { log::error!("Failed to send events: HTTP {:?}", response.status()); }