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<opentelemetry::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<opentelemetry::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 (this sets it as global)
103    // opentelemetry-jaeger 0.21 uses a different runtime API
104    let _tracer_provider = opentelemetry_jaeger::new_agent_pipeline()
105        .with_service_name(&config.service_name)
106        .with_endpoint(&endpoint)
107        .install_simple()?;
108
109    // Get the tracer from the global provider
110    let tracer = opentelemetry::global::tracer("mockforge");
111    Ok(tracer)
112}
113
114/// Initialize OTLP tracer
115fn init_otlp_tracer(
116    config: TracingConfig,
117) -> Result<opentelemetry::global::BoxedTracer, Box<dyn Error + Send + Sync>> {
118    let endpoint = config.otlp_endpoint.ok_or("OTLP endpoint not configured")?;
119
120    // Build resource attributes
121    // Note: In opentelemetry_sdk 0.22, Resource creation API
122    let resource = Resource::default();
123
124    // Create OTLP exporter with gRPC protocol (opentelemetry-otlp 0.22 API)
125    // Build the exporter configuration
126    let mut exporter_builder = opentelemetry_otlp::TonicExporterBuilder::default();
127    exporter_builder = exporter_builder.with_endpoint(endpoint);
128    exporter_builder = exporter_builder.with_timeout(Duration::from_secs(10));
129
130    // Build the exporter
131    let exporter = exporter_builder.build_span_exporter()?;
132
133    // Build tracer provider using opentelemetry_sdk directly
134    let tracer_provider = TracerProvider::builder()
135        .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
136        .with_config(
137            opentelemetry_sdk::trace::Config::default()
138                .with_resource(resource)
139                .with_sampler(opentelemetry_sdk::trace::Sampler::TraceIdRatioBased(
140                    config.sampling_rate,
141                )),
142        )
143        .build();
144
145    // Set the tracer provider as global
146    opentelemetry::global::set_tracer_provider(tracer_provider.clone());
147
148    // Get the tracer from the global provider
149    let tracer = opentelemetry::global::tracer("mockforge");
150
151    Ok(tracer)
152}
153
154/// Shutdown the tracer and flush pending spans
155pub fn shutdown_tracer() {
156    global::shutdown_tracer_provider();
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_default_config() {
165        let config = TracingConfig::default();
166        assert_eq!(config.service_name, "mockforge");
167        assert_eq!(config.sampling_rate, 1.0);
168        assert_eq!(config.environment, "development");
169        assert_eq!(config.exporter_type, ExporterType::Jaeger);
170        assert!(config.jaeger_endpoint.is_some());
171        assert!(config.otlp_endpoint.is_none());
172    }
173
174    #[test]
175    fn test_jaeger_config() {
176        let config = TracingConfig::with_jaeger(
177            "test-service".to_string(),
178            "http://custom:14268/api/traces".to_string(),
179        )
180        .with_sampling_rate(0.5)
181        .with_environment("staging".to_string())
182        .with_service_version("1.0.0".to_string());
183
184        assert_eq!(config.service_name, "test-service");
185        assert_eq!(config.exporter_type, ExporterType::Jaeger);
186        assert_eq!(config.jaeger_endpoint, Some("http://custom:14268/api/traces".to_string()));
187        assert_eq!(config.sampling_rate, 0.5);
188        assert_eq!(config.environment, "staging");
189        assert_eq!(config.service_version, Some("1.0.0".to_string()));
190    }
191
192    #[test]
193    fn test_otlp_config() {
194        let config = TracingConfig::with_otlp(
195            "test-service".to_string(),
196            "http://otel-collector:4317".to_string(),
197        )
198        .with_sampling_rate(0.8)
199        .with_environment("production".to_string())
200        .with_service_version("2.0.0".to_string());
201
202        assert_eq!(config.service_name, "test-service");
203        assert_eq!(config.exporter_type, ExporterType::Otlp);
204        assert_eq!(config.otlp_endpoint, Some("http://otel-collector:4317".to_string()));
205        assert!(config.jaeger_endpoint.is_none());
206        assert_eq!(config.sampling_rate, 0.8);
207        assert_eq!(config.environment, "production");
208        assert_eq!(config.service_version, Some("2.0.0".to_string()));
209    }
210}