#![warn(missing_docs)]
#![warn(clippy::all)]
pub mod logging;
pub mod metrics;
pub mod tracing_config;
use std::sync::Arc;
use parking_lot::RwLock;
pub use logging::init_logging;
pub use metrics::{InferenceMetrics, MetricsCollector, Timer};
pub use tracing_config::{
create_tracer, init_tracing, LLMSpan, LLMSpanBuilder, TracingConfig, TracingGuard,
};
static TELEMETRY: RwLock<Option<Arc<Telemetry>>> = RwLock::new(None);
pub struct Telemetry {
pub metrics: MetricsCollector,
}
impl Telemetry {
pub fn init(config: TelemetryConfig) -> Arc<Self> {
let telemetry = Arc::new(Self {
metrics: MetricsCollector::new(&config),
});
*TELEMETRY.write() = Some(Arc::clone(&telemetry));
telemetry
}
#[must_use]
pub fn global() -> Option<Arc<Self>> {
TELEMETRY.read().clone()
}
}
#[derive(Debug, Clone, Default)]
pub struct TelemetryConfig {
pub service_name: String,
pub otlp_endpoint: Option<String>,
pub prometheus_enabled: bool,
pub prometheus_addr: Option<String>,
pub log_level: String,
pub json_logs: bool,
}
impl TelemetryConfig {
#[must_use]
pub fn new(service_name: impl Into<String>) -> Self {
Self {
service_name: service_name.into(),
otlp_endpoint: None,
prometheus_enabled: false,
prometheus_addr: None,
log_level: "info".to_string(),
json_logs: false,
}
}
#[must_use]
pub fn with_otlp(mut self, endpoint: impl Into<String>) -> Self {
self.otlp_endpoint = Some(endpoint.into());
self
}
#[must_use]
pub fn with_prometheus(mut self, addr: impl Into<String>) -> Self {
self.prometheus_enabled = true;
self.prometheus_addr = Some(addr.into());
self
}
#[must_use]
pub fn with_log_level(mut self, level: impl Into<String>) -> Self {
self.log_level = level.into();
self
}
#[must_use]
pub fn with_json_logs(mut self) -> Self {
self.json_logs = true;
self
}
}