Skip to content

Commit 3d56a4f

Browse files
committed
initial commit
1 parent a719d0d commit 3d56a4f

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

opentelemetry-otlp/tests/integration_test/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ anyhow = "1.0.94"
1616
ctor = "0.2.9"
1717
tracing-subscriber = { workspace = true, features = ["env-filter","registry", "std", "fmt"] }
1818
tracing = {workspace = true}
19+
serial_test = "3.2.0"
1920

2021
[target.'cfg(unix)'.dependencies]
2122
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}

opentelemetry-otlp/tests/integration_test/src/test_utils.rs

+20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use anyhow::Result;
2222
use opentelemetry::{otel_debug, otel_info};
2323
use std::fs;
2424
use std::fs::File;
25+
use std::fs::OpenOptions;
2526
use std::os::unix::fs::PermissionsExt;
2627
use std::sync::{Arc, Mutex, Once, OnceLock};
2728
use testcontainers::core::wait::HttpWaitStrategy;
@@ -125,6 +126,25 @@ fn upsert_empty_file(path: &str) -> File {
125126
file
126127
}
127128

129+
/// Cleans up file specificed as argument by truncating its content.
130+
///
131+
/// This function is meant to cleanup the generated json file before a test starts,
132+
/// preventing entries from previous tests from interfering with the current test's results.
133+
pub fn cleanup_logs_file(file_path: &str) -> Result<()> {
134+
let file = OpenOptions::new()
135+
.write(true)
136+
.truncate(true)
137+
.open(file_path);
138+
match file {
139+
Ok(_) => Ok(()),
140+
Err(err) => Err(anyhow::anyhow!(
141+
"Failed to clean up file '{}': {:?}",
142+
file_path,
143+
err
144+
)),
145+
}
146+
}
147+
128148
///
129149
/// Shuts down our collector container. This should be run as part of each test
130150
/// suite shutting down!

opentelemetry-otlp/tests/integration_test/tests/logs.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod logtests {
3939
use super::*;
4040
use integration_test_runner::logs_asserter::{read_logs_from_json, LogsAsserter};
4141
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
42-
use std::{fs::File, time::Duration};
42+
use std::fs::File;
4343
use tracing::info;
4444
use tracing_subscriber::layer::SubscriberExt;
4545

@@ -70,9 +70,11 @@ mod logtests {
7070
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
7171
#[cfg(not(feature = "hyper-client"))]
7272
#[cfg(not(feature = "reqwest-client"))]
73+
#[serial_test::serial]
7374
pub async fn test_logs() -> Result<()> {
74-
// Make sure the container is running
75+
test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test
7576

77+
// Make sure the container is running
7678
use integration_test_runner::test_utils;
7779
use opentelemetry_appender_tracing::layer;
7880
use tracing::info;
@@ -88,21 +90,20 @@ mod logtests {
8890
let _guard = tracing::subscriber::set_default(subscriber);
8991
info!(target: "my-target", "hello from {}. My price is {}.", "banana", 2.99);
9092
}
91-
// TODO: remove below wait before calling logger_provider.shutdown()
92-
// tokio::time::sleep(Duration::from_secs(10)).await;
93-
let _ = logger_provider.shutdown();
94-
95-
tokio::time::sleep(Duration::from_secs(10)).await;
9693

94+
let _ = logger_provider.shutdown();
9795
assert_logs_results(test_utils::LOGS_FILE, "expected/logs.json")?;
9896

9997
Ok(())
10098
}
10199

102-
#[ignore = "TODO: [Fix Me] Failing on CI. Needs to be investigated and resolved."]
100+
//#[ignore = "TODO: [Fix Me] Failing on CI. Needs to be investigated and resolved."]
103101
#[test]
102+
#[serial_test::serial]
104103
#[cfg(any(feature = "tonic-client", feature = "reqwest-blocking-client"))]
105104
pub fn logs_batch_non_tokio_main() -> Result<()> {
105+
test_utils::cleanup_logs_file("./actual/logs.json")?; // Ensure logs.json is empty before the test
106+
106107
// Initialize the logger provider inside a tokio runtime
107108
// as this allows tonic client to capture the runtime,
108109
// but actual export occurs from the dedicated std::thread
@@ -114,15 +115,14 @@ mod logtests {
114115
init_logs()
115116
})?;
116117

117-
info!("LoggerProvider created");
118118
let layer = OpenTelemetryTracingBridge::new(&logger_provider);
119119
let subscriber = tracing_subscriber::registry().with(layer);
120120
{
121121
let _guard = tracing::subscriber::set_default(subscriber);
122122
info!(target: "my-target", "hello from {}. My price is {}.", "banana", 2.99);
123123
}
124124
let _ = logger_provider.shutdown();
125-
// tokio::time::sleep(Duration::from_secs(10)).await;
125+
126126
assert_logs_results(test_utils::LOGS_FILE, "expected/logs.json")?;
127127

128128
Ok(())

0 commit comments

Comments
 (0)