#[cfg(test)]
mod tests {
use gitops_operator::telemetry::{get_subscriber, init_subscriber, resource};
use opentelemetry::global;
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
use opentelemetry_semantic_conventions::resource::SERVICE_VERSION;
use std::sync::Once;
use std::time::Duration;
use tokio::time::timeout;
static INIT: Once = Once::new();
fn init_test_telemetry() {
INIT.call_once(|| {
let subscriber = get_subscriber("test-telemetry".into(), "debug".into());
init_subscriber(subscriber);
});
}
async fn setup_test_environment() {
opentelemetry::global::shutdown_tracer_provider();
tokio::time::sleep(Duration::from_millis(100)).await;
match timeout(Duration::from_secs(5), async {
init_test_telemetry();
})
.await
{
Ok(_) => (),
Err(_) => panic!("Timeout while setting up subscriber"),
}
}
async fn cleanup_test_environment() {
match timeout(Duration::from_secs(5), async {
opentelemetry::global::shutdown_tracer_provider();
})
.await
{
Ok(_) => (),
Err(_) => eprintln!(
"Warning: Cleanup timeout - some resources might not be properly released"
),
}
}
#[tokio::test(flavor = "multi_thread")]
async fn test_resource_creation() {
let resource = resource();
let attributes = resource.iter().collect::<Vec<_>>();
assert!(attributes.iter().any(|kv| kv.0.as_str() == SERVICE_NAME));
assert!(attributes.iter().any(|kv| kv.0.as_str() == SERVICE_VERSION));
}
#[tokio::test(flavor = "multi_thread")]
async fn test_get_subscriber_creates_valid_subscriber() {
setup_test_environment().await;
cleanup_test_environment().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn test_get_subscriber_with_env_filter() {
std::env::set_var("RUST_LOG", "debug");
setup_test_environment().await;
let span = tracing::info_span!("test_span");
let _guard = span.enter();
tracing::debug!("This is a debug message");
tracing::info!(event = "test_event", "Testing telemetry configuration");
cleanup_test_environment().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn test_subscriber_with_metrics() {
setup_test_environment().await;
let meter = global::meter("test-meter");
let counter = meter
.u64_counter("test_counter")
.with_description("A test counter")
.build();
counter.add(1, &[]);
cleanup_test_environment().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn test_init_subscriber() {
setup_test_environment().await;
let span = tracing::info_span!("test_span");
assert!(!span.is_disabled());
cleanup_test_environment().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn test_telemetry_layer_configuration() {
setup_test_environment().await;
let span = tracing::info_span!(
"test_operation",
service.name = "test-telemetry",
service.version = env!("CARGO_PKG_VERSION")
);
let _guard = span.enter();
tracing::info!(event = "test_event", "Testing telemetry configuration");
cleanup_test_environment().await;
}
}