telemetry: Refactor telemetry request into separate method (#18890)

Adam Wolff and Marshall Bowers created

Refactor telemetry request into separate method to make it easier to
override in a fork.

Release Notes:

- N/A

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>

Change summary

crates/client/src/telemetry.rs | 52 +++++++++++++++++++++--------------
1 file changed, 31 insertions(+), 21 deletions(-)

Detailed changes

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<Self>,
+        // We take in the JSON bytes buffer so we can reuse the existing allocation.
+        mut json_bytes: Vec<u8>,
+        event_request: EventRequestBody,
+    ) -> Result<Request<AsyncBody>> {
+        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<Self>) {
         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());
                     }