From 3d56a4f584eb76cc5fc970fa9016c64a08f3f488 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 16 Jan 2025 23:17:59 +0000 Subject: [PATCH 1/2] initial commit --- .../tests/integration_test/Cargo.toml | 1 + .../tests/integration_test/src/test_utils.rs | 20 +++++++++++++++++++ .../tests/integration_test/tests/logs.rs | 20 +++++++++---------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/opentelemetry-otlp/tests/integration_test/Cargo.toml b/opentelemetry-otlp/tests/integration_test/Cargo.toml index 5314c1fe61..eed36f5e4b 100644 --- a/opentelemetry-otlp/tests/integration_test/Cargo.toml +++ b/opentelemetry-otlp/tests/integration_test/Cargo.toml @@ -16,6 +16,7 @@ anyhow = "1.0.94" ctor = "0.2.9" tracing-subscriber = { workspace = true, features = ["env-filter","registry", "std", "fmt"] } tracing = {workspace = true} +serial_test = "3.2.0" [target.'cfg(unix)'.dependencies] opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false} diff --git a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs index bd62674868..f3f50b2171 100644 --- a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs +++ b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs @@ -22,6 +22,7 @@ use anyhow::Result; use opentelemetry::{otel_debug, otel_info}; use std::fs; use std::fs::File; +use std::fs::OpenOptions; use std::os::unix::fs::PermissionsExt; use std::sync::{Arc, Mutex, Once, OnceLock}; use testcontainers::core::wait::HttpWaitStrategy; @@ -125,6 +126,25 @@ fn upsert_empty_file(path: &str) -> File { file } +/// Cleans up file specificed as argument by truncating its content. +/// +/// This function is meant to cleanup the generated json file before a test starts, +/// preventing entries from previous tests from interfering with the current test's results. +pub fn cleanup_logs_file(file_path: &str) -> Result<()> { + let file = OpenOptions::new() + .write(true) + .truncate(true) + .open(file_path); + match file { + Ok(_) => Ok(()), + Err(err) => Err(anyhow::anyhow!( + "Failed to clean up file '{}': {:?}", + file_path, + err + )), + } +} + /// /// Shuts down our collector container. This should be run as part of each test /// suite shutting down! diff --git a/opentelemetry-otlp/tests/integration_test/tests/logs.rs b/opentelemetry-otlp/tests/integration_test/tests/logs.rs index af78ee9005..5a0b72c74d 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/logs.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/logs.rs @@ -39,7 +39,7 @@ mod logtests { use super::*; use integration_test_runner::logs_asserter::{read_logs_from_json, LogsAsserter}; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; - use std::{fs::File, time::Duration}; + use std::fs::File; use tracing::info; use tracing_subscriber::layer::SubscriberExt; @@ -70,9 +70,11 @@ mod logtests { #[tokio::test(flavor = "multi_thread", worker_threads = 4)] #[cfg(not(feature = "hyper-client"))] #[cfg(not(feature = "reqwest-client"))] + #[serial_test::serial] pub async fn test_logs() -> Result<()> { - // Make sure the container is running + test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test + // Make sure the container is running use integration_test_runner::test_utils; use opentelemetry_appender_tracing::layer; use tracing::info; @@ -88,21 +90,20 @@ mod logtests { let _guard = tracing::subscriber::set_default(subscriber); info!(target: "my-target", "hello from {}. My price is {}.", "banana", 2.99); } - // TODO: remove below wait before calling logger_provider.shutdown() - // tokio::time::sleep(Duration::from_secs(10)).await; - let _ = logger_provider.shutdown(); - - tokio::time::sleep(Duration::from_secs(10)).await; + let _ = logger_provider.shutdown(); assert_logs_results(test_utils::LOGS_FILE, "expected/logs.json")?; Ok(()) } - #[ignore = "TODO: [Fix Me] Failing on CI. Needs to be investigated and resolved."] + //#[ignore = "TODO: [Fix Me] Failing on CI. Needs to be investigated and resolved."] #[test] + #[serial_test::serial] #[cfg(any(feature = "tonic-client", feature = "reqwest-blocking-client"))] pub fn logs_batch_non_tokio_main() -> Result<()> { + test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test + // Initialize the logger provider inside a tokio runtime // as this allows tonic client to capture the runtime, // but actual export occurs from the dedicated std::thread @@ -114,7 +115,6 @@ mod logtests { init_logs() })?; - info!("LoggerProvider created"); let layer = OpenTelemetryTracingBridge::new(&logger_provider); let subscriber = tracing_subscriber::registry().with(layer); { @@ -122,7 +122,7 @@ mod logtests { info!(target: "my-target", "hello from {}. My price is {}.", "banana", 2.99); } let _ = logger_provider.shutdown(); - // tokio::time::sleep(Duration::from_secs(10)).await; + assert_logs_results(test_utils::LOGS_FILE, "expected/logs.json")?; Ok(()) From b2e897dca40a353c26dee8a1ddb55870178bc783 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 16 Jan 2025 23:29:17 +0000 Subject: [PATCH 2/2] ignore file truncate error --- .../tests/integration_test/src/test_utils.rs | 15 ++++----------- .../tests/integration_test/tests/logs.rs | 4 ++-- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs index f3f50b2171..3217cf2c08 100644 --- a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs +++ b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs @@ -18,6 +18,7 @@ //! #![cfg(unix)] +use anyhow::Ok; use anyhow::Result; use opentelemetry::{otel_debug, otel_info}; use std::fs; @@ -130,19 +131,11 @@ fn upsert_empty_file(path: &str) -> File { /// /// This function is meant to cleanup the generated json file before a test starts, /// preventing entries from previous tests from interfering with the current test's results. -pub fn cleanup_logs_file(file_path: &str) -> Result<()> { - let file = OpenOptions::new() +pub fn cleanup_logs_file(file_path: &str) { + let _ = OpenOptions::new() .write(true) .truncate(true) - .open(file_path); - match file { - Ok(_) => Ok(()), - Err(err) => Err(anyhow::anyhow!( - "Failed to clean up file '{}': {:?}", - file_path, - err - )), - } + .open(file_path); // ignore result, as file may not exist } /// diff --git a/opentelemetry-otlp/tests/integration_test/tests/logs.rs b/opentelemetry-otlp/tests/integration_test/tests/logs.rs index 5a0b72c74d..49d6afb2fe 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/logs.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/logs.rs @@ -72,7 +72,7 @@ mod logtests { #[cfg(not(feature = "reqwest-client"))] #[serial_test::serial] pub async fn test_logs() -> Result<()> { - test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test + test_utils::cleanup_logs_file("./actual/logs.json"); // Ensure logs.json is empty before the test // Make sure the container is running use integration_test_runner::test_utils; @@ -102,7 +102,7 @@ mod logtests { #[serial_test::serial] #[cfg(any(feature = "tonic-client", feature = "reqwest-blocking-client"))] pub fn logs_batch_non_tokio_main() -> Result<()> { - test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test + test_utils::cleanup_logs_file("./actual/logs.json"); // Ensure logs.json is empty before the test // Initialize the logger provider inside a tokio runtime // as this allows tonic client to capture the runtime,