Skip to content

Commit 4db2ce4

Browse files
committed
.
1 parent ff61111 commit 4db2ce4

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
//!
1919
#![cfg(unix)]
2020

21-
use std::collections::HashMap;
21+
use anyhow::Result;
22+
use opentelemetry::{otel_debug, otel_info};
2223
use std::fs;
2324
use std::fs::File;
2425
use std::os::unix::fs::PermissionsExt;
2526
use std::sync::{Arc, Mutex, Once, OnceLock};
26-
use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::AsyncRunner, ContainerAsync, GenericImage, ImageExt};
27-
use testcontainers::core::{ContainerPort, Mount};
2827
use testcontainers::core::wait::HttpWaitStrategy;
28+
use testcontainers::core::{ContainerPort, Mount};
29+
use testcontainers::{
30+
core::WaitFor,
31+
runners::AsyncRunner,
32+
ContainerAsync, GenericImage, ImageExt,
33+
};
2934
use tracing_subscriber::FmtSubscriber;
30-
use opentelemetry::{otel_debug, otel_info};
31-
use anyhow::Result;
3235

3336
// Static references for container management
3437
static COLLECTOR_ARC: OnceLock<Mutex<Option<Arc<ContainerAsync<GenericImage>>>>> = OnceLock::new();
@@ -71,13 +74,26 @@ pub async fn start_collector_container() -> Result<()> {
7174
.with_wait_for(WaitFor::http(
7275
HttpWaitStrategy::new("/")
7376
.with_expected_status_code(404u16)
74-
.with_port(ContainerPort::Tcp(4318))))
77+
.with_port(ContainerPort::Tcp(4318)),
78+
))
7579
.with_mapped_port(4317, ContainerPort::Tcp(4317))
7680
.with_mapped_port(4318, ContainerPort::Tcp(4318))
77-
.with_mount(Mount::bind_mount(fs::canonicalize("./otel-collector-config.yaml")?.to_string_lossy(), "/etc/otelcol/config.yaml"))
78-
.with_mount(Mount::bind_mount(fs::canonicalize("./actual/logs.json")?.to_string_lossy(), "/testresults/logs.json"))
79-
.with_mount(Mount::bind_mount(fs::canonicalize("./actual/metrics.json")?.to_string_lossy(), "/testresults/metrics.json"))
80-
.with_mount(Mount::bind_mount(fs::canonicalize("./actual/traces.json")?.to_string_lossy(), "/testresults/traces.json"))
81+
.with_mount(Mount::bind_mount(
82+
fs::canonicalize("./otel-collector-config.yaml")?.to_string_lossy(),
83+
"/etc/otelcol/config.yaml",
84+
))
85+
.with_mount(Mount::bind_mount(
86+
fs::canonicalize("./actual/logs.json")?.to_string_lossy(),
87+
"/testresults/logs.json",
88+
))
89+
.with_mount(Mount::bind_mount(
90+
fs::canonicalize("./actual/metrics.json")?.to_string_lossy(),
91+
"/testresults/metrics.json",
92+
))
93+
.with_mount(Mount::bind_mount(
94+
fs::canonicalize("./actual/traces.json")?.to_string_lossy(),
95+
"/testresults/traces.json",
96+
))
8197
.start()
8298
.await?;
8399

@@ -112,12 +128,24 @@ fn upsert_empty_file(path: &str) -> File {
112128
/// suite shutting down!
113129
///
114130
pub fn stop_collector_container() {
131+
// This is a bit heinous. We get called after tokio has shutdown, but testcontainers
132+
// needs an async runtime to shutdown, so we create a temporary new one just for this
133+
// purpose.
134+
// https://github.com/testcontainers/testcontainers-rs/issues/707
115135
otel_debug!(name: "stop_collector_container");
136+
tokio::runtime::Runtime::new()
137+
.unwrap()
138+
.block_on(stop_it())
139+
.expect("Couldn't create runtime to shutdown");
140+
}
141+
142+
async fn stop_it() -> Result<()> {
116143
if let Some(container) = COLLECTOR_ARC
117144
.get()
118145
.and_then(|arc_lock| arc_lock.lock().unwrap().take())
119146
{
120-
container.stop();
147+
container.stop().await?;
121148
otel_debug!(name: "stop_collector_container::stopped");
122149
}
150+
Ok(())
123151
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result<Value> {
137137
///
138138
/// Performs setup for metrics tests
139139
///
140-
async fn setup_metrics_test() {
140+
async fn setup_metrics_test() -> Result<()> {
141141
// Make sure the collector container is running
142142
start_collector_container().await?;
143143

@@ -152,6 +152,8 @@ async fn setup_metrics_test() {
152152

153153
// Truncate results
154154
_ = File::create(RESULT_PATH).expect("it's good");
155+
156+
Ok(())
155157
}
156158

157159
///

0 commit comments

Comments
 (0)