mockforge_tracing/
tracer.rs

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