mockforge_tracing/
tracer.rs1use crate::exporter::ExporterType;
4use opentelemetry::global;
5use opentelemetry_otlp::WithExportConfig;
6use opentelemetry_sdk::trace::TracerProvider;
7use opentelemetry_sdk::Resource;
8use std::error::Error;
9use std::time::Duration;
10
11#[derive(Debug, Clone)]
13pub struct TracingConfig {
14 pub service_name: String,
16 pub exporter_type: ExporterType,
18 pub jaeger_endpoint: Option<String>,
20 pub otlp_endpoint: Option<String>,
22 pub sampling_rate: f64,
24 pub environment: String,
26 pub service_version: Option<String>,
28}
29
30impl Default for TracingConfig {
31 fn default() -> Self {
32 Self {
33 service_name: "mockforge".to_string(),
34 exporter_type: ExporterType::Jaeger,
35 jaeger_endpoint: Some("http://localhost:14268/api/traces".to_string()),
36 otlp_endpoint: None,
37 sampling_rate: 1.0,
38 environment: "development".to_string(),
39 service_version: None,
40 }
41 }
42}
43
44impl TracingConfig {
45 pub fn with_jaeger(service_name: String, endpoint: String) -> Self {
47 Self {
48 service_name,
49 exporter_type: ExporterType::Jaeger,
50 jaeger_endpoint: Some(endpoint),
51 otlp_endpoint: None,
52 ..Default::default()
53 }
54 }
55
56 pub fn with_otlp(service_name: String, endpoint: String) -> Self {
58 Self {
59 service_name,
60 exporter_type: ExporterType::Otlp,
61 jaeger_endpoint: None,
62 otlp_endpoint: Some(endpoint),
63 ..Default::default()
64 }
65 }
66
67 pub fn with_sampling_rate(mut self, rate: f64) -> Self {
69 self.sampling_rate = rate;
70 self
71 }
72
73 pub fn with_environment(mut self, env: String) -> Self {
75 self.environment = env;
76 self
77 }
78
79 pub fn with_service_version(mut self, version: String) -> Self {
81 self.service_version = Some(version);
82 self
83 }
84}
85
86pub fn init_tracer(
88 config: TracingConfig,
89) -> Result<global::BoxedTracer, Box<dyn Error + Send + Sync>> {
90 match config.exporter_type {
91 ExporterType::Jaeger => init_jaeger_tracer(config),
92 ExporterType::Otlp => init_otlp_tracer(config),
93 }
94}
95
96fn init_jaeger_tracer(
98 config: TracingConfig,
99) -> Result<global::BoxedTracer, Box<dyn Error + Send + Sync>> {
100 let endpoint = config.jaeger_endpoint.ok_or("Jaeger endpoint not configured")?;
101
102 #[allow(deprecated)]
105 let _tracer_provider = opentelemetry_jaeger::new_agent_pipeline()
106 .with_service_name(&config.service_name)
107 .with_endpoint(&endpoint)
108 .with_trace_config(opentelemetry_sdk::trace::Config::default().with_sampler(
109 opentelemetry_sdk::trace::Sampler::TraceIdRatioBased(config.sampling_rate),
110 ))
111 .install_simple()?;
112
113 let tracer = global::tracer("mockforge");
115 Ok(tracer)
116}
117
118fn init_otlp_tracer(
120 config: TracingConfig,
121) -> Result<global::BoxedTracer, Box<dyn Error + Send + Sync>> {
122 let endpoint = config.otlp_endpoint.ok_or("OTLP endpoint not configured")?;
123
124 let resource = Resource::default();
127
128 let mut exporter_builder = opentelemetry_otlp::TonicExporterBuilder::default();
131 exporter_builder = exporter_builder.with_endpoint(endpoint);
132 exporter_builder = exporter_builder.with_timeout(Duration::from_secs(10));
133
134 let exporter = exporter_builder.build_span_exporter()?;
136
137 let tracer_provider = TracerProvider::builder()
139 .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
140 .with_config(
141 opentelemetry_sdk::trace::Config::default()
142 .with_resource(resource)
143 .with_sampler(opentelemetry_sdk::trace::Sampler::TraceIdRatioBased(
144 config.sampling_rate,
145 )),
146 )
147 .build();
148
149 global::set_tracer_provider(tracer_provider.clone());
151
152 let tracer = global::tracer("mockforge");
154
155 Ok(tracer)
156}
157
158pub fn shutdown_tracer() {
160 global::shutdown_tracer_provider();
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn test_default_config() {
169 let config = TracingConfig::default();
170 assert_eq!(config.service_name, "mockforge");
171 assert_eq!(config.sampling_rate, 1.0);
172 assert_eq!(config.environment, "development");
173 assert_eq!(config.exporter_type, ExporterType::Jaeger);
174 assert!(config.jaeger_endpoint.is_some());
175 assert!(config.otlp_endpoint.is_none());
176 }
177
178 #[test]
179 fn test_jaeger_config() {
180 let config = TracingConfig::with_jaeger(
181 "test-service".to_string(),
182 "http://custom:14268/api/traces".to_string(),
183 )
184 .with_sampling_rate(0.5)
185 .with_environment("staging".to_string())
186 .with_service_version("1.0.0".to_string());
187
188 assert_eq!(config.service_name, "test-service");
189 assert_eq!(config.exporter_type, ExporterType::Jaeger);
190 assert_eq!(config.jaeger_endpoint, Some("http://custom:14268/api/traces".to_string()));
191 assert_eq!(config.sampling_rate, 0.5);
192 assert_eq!(config.environment, "staging");
193 assert_eq!(config.service_version, Some("1.0.0".to_string()));
194 }
195
196 #[test]
197 fn test_otlp_config() {
198 let config = TracingConfig::with_otlp(
199 "test-service".to_string(),
200 "http://otel-collector:4317".to_string(),
201 )
202 .with_sampling_rate(0.8)
203 .with_environment("production".to_string())
204 .with_service_version("2.0.0".to_string());
205
206 assert_eq!(config.service_name, "test-service");
207 assert_eq!(config.exporter_type, ExporterType::Otlp);
208 assert_eq!(config.otlp_endpoint, Some("http://otel-collector:4317".to_string()));
209 assert!(config.jaeger_endpoint.is_none());
210 assert_eq!(config.sampling_rate, 0.8);
211 assert_eq!(config.environment, "production");
212 assert_eq!(config.service_version, Some("2.0.0".to_string()));
213 }
214}