Skip to main content

openauth_telemetry/
types.rs

1//! Telemetry types shared with the Better Auth telemetry payload shape.
2
3use serde::Serialize;
4use std::future::Future;
5use std::pin::Pin;
6use std::sync::Arc;
7
8/// Provider / framework detection row (mirrors upstream `DetectionInfo`).
9#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
10pub struct DetectionInfo {
11    pub name: String,
12    pub version: String,
13}
14
15/// Runtime identification for Rust hosts (replaces Node/Bun/Deno detection).
16#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
17pub struct RuntimeInfo {
18    pub name: String,
19    pub version: Option<String>,
20}
21
22/// Event sent to the telemetry collector or `custom_track`.
23#[derive(Clone, Debug, Serialize)]
24pub struct TelemetryEvent {
25    #[serde(rename = "type")]
26    pub event_type: String,
27    #[serde(rename = "anonymousId", skip_serializing_if = "Option::is_none")]
28    pub anonymous_id: Option<String>,
29    pub payload: serde_json::Value,
30}
31
32impl TelemetryEvent {
33    pub fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
34        serde_json::to_value(self)
35    }
36}
37
38pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
39
40pub type CustomTrackFn = Arc<dyn Fn(TelemetryEvent) -> BoxFuture<'static, ()> + Send + Sync>;
41
42/// Overrides used by integration tests (Vitest mocks replacement).
43#[derive(Clone, Default)]
44pub struct TelemetryTestHooks {
45    pub anonymous_id: Option<String>,
46    pub runtime: Option<RuntimeInfo>,
47    pub database: Option<Option<DetectionInfo>>,
48    pub framework: Option<Option<DetectionInfo>>,
49    pub environment: Option<String>,
50    pub system_info: Option<serde_json::Value>,
51    pub package_manager: Option<Option<DetectionInfo>>,
52}
53
54/// Optional hints and hooks for telemetry collection.
55#[derive(Clone, Default)]
56pub struct TelemetryContext {
57    pub database: Option<String>,
58    pub adapter: Option<String>,
59    pub skip_test_check: bool,
60    pub custom_track: Option<CustomTrackFn>,
61    pub http_transport: Option<Arc<dyn TelemetryHttpTransport>>,
62    pub test_hooks: Option<TelemetryTestHooks>,
63}
64
65/// Async HTTP sink for telemetry JSON payloads.
66pub trait TelemetryHttpTransport: Send + Sync {
67    fn post_json<'a>(
68        &'a self,
69        url: &'a str,
70        body: &'a serde_json::Value,
71    ) -> BoxFuture<'a, Result<(), TelemetryHttpError>>;
72}
73
74/// Transport failures are intentionally opaque (upstream logs and continues).
75#[derive(Clone, Debug)]
76pub struct TelemetryHttpError(pub String);
77
78impl std::fmt::Display for TelemetryHttpError {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        self.0.fmt(f)
81    }
82}
83
84impl std::error::Error for TelemetryHttpError {}