use crate::logging::LoggingConfig;
#[derive(Debug, Clone)]
pub struct OtelTracingConfig {
pub service_name: String,
pub environment: String,
pub jaeger_endpoint: Option<String>,
pub otlp_endpoint: Option<String>,
pub protocol: String,
pub sampling_rate: f64,
}
impl Default for OtelTracingConfig {
fn default() -> Self {
Self {
service_name: "mockforge".to_string(),
environment: "development".to_string(),
jaeger_endpoint: Some("http://localhost:14268/api/traces".to_string()),
otlp_endpoint: Some("http://localhost:4317".to_string()),
protocol: "grpc".to_string(),
sampling_rate: 1.0,
}
}
}
#[cfg(feature = "opentelemetry")]
pub fn init_with_otel(
logging_config: LoggingConfig,
tracing_config: OtelTracingConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
#[cfg(feature = "mockforge-tracing")]
{
use mockforge_tracing::{init_tracer, ExporterType, TracingConfig};
let exporter_type = match tracing_config.protocol.as_str() {
"grpc" | "http" if tracing_config.otlp_endpoint.is_some() => ExporterType::Otlp,
_ => ExporterType::Jaeger, };
let tracing_cfg = TracingConfig {
service_name: tracing_config.service_name.clone(),
service_version: None, environment: tracing_config.environment.clone(),
jaeger_endpoint: tracing_config.jaeger_endpoint.clone(),
otlp_endpoint: tracing_config.otlp_endpoint.clone(),
exporter_type,
sampling_rate: tracing_config.sampling_rate,
};
match init_tracer(tracing_cfg) {
Ok(_) => {
tracing::info!("OpenTelemetry tracing initialized successfully");
}
Err(e) => {
tracing::warn!(
"Failed to initialize OpenTelemetry tracing: {}. Using logging only.",
e
);
}
}
}
#[cfg(not(feature = "mockforge-tracing"))]
{
tracing::warn!("OpenTelemetry feature enabled but mockforge-tracing crate not available. Using logging only.");
}
crate::logging::init_logging(logging_config)?;
Ok(())
}
#[cfg(feature = "opentelemetry")]
pub fn shutdown_otel() {
#[cfg(feature = "mockforge-tracing")]
{
use mockforge_tracing::shutdown_tracer;
shutdown_tracer();
tracing::info!("OpenTelemetry tracer shut down");
}
#[cfg(not(feature = "mockforge-tracing"))]
{
tracing::debug!("OpenTelemetry shutdown called but mockforge-tracing not available");
}
}
#[cfg(not(feature = "opentelemetry"))]
pub fn init_with_otel(
logging_config: LoggingConfig,
_tracing_config: OtelTracingConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
tracing::warn!("OpenTelemetry feature not enabled, using standard logging");
crate::logging::init_logging(logging_config)?;
Ok(())
}
#[cfg(not(feature = "opentelemetry"))]
pub fn shutdown_otel() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_otel_config() {
let config = OtelTracingConfig::default();
assert_eq!(config.service_name, "mockforge");
assert_eq!(config.environment, "development");
assert_eq!(config.sampling_rate, 1.0);
assert_eq!(config.protocol, "grpc");
}
#[test]
fn test_custom_otel_config() {
let config = OtelTracingConfig {
service_name: "test-service".to_string(),
environment: "production".to_string(),
jaeger_endpoint: Some("http://jaeger:14268/api/traces".to_string()),
otlp_endpoint: Some("http://otel:4317".to_string()),
protocol: "http".to_string(),
sampling_rate: 0.5,
};
assert_eq!(config.service_name, "test-service");
assert_eq!(config.environment, "production");
assert_eq!(config.sampling_rate, 0.5);
}
}