Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int test fix - alternate approach #2521

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opentelemetry-otlp/tests/integration_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
13 changes: 13 additions & 0 deletions opentelemetry-otlp/tests/integration_test/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
//!
#![cfg(unix)]

use anyhow::Ok;
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;
Expand Down Expand Up @@ -125,6 +127,17 @@ 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) {
let _ = OpenOptions::new()
.write(true)
.truncate(true)
.open(file_path); // ignore result, as file may not exist
}

///
/// Shuts down our collector container. This should be run as part of each test
/// suite shutting down!
Expand Down
20 changes: 10 additions & 10 deletions opentelemetry-otlp/tests/integration_test/tests/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -114,15 +115,14 @@ mod logtests {
init_logs()
})?;

info!("LoggerProvider created");
let layer = OpenTelemetryTracingBridge::new(&logger_provider);
let subscriber = tracing_subscriber::registry().with(layer);
{
let _guard = tracing::subscriber::set_default(subscriber);
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(())
Expand Down
Loading