Skip to content

Commit

Permalink
fix: add more meta (#3075)
Browse files Browse the repository at this point in the history
Co-authored-by: laststylebender <[email protected]>
  • Loading branch information
tusharmath and laststylebender14 authored Nov 2, 2024
1 parent ec22a4e commit d63b288
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tailcall-tracker/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::collections::HashSet;
use std::process::Output;

use chrono::{DateTime, Utc};
use machineid_rs::{Encryption, HWIDComponent, IdBuilder};
use sysinfo::System;
use tokio::process::Command;
use tokio::sync::Mutex;
use tokio::time::Duration;

use super::Result;
Expand Down Expand Up @@ -29,6 +34,7 @@ pub struct Tracker {
collectors: Vec<Box<dyn Collect>>,
can_track: bool,
start_time: DateTime<Utc>,
email: Mutex<Option<Vec<String>>>,
}

impl Default for Tracker {
Expand All @@ -44,6 +50,7 @@ impl Default for Tracker {
collectors: vec![ga_tracker, posthog_tracker],
can_track,
start_time,
email: Mutex::new(None),
}
}
}
Expand Down Expand Up @@ -74,6 +81,7 @@ impl Tracker {
cwd: cwd(),
user: user(),
version: version(),
email: self.email().await.clone(),
};

// Dispatch the event to all collectors
Expand All @@ -86,6 +94,74 @@ impl Tracker {

Ok(())
}

async fn email(&'static self) -> Vec<String> {
let mut guard = self.email.lock().await;
if guard.is_none() {
*guard = Some(email().await.into_iter().collect());
}
guard.clone().unwrap_or_default()
}
}

// Get the email address
async fn email() -> HashSet<String> {
fn parse(output: Output) -> Option<String> {
if output.status.success() {
let text = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !text.is_empty() {
return Some(text);
}
}

None
}

// From Git
async fn git() -> Option<String> {
let output = Command::new("git")
.args(["config", "--global", "user.email"])
.output()
.await
.ok()?;

parse(output)
}

// From SSH Keys
async fn ssh() -> Option<HashSet<String>> {
// Single command to find all unique email addresses from .pub files
let output = Command::new("sh")
.args([
"-c",
"cat ~/.ssh/*.pub | grep -o '[^ ]\\+@[^ ]\\+\\.[^ ]\\+'",
])
.output()
.await
.ok()?;

Some(parse(output)?.lines().map(|o| o.to_owned()).collect())
}

let git_email = git().await;
let ssh_emails = ssh().await;
let mut email_ids = HashSet::new();

if let Some(email) = git_email {
if !email.trim().is_empty() {
email_ids.insert(email.trim().to_string());
}
}

if let Some(emails) = ssh_emails {
for email in emails {
if !email.trim().is_empty() {
email_ids.insert(email.trim().to_string());
}
}
}

email_ids
}

// Generates a random client ID
Expand Down Expand Up @@ -141,6 +217,7 @@ fn os_name() -> String {

#[cfg(test)]
mod tests {

use lazy_static::lazy_static;

use super::*;
Expand Down
1 change: 1 addition & 0 deletions tailcall-tracker/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Event {
pub user: String,
pub args: Vec<String>,
pub version: String,
pub email: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down

1 comment on commit d63b288

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 8.20ms 3.64ms 120.29ms 95.17%
Req/Sec 3.11k 438.52 4.45k 80.83%

371427 requests in 30.01s, 1.86GB read

Requests/sec: 12374.74

Transfer/sec: 63.52MB

Please sign in to comment.