use std::{collections::BTreeMap, time::Duration};
use crate::observability::{ObservabilityError, ObservabilityResult};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OtlpProtocol {
Grpc,
HttpProtobuf,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OtlpTraceConfig {
pub endpoint: String,
pub protocol: OtlpProtocol,
pub headers: BTreeMap<String, String>,
pub resource: BTreeMap<String, String>,
pub sampling_ratio: f64,
pub timeout: Duration,
}
impl Default for OtlpTraceConfig {
fn default() -> Self {
Self {
endpoint: "http://127.0.0.1:4317".to_string(),
protocol: OtlpProtocol::Grpc,
headers: BTreeMap::new(),
resource: BTreeMap::new(),
sampling_ratio: 1.0,
timeout: Duration::from_secs(5),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct TraceShutdownHandle {
installed: bool,
}
impl TraceShutdownHandle {
pub fn installed() -> Self {
Self { installed: true }
}
pub fn is_installed(&self) -> bool {
self.installed
}
pub fn flush(&self) -> ObservabilityResult<()> {
Ok(())
}
pub fn shutdown(self) -> ObservabilityResult<()> {
Ok(())
}
}
pub fn build_otlp_trace_config(config: OtlpTraceConfig) -> ObservabilityResult<OtlpTraceConfig> {
if config.endpoint.trim().is_empty() {
return Err(ObservabilityError::MissingOtlpEndpoint);
}
Ok(config)
}
#[cfg(test)]
mod tests {
use super::{OtlpTraceConfig, TraceShutdownHandle, build_otlp_trace_config};
use crate::observability::ObservabilityError;
#[test]
fn otlp_config_requires_endpoint() {
let error = build_otlp_trace_config(OtlpTraceConfig {
endpoint: String::new(),
..OtlpTraceConfig::default()
})
.expect_err("endpoint");
assert_eq!(error, ObservabilityError::MissingOtlpEndpoint);
}
#[test]
fn shutdown_handle_flushes_without_collector() {
let handle = TraceShutdownHandle::installed();
assert!(handle.is_installed());
handle.flush().expect("flush");
handle.shutdown().expect("shutdown");
}
}