barbacane_telemetry/
config.rs1#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5pub enum LogFormat {
6 #[default]
8 Json,
9 Pretty,
11}
12
13impl LogFormat {
14 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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
26pub enum OtlpProtocol {
27 #[default]
29 Grpc,
30 Http,
32}
33
34impl OtlpProtocol {
35 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#[derive(Debug, Clone)]
47pub struct TelemetryConfig {
48 pub service_name: String,
50
51 pub log_level: String,
53
54 pub log_format: LogFormat,
56
57 pub otlp_endpoint: Option<String>,
60
61 pub otlp_protocol: OtlpProtocol,
63
64 pub otlp_headers: Vec<(String, String)>,
66
67 pub trace_sampling: f64,
69
70 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 pub fn new() -> Self {
92 Self::default()
93 }
94
95 pub fn with_service_name(mut self, name: impl Into<String>) -> Self {
97 self.service_name = name.into();
98 self
99 }
100
101 pub fn with_log_level(mut self, level: impl Into<String>) -> Self {
103 self.log_level = level.into();
104 self
105 }
106
107 pub fn with_log_format(mut self, format: LogFormat) -> Self {
109 self.log_format = format;
110 self
111 }
112
113 pub fn with_otlp_endpoint(mut self, endpoint: impl Into<String>) -> Self {
115 self.otlp_endpoint = Some(endpoint.into());
116 self
117 }
118
119 pub fn with_otlp_protocol(mut self, protocol: OtlpProtocol) -> Self {
121 self.otlp_protocol = protocol;
122 self
123 }
124
125 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 pub fn with_artifact_hash(mut self, hash: impl Into<String>) -> Self {
133 self.artifact_hash = Some(hash.into());
134 self
135 }
136}