Skip to main content

barbacane_telemetry/
config.rs

1//! Telemetry configuration.
2
3/// Log output format.
4#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5pub enum LogFormat {
6    /// Structured JSON output (production).
7    #[default]
8    Json,
9    /// Human-readable pretty output (development).
10    Pretty,
11}
12
13impl LogFormat {
14    /// Parse from string.
15    pub fn parse(s: &str) -> Option<Self> {
16        match s.to_lowercase().as_str() {
17            "json" => Some(Self::Json),
18            "pretty" => Some(Self::Pretty),
19            _ => None,
20        }
21    }
22}
23
24/// OTLP export protocol.
25#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
26pub enum OtlpProtocol {
27    /// gRPC protocol (default).
28    #[default]
29    Grpc,
30    /// HTTP/protobuf protocol.
31    Http,
32}
33
34impl OtlpProtocol {
35    /// Parse from string.
36    pub fn parse(s: &str) -> Option<Self> {
37        match s.to_lowercase().as_str() {
38            "grpc" => Some(Self::Grpc),
39            "http" => Some(Self::Http),
40            _ => None,
41        }
42    }
43}
44
45/// Telemetry configuration.
46#[derive(Debug, Clone)]
47pub struct TelemetryConfig {
48    /// Service name for telemetry (default: "barbacane").
49    pub service_name: String,
50
51    /// Log level filter (default: "info").
52    pub log_level: String,
53
54    /// Log output format.
55    pub log_format: LogFormat,
56
57    /// OTLP endpoint for trace/metric export (optional).
58    /// If not set, OTLP export is disabled.
59    pub otlp_endpoint: Option<String>,
60
61    /// OTLP protocol to use.
62    pub otlp_protocol: OtlpProtocol,
63
64    /// Additional OTLP headers (e.g., for authentication).
65    pub otlp_headers: Vec<(String, String)>,
66
67    /// Global trace sampling rate (0.0 to 1.0, default: 1.0).
68    pub trace_sampling: f64,
69
70    /// Artifact hash for span attributes (set at runtime).
71    pub artifact_hash: Option<String>,
72}
73
74impl Default for TelemetryConfig {
75    fn default() -> Self {
76        Self {
77            service_name: "barbacane".to_string(),
78            log_level: "info".to_string(),
79            log_format: LogFormat::Json,
80            otlp_endpoint: None,
81            otlp_protocol: OtlpProtocol::Grpc,
82            otlp_headers: Vec::new(),
83            trace_sampling: 1.0,
84            artifact_hash: None,
85        }
86    }
87}
88
89impl TelemetryConfig {
90    /// Create a new telemetry config with default values.
91    pub fn new() -> Self {
92        Self::default()
93    }
94
95    /// Set the service name.
96    pub fn with_service_name(mut self, name: impl Into<String>) -> Self {
97        self.service_name = name.into();
98        self
99    }
100
101    /// Set the log level.
102    pub fn with_log_level(mut self, level: impl Into<String>) -> Self {
103        self.log_level = level.into();
104        self
105    }
106
107    /// Set the log format.
108    pub fn with_log_format(mut self, format: LogFormat) -> Self {
109        self.log_format = format;
110        self
111    }
112
113    /// Set the OTLP endpoint.
114    pub fn with_otlp_endpoint(mut self, endpoint: impl Into<String>) -> Self {
115        self.otlp_endpoint = Some(endpoint.into());
116        self
117    }
118
119    /// Set the OTLP protocol.
120    pub fn with_otlp_protocol(mut self, protocol: OtlpProtocol) -> Self {
121        self.otlp_protocol = protocol;
122        self
123    }
124
125    /// Set the trace sampling rate.
126    pub fn with_trace_sampling(mut self, rate: f64) -> Self {
127        self.trace_sampling = rate.clamp(0.0, 1.0);
128        self
129    }
130
131    /// Set the artifact hash.
132    pub fn with_artifact_hash(mut self, hash: impl Into<String>) -> Self {
133        self.artifact_hash = Some(hash.into());
134        self
135    }
136}