pub mod client;
pub mod consent;
pub mod consent_file;
pub mod events;
pub mod identity;
pub mod network;
pub mod sentry;
pub mod super_props;
pub use client::TelemetryHandle;
pub use consent::{resolve, ConsentState, DecidedBy, Resolved};
pub use events::Event;
pub const POSTHOG_KEY: &str = env!("OPENLATCH_PROVIDER_POSTHOG_KEY");
pub const SENTRY_DSN: &str = env!("OPENLATCH_PROVIDER_SENTRY_DSN");
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
static GLOBAL_HANDLE: OnceLock<TelemetryHandle> = OnceLock::new();
pub fn consent_file_path(provider_dir: &Path) -> PathBuf {
provider_dir.join("telemetry.json")
}
pub fn init(provider_dir: &Path, machine_id: String, authenticated: bool) -> TelemetryHandle {
let resolved = resolve(&consent_file_path(provider_dir));
let debug_stderr = std::env::var("OPENLATCH_PROVIDER_TELEMETRY_DEBUG")
.map(|v| !v.is_empty() && v != "0")
.unwrap_or(false);
let baked_key_present = network::key_is_present();
let super_props = super_props::SuperProps::new(machine_id, authenticated);
client::start(client::ClientConfig {
resolved,
super_props,
debug_stderr,
baked_key_present,
})
}
pub fn install_global(handle: TelemetryHandle) -> bool {
GLOBAL_HANDLE.set(handle).is_ok()
}
pub fn capture_global(event: Event) {
if let Some(h) = GLOBAL_HANDLE.get() {
h.capture(event);
}
}
pub fn global() -> Option<&'static TelemetryHandle> {
GLOBAL_HANDLE.get()
}
pub async fn shutdown(_handle: TelemetryHandle) {
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn init_without_baked_key_is_noop() {
let tmp = TempDir::new().unwrap();
let handle = init(tmp.path(), "mach_a".into(), false);
assert!(!handle.is_enabled() || network::key_is_present());
}
#[test]
fn disabled_consent_is_noop_even_with_key() {
let tmp = TempDir::new().unwrap();
consent_file::write_consent(&consent_file_path(tmp.path()), false).unwrap();
let handle = init(tmp.path(), "mach_a".into(), false);
assert!(!handle.is_enabled());
}
}