use std::time::Duration;
#[derive(Debug, Clone)]
pub struct OpenTelemetryConfig {
pub endpoint: Option<String>,
pub export_interval: Duration,
pub max_batch_size: usize,
pub async_export: bool,
pub export_timeout: Duration,
pub service_name: String,
pub resource_attributes: Vec<(String, String)>,
}
impl Default for OpenTelemetryConfig {
fn default() -> Self {
Self {
endpoint: None, export_interval: Duration::from_secs(10),
max_batch_size: 100,
async_export: true,
export_timeout: Duration::from_secs(5),
service_name: "burn-training".to_string(),
resource_attributes: Vec::new(),
}
}
}
impl OpenTelemetryConfig {
pub fn new(service_name: impl Into<String>) -> Self {
Self {
service_name: service_name.into(),
..Default::default()
}
}
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint = Some(endpoint.into());
self
}
pub fn with_export_interval(mut self, interval: Duration) -> Self {
self.export_interval = interval;
self
}
pub fn with_max_batch_size(mut self, size: usize) -> Self {
self.max_batch_size = size;
self
}
pub fn with_resource_attribute(
mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self {
self.resource_attributes.push((key.into(), value.into()));
self
}
pub fn with_async_export(mut self, async_export: bool) -> Self {
self.async_export = async_export;
self
}
pub fn with_export_timeout(mut self, timeout: Duration) -> Self {
self.export_timeout = timeout;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = OpenTelemetryConfig::default();
assert_eq!(config.service_name, "burn-training");
assert!(config.async_export);
assert_eq!(config.max_batch_size, 100);
}
#[test]
fn test_builder_pattern() {
let config = OpenTelemetryConfig::new("test-service")
.with_endpoint("http://localhost:4317")
.with_max_batch_size(50)
.with_resource_attribute("gpu.id", "0")
.with_async_export(false);
assert_eq!(config.service_name, "test-service");
assert_eq!(config.endpoint, Some("http://localhost:4317".to_string()));
assert_eq!(config.max_batch_size, 50);
assert!(!config.async_export);
assert_eq!(config.resource_attributes.len(), 1);
}
}