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 <elliott.codes@gmail.com>
This commit is contained in:
parent
a62a2fa8f7
commit
dbf986d37a
1 changed files with 31 additions and 21 deletions
|
@ -1,12 +1,13 @@
|
||||||
mod event_coalescer;
|
mod event_coalescer;
|
||||||
|
|
||||||
use crate::{ChannelId, TelemetrySettings};
|
use crate::{ChannelId, TelemetrySettings};
|
||||||
|
use anyhow::Result;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use clock::SystemClock;
|
use clock::SystemClock;
|
||||||
use collections::{HashMap, HashSet};
|
use collections::{HashMap, HashSet};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use gpui::{AppContext, BackgroundExecutor, Task};
|
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 once_cell::sync::Lazy;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use release_channel::ReleaseChannel;
|
use release_channel::ReleaseChannel;
|
||||||
|
@ -597,6 +598,29 @@ impl Telemetry {
|
||||||
self.state.lock().is_staff
|
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>) {
|
pub fn flush_events(self: &Arc<Self>) {
|
||||||
let mut state = self.state.lock();
|
let mut state = self.state.lock();
|
||||||
state.first_event_date_time = None;
|
state.first_event_date_time = None;
|
||||||
|
@ -623,10 +647,10 @@ impl Telemetry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
let request_body = {
|
||||||
let state = this.state.lock();
|
let state = this.state.lock();
|
||||||
|
|
||||||
let request_body = EventRequestBody {
|
EventRequestBody {
|
||||||
system_id: state.system_id.as_deref().map(Into::into),
|
system_id: state.system_id.as_deref().map(Into::into),
|
||||||
installation_id: state.installation_id.as_deref().map(Into::into),
|
installation_id: state.installation_id.as_deref().map(Into::into),
|
||||||
session_id: state.session_id.clone(),
|
session_id: state.session_id.clone(),
|
||||||
|
@ -639,25 +663,11 @@ impl Telemetry {
|
||||||
|
|
||||||
release_channel: state.release_channel.map(Into::into),
|
release_channel: state.release_channel.map(Into::into),
|
||||||
events,
|
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 = this.build_request(json_bytes, request_body)?;
|
||||||
|
let response = this.http_client.send(request).await?;
|
||||||
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?;
|
|
||||||
if response.status() != 200 {
|
if response.status() != 200 {
|
||||||
log::error!("Failed to send events: HTTP {:?}", response.status());
|
log::error!("Failed to send events: HTTP {:?}", response.status());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue