use std::env;
use std::time::UNIX_EPOCH;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize)]
pub struct TelemetryPayload {
pub client_information: ClientInformation,
pub events: Vec<EventWithTimestamp>,
pub num_dropped_events: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EventWithTimestamp {
pub unixtime: u64,
pub event: TelemetryEvent,
}
fn unixtime() -> u64 {
match UNIX_EPOCH.elapsed() {
Ok(duration) => duration.as_secs(),
Err(_) => 0u64,
}
}
impl From<TelemetryEvent> for EventWithTimestamp {
fn from(event: TelemetryEvent) -> Self {
EventWithTimestamp {
unixtime: unixtime(),
event,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum TelemetryEvent {
Create,
Ingest,
Delete,
GarbageCollect,
RunService(String),
EndCommand { return_code: i32 },
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ClientInformation {
session_uuid: uuid::Uuid,
quickwit_version: String,
os: String,
arch: String,
hashed_host_username: String,
kubernetes: bool,
}
fn hashed_host_username() -> String {
let hostname = hostname::get()
.map(|hostname| hostname.to_string_lossy().to_string())
.unwrap_or_else(|_| "".to_string());
let username = username::get_user_name().unwrap_or_else(|_| "".to_string());
let hashed_value = format!("{}:{}", hostname, username);
let digest = md5::compute(hashed_value.as_bytes());
format!("{:x}", digest)
}
impl Default for ClientInformation {
fn default() -> ClientInformation {
ClientInformation {
session_uuid: Uuid::new_v4(),
quickwit_version: env!("CARGO_PKG_VERSION").to_string(),
os: env::consts::OS.to_string(),
arch: env::consts::ARCH.to_string(),
hashed_host_username: hashed_host_username(),
kubernetes: std::env::var_os("KUBERNETES_SERVICE_HOST").is_some(),
}
}
}