Skip to content

Commit

Permalink
fix: Skip creating usage_anonymous_id when TABBY_DISABLE_USAGE_COLL…
Browse files Browse the repository at this point in the history
…ECTION is set (#864)

* dont write file if tracking is disabled

* remove comment, id is optional

* Update usage.rs

---------

Co-authored-by: Meng Zhang <[email protected]>
  • Loading branch information
praktiskt and wsxiaoys authored Nov 23, 2023
1 parent 9746865 commit 821ca2d
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions crates/tabby-common/src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static USAGE_API_ENDPOINT: &str = "https://app.tabbyml.com/api/usage";

struct UsageTracker {
id: String,
client: Option<Client>,
client: Client,
}

impl UsageTracker {
Expand Down Expand Up @@ -42,32 +42,27 @@ impl UsageTracker {
}

let id = fs::read_to_string(usage_id_file()).expect("Failed to read usage id");
let client = if std::env::var("TABBY_DISABLE_USAGE_COLLECTION").is_ok() {
None
} else {
Some(Client::new())
};

Self { id, client }
Self {
id,
client: Client::new(),
}
}

async fn capture<T>(&self, event: &str, properties: T)
where
T: Serialize,
{
if let Some(client) = &self.client {
let payload = Payload {
distinct_id: self.id.as_ref(),
event,
properties,
};
client
.post(USAGE_API_ENDPOINT)
.json(&payload)
.send()
.await
.ok();
}
let payload = Payload {
distinct_id: &self.id,
event,
properties,
};
self.client
.post(USAGE_API_ENDPOINT)
.json(&payload)
.send()
.await
.ok();
}
}

Expand All @@ -80,12 +75,20 @@ struct Payload<'a, T> {
}

lazy_static! {
static ref TRACKER: UsageTracker = UsageTracker::new();
static ref TRACKER: Option<UsageTracker> = {
if std::env::var("TABBY_DISABLE_USAGE_COLLECTION").is_ok() {
None
} else {
Some(UsageTracker::new())
}
};
}

pub async fn capture<T>(event: &str, properties: T)
where
T: Serialize,
{
TRACKER.capture(event, properties).await
if let Some(tracker) = TRACKER.as_ref() {
tracker.capture(event, properties).await;
}
}

0 comments on commit 821ca2d

Please sign in to comment.