Report editor open and save events to Amplitude
Co-authored-by: Max Brunsfeld <max@zed.dev>
This commit is contained in:
parent
f0c50c1e0a
commit
824fdb54e6
8 changed files with 137 additions and 35 deletions
|
@ -1,10 +1,12 @@
|
|||
use crate::{http::HttpClient, ZED_SECRET_CLIENT_TOKEN};
|
||||
use crate::http::HttpClient;
|
||||
use db::Db;
|
||||
use gpui::{
|
||||
executor::Background,
|
||||
serde_json::{self, value::Map, Value},
|
||||
AppContext, Task,
|
||||
};
|
||||
use isahc::Request;
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::Mutex;
|
||||
use serde::Serialize;
|
||||
use std::{
|
||||
|
@ -12,7 +14,8 @@ use std::{
|
|||
sync::Arc,
|
||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use util::{post_inc, ResultExt};
|
||||
use util::{post_inc, ResultExt, TryFutureExt};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct Telemetry {
|
||||
client: Arc<dyn HttpClient>,
|
||||
|
@ -33,7 +36,13 @@ struct TelemetryState {
|
|||
flush_task: Option<Task<()>>,
|
||||
}
|
||||
|
||||
const AMPLITUDE_EVENTS_URL: &'static str = "https//api2.amplitude.com/batch";
|
||||
const AMPLITUDE_EVENTS_URL: &'static str = "https://api2.amplitude.com/batch";
|
||||
|
||||
lazy_static! {
|
||||
static ref AMPLITUDE_API_KEY: Option<String> = option_env!("AMPLITUDE_API_KEY")
|
||||
.map(|key| key.to_string())
|
||||
.or(std::env::var("AMPLITUDE_API_KEY").ok());
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AmplitudeEventBatch {
|
||||
|
@ -62,6 +71,10 @@ const MAX_QUEUE_LEN: usize = 1;
|
|||
#[cfg(not(debug_assertions))]
|
||||
const MAX_QUEUE_LEN: usize = 10;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
|
||||
|
||||
impl Telemetry {
|
||||
|
@ -93,7 +106,52 @@ impl Telemetry {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn log_event(self: &Arc<Self>, kind: &str, properties: Value) {
|
||||
pub fn start(self: &Arc<Self>, db: Arc<Db>) {
|
||||
let this = self.clone();
|
||||
self.executor
|
||||
.spawn(
|
||||
async move {
|
||||
let device_id = if let Some(device_id) = db
|
||||
.read(["device_id"])?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.next()
|
||||
.and_then(|bytes| String::from_utf8(bytes).ok())
|
||||
{
|
||||
device_id
|
||||
} else {
|
||||
let device_id = Uuid::new_v4().to_string();
|
||||
db.write([("device_id", device_id.as_bytes())])?;
|
||||
device_id
|
||||
};
|
||||
|
||||
let device_id = Some(Arc::from(device_id));
|
||||
let mut state = this.state.lock();
|
||||
state.device_id = device_id.clone();
|
||||
for event in &mut state.queue {
|
||||
event.device_id = device_id.clone();
|
||||
}
|
||||
if !state.queue.is_empty() {
|
||||
drop(state);
|
||||
this.flush();
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.log_err(),
|
||||
)
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn set_user_id(&self, user_id: Option<u64>) {
|
||||
self.state.lock().user_id = user_id.map(|id| id.to_string().into());
|
||||
}
|
||||
|
||||
pub fn report_event(self: &Arc<Self>, kind: &str, properties: Value) {
|
||||
if AMPLITUDE_API_KEY.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut state = self.state.lock();
|
||||
let event = AmplitudeEvent {
|
||||
event_type: kind.to_string(),
|
||||
|
@ -116,38 +174,39 @@ impl Telemetry {
|
|||
event_id: post_inc(&mut state.next_event_id),
|
||||
};
|
||||
state.queue.push(event);
|
||||
if state.queue.len() >= MAX_QUEUE_LEN {
|
||||
drop(state);
|
||||
self.flush();
|
||||
} else {
|
||||
let this = self.clone();
|
||||
let executor = self.executor.clone();
|
||||
state.flush_task = Some(self.executor.spawn(async move {
|
||||
executor.timer(DEBOUNCE_INTERVAL).await;
|
||||
this.flush();
|
||||
}));
|
||||
if state.device_id.is_some() {
|
||||
if state.queue.len() >= MAX_QUEUE_LEN {
|
||||
drop(state);
|
||||
self.flush();
|
||||
} else {
|
||||
let this = self.clone();
|
||||
let executor = self.executor.clone();
|
||||
state.flush_task = Some(self.executor.spawn(async move {
|
||||
executor.timer(DEBOUNCE_INTERVAL).await;
|
||||
this.flush();
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
let mut state = self.state.lock();
|
||||
let events = mem::take(&mut state.queue);
|
||||
let client = self.client.clone();
|
||||
state.flush_task.take();
|
||||
self.executor
|
||||
.spawn(async move {
|
||||
let body = serde_json::to_vec(&AmplitudeEventBatch {
|
||||
api_key: ZED_SECRET_CLIENT_TOKEN,
|
||||
events,
|
||||
|
||||
if let Some(api_key) = AMPLITUDE_API_KEY.as_ref() {
|
||||
let client = self.client.clone();
|
||||
self.executor
|
||||
.spawn(async move {
|
||||
let batch = AmplitudeEventBatch { api_key, events };
|
||||
let body = serde_json::to_vec(&batch).log_err()?;
|
||||
let request = Request::post(AMPLITUDE_EVENTS_URL)
|
||||
.body(body.into())
|
||||
.log_err()?;
|
||||
client.send(request).await.log_err();
|
||||
Some(())
|
||||
})
|
||||
.log_err()?;
|
||||
let request = Request::post(AMPLITUDE_EVENTS_URL)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body.into())
|
||||
.log_err()?;
|
||||
client.send(request).await.log_err();
|
||||
Some(())
|
||||
})
|
||||
.detach();
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue