1919#![ cfg( unix) ]
2020
2121use std:: collections:: HashMap ;
22+ use std:: fs;
2223use std:: fs:: File ;
2324use std:: os:: unix:: fs:: PermissionsExt ;
2425use std:: sync:: { Arc , Mutex , Once , OnceLock } ;
25- use testcontainers:: clients :: Cli ;
26- use testcontainers:: core:: { Port , WaitFor } ;
27- use testcontainers:: { Container , Image , RunnableImage } ;
26+ use testcontainers:: { core :: { IntoContainerPort , WaitFor } , runners :: AsyncRunner , ContainerAsync , GenericImage , ImageExt } ;
27+ use testcontainers:: core:: { ContainerPort , Mount } ;
28+ use testcontainers:: core :: wait :: HttpWaitStrategy ;
2829use tracing_subscriber:: FmtSubscriber ;
2930use opentelemetry:: { otel_debug, otel_info} ;
31+ use anyhow:: Result ;
3032
3133// Static references for container management
32- static COLLECTOR_ARC : OnceLock < Mutex < Option < Arc < Container < Collector > > > > > = OnceLock :: new ( ) ;
33- static DOCKER_CLIENT : OnceLock < Cli > = OnceLock :: new ( ) ;
34-
35- fn init_docker_client ( ) -> Cli {
36- Cli :: default ( )
37- }
34+ static COLLECTOR_ARC : OnceLock < Mutex < Option < Arc < ContainerAsync < GenericImage > > > > > = OnceLock :: new ( ) ;
3835
3936pub static METRICS_FILE : & str = "./actual/metrics.json" ;
4037pub static LOGS_FILE : & str = "./actual/logs.json" ;
@@ -54,10 +51,9 @@ fn init_tracing() {
5451 } ) ;
5552}
5653
57- pub async fn start_collector_container ( ) {
54+ pub async fn start_collector_container ( ) -> Result < ( ) > {
5855 init_tracing ( ) ;
5956
60- let docker = DOCKER_CLIENT . get_or_init ( init_docker_client) ;
6157 let mut arc_guard = COLLECTOR_ARC
6258 . get_or_init ( || Mutex :: new ( None ) )
6359 . lock ( )
@@ -71,24 +67,33 @@ pub async fn start_collector_container() {
7167 upsert_empty_file ( LOGS_FILE ) ;
7268
7369 // Start a new container
74- let image = Collector :: default ( ) ;
75- let runnable_image =
76- RunnableImage :: from ( image)
77- . with_mapped_port ( Port :: from ( ( 4317 , 4317 ) ) )
78- . with_mapped_port ( Port :: from ( ( 4318 , 4318 ) ) ) ;
70+ let container_instance = GenericImage :: new ( "otel/opentelemetry-collector" , "latest" )
71+ . with_wait_for ( WaitFor :: http (
72+ HttpWaitStrategy :: new ( "/" )
73+ . with_expected_status_code ( 404u16 )
74+ . with_port ( ContainerPort :: Tcp ( 4318 ) ) ) )
75+ . with_mapped_port ( 4317 , ContainerPort :: Tcp ( 4317 ) )
76+ . 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+ . start ( )
82+ . await ?;
7983
80- let container_instance = docker. run ( runnable_image) ;
8184 let container = Arc :: new ( container_instance) ;
8285 otel_debug ! (
8386 name: "Container started" ,
84- ports = format!( "{:?}" , container. ports( ) ) ) ;
87+ ports = format!( "{:?}" , container. ports( ) . await ) ) ;
8588
8689 // Give the container a second to stabilize
87- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 5 ) ) . await ;
90+ // tokio::time::sleep(std::time::Duration::from_secs(5)).await;
8891
8992 // Store the container in COLLECTOR_ARC
9093 * arc_guard = Some ( Arc :: clone ( & container) ) ;
9194 }
95+
96+ Ok ( ( ) )
9297}
9398
9499///
@@ -116,43 +121,3 @@ pub fn stop_collector_container() {
116121 otel_debug ! ( name: "stop_collector_container::stopped" ) ;
117122 }
118123}
119-
120- struct Collector {
121- volumes : HashMap < String , String > ,
122- }
123-
124- impl Image for Collector {
125- type Args = ( ) ;
126-
127- fn name ( & self ) -> String {
128- "otel/opentelemetry-collector" . to_string ( )
129- }
130-
131- fn tag ( & self ) -> String {
132- "latest" . to_string ( )
133- }
134-
135- fn ready_conditions ( & self ) -> Vec < WaitFor > {
136- vec ! [ WaitFor :: Nothing ]
137- }
138-
139- fn volumes ( & self ) -> Box < dyn Iterator < Item = ( & String , & String ) > + ' _ > {
140- Box :: new ( self . volumes . iter ( ) )
141- }
142- }
143-
144- impl Default for Collector {
145- fn default ( ) -> Self {
146- Collector {
147- volumes : HashMap :: from ( [
148- (
149- "./otel-collector-config.yaml" . into ( ) ,
150- "/etc/otelcol/config.yaml" . into ( ) ,
151- ) ,
152- ( TRACES_FILE . into ( ) , "/testresults/traces.json" . into ( ) ) ,
153- ( LOGS_FILE . into ( ) , "/testresults/logs.json" . into ( ) ) ,
154- ( METRICS_FILE . into ( ) , "/testresults/metrics.json" . into ( ) ) ,
155- ] ) ,
156- }
157- }
158- }
0 commit comments