use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Surface {
Cdx,
Content,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Phase {
Start,
Complete,
Error,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorClass {
Timeout,
Connect,
Tls,
Protocol,
Decode,
Blocked,
Http,
Other,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Event {
pub surface: Surface,
pub phase: Phase,
pub method: &'static str,
pub url: Arc<str>,
pub status: Option<u16>,
pub elapsed: Option<Duration>,
pub error: Option<ErrorClass>,
pub extras: &'static [(&'static str, &'static str)],
}
impl Event {
pub(crate) fn start(surface: Surface, method: &'static str, url: Arc<str>) -> Self {
Self {
surface,
phase: Phase::Start,
method,
url,
status: None,
elapsed: None,
error: None,
extras: &[],
}
}
pub(crate) fn complete(
surface: Surface,
method: &'static str,
url: Arc<str>,
status: u16,
elapsed: Duration,
) -> Self {
Self {
surface,
phase: Phase::Complete,
method,
url,
status: Some(status),
elapsed: Some(elapsed),
error: None,
extras: &[],
}
}
pub(crate) fn error(
surface: Surface,
method: &'static str,
url: Arc<str>,
status: Option<u16>,
elapsed: Option<Duration>,
error: ErrorClass,
) -> Self {
Self {
surface,
phase: Phase::Error,
method,
url,
status,
elapsed,
error: Some(error),
extras: &[],
}
}
}
pub trait Observer: Send + Sync {
fn on_event(&self, event: &Event);
}