Skip to main content

mockforge_tracing/
tracer.rs

1//! OpenTelemetry tracer initialization and configuration
2
3use 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/// Tracing configuration
12#[derive(Debug, Clone)]
13pub struct TracingConfig {
14    /// Service name for traces
15    pub service_name: String,
16    /// Exporter type (Jaeger or OTLP)
17    pub exporter_type: ExporterType,
18    /// Jaeger endpoint (e.g., "http://localhost:14268/api/traces")
19    pub jaeger_endpoint: Option<String>,
20    /// OTLP endpoint (e.g., "http://localhost:4317")
21    pub otlp_endpoint: Option<String>,
22    /// Sampling rate (0.0 to 1.0)
23    pub sampling_rate: f64,
24    /// Environment (e.g., "development", "production")
25    pub environment: String,
26    /// Service version
27    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    /// Create config for Jaeger exporter
46    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    /// Create config for OTLP exporter
57    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    /// Set sampling rate
68    pub fn with_sampling_rate(mut self, rate: f64) -> Self {
69        self.sampling_rate = rate;
70        self
71    }
72
73    /// Set environment
74    pub fn with_environment(mut self, env: String) -> Self {
75        self.environment = env;
76        self
77    }
78
79    /// Set service version
80    pub fn with_service_version(mut self, version: String) -> Self {
81        self.service_version = Some(version);
82        self
83    }
84}
85
86/// Initialize the OpenTelemetry tracer
87pub 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
96/// Initialize Jaeger tracer
97fn 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    // Install the tracer provider with sampling rate configuration
103    // opentelemetry-jaeger 0.21 uses a different runtime API
104    #[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    // Get the tracer from the global provider
114    let tracer = global::tracer("mockforge");
115    Ok(tracer)
116}
117
118/// Initialize OTLP tracer
119fn 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    // Build resource attributes
125    // Note: In opentelemetry_sdk 0.22, Resource creation API
126    let resource = Resource::default();
127
128    // Create OTLP exporter with gRPC protocol (opentelemetry-otlp 0.22 API)
129    // Build the exporter configuration
130    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    // Build the exporter
135    let exporter = exporter_builder.build_span_exporter()?;
136
137    // Build tracer provider using opentelemetry_sdk directly
138    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    // Set the tracer provider as global
150    global::set_tracer_provider(tracer_provider.clone());
151
152    // Get the tracer from the global provider
153    let tracer = global::tracer("mockforge");
154
155    Ok(tracer)
156}
157
158/// Shutdown the tracer and flush pending spans
159pub 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}