2121use std:: collections:: HashMap ;
2222use std:: fs:: File ;
2323use std:: os:: unix:: fs:: PermissionsExt ;
24- use std:: sync:: { Arc , Mutex , OnceLock } ;
24+ use std:: sync:: { Arc , Mutex , Once , OnceLock } ;
2525use testcontainers:: clients:: Cli ;
2626use testcontainers:: core:: { Port , WaitFor } ;
2727use testcontainers:: { Container , Image , RunnableImage } ;
28+ use tracing_subscriber:: FmtSubscriber ;
29+ use opentelemetry:: { otel_debug, otel_info} ;
2830
2931// Static references for container management
3032static COLLECTOR_ARC : OnceLock < Mutex < Option < Arc < Container < Collector > > > > > = OnceLock :: new ( ) ;
@@ -38,7 +40,23 @@ pub static METRICS_FILE: &str = "./actual/metrics.json";
3840pub static LOGS_FILE : & str = "./actual/logs.json" ;
3941pub static TRACES_FILE : & str = "./actual/traces.json" ;
4042
43+ static INIT_TRACING : Once = Once :: new ( ) ;
44+
45+ fn init_tracing ( ) {
46+ INIT_TRACING . call_once ( || {
47+ let subscriber = FmtSubscriber :: builder ( )
48+ . with_max_level ( tracing:: Level :: DEBUG )
49+ . finish ( ) ;
50+
51+ tracing:: subscriber:: set_global_default ( subscriber)
52+ . expect ( "Failed to set tracing subscriber" ) ;
53+ otel_info ! ( name: "init_tracing" ) ;
54+ } ) ;
55+ }
56+
4157pub async fn start_collector_container ( ) {
58+ init_tracing ( ) ;
59+
4260 let docker = DOCKER_CLIENT . get_or_init ( init_docker_client) ;
4361 let mut arc_guard = COLLECTOR_ARC
4462 . get_or_init ( || Mutex :: new ( None ) )
@@ -54,17 +72,16 @@ pub async fn start_collector_container() {
5472
5573 // Start a new container
5674 let image = Collector :: default ( ) ;
57- let mut runnable_image = RunnableImage :: from ( image) ;
58-
59- // Map ports
60- for port in [ 4317 , 4318 ] {
61- runnable_image = runnable_image. with_mapped_port ( Port {
62- local : port,
63- internal : port,
64- } ) ;
65- }
75+ let runnable_image =
76+ RunnableImage :: from ( image)
77+ . with_mapped_port ( Port :: from ( ( 4317 , 4317 ) ) )
78+ . with_mapped_port ( Port :: from ( ( 4318 , 4318 ) ) ) ;
6679
67- let container = Arc :: new ( docker. run ( runnable_image) ) ;
80+ let container_instance = docker. run ( runnable_image) ;
81+ let container = Arc :: new ( container_instance) ;
82+ otel_debug ! (
83+ name: "Container started" ,
84+ ports = format!( "{:?}" , container. ports( ) ) ) ;
6885
6986 // Store the container in COLLECTOR_ARC
7087 * arc_guard = Some ( Arc :: clone ( & container) ) ;
@@ -87,13 +104,13 @@ fn upsert_empty_file(path: &str) -> File {
87104/// suite shutting down!
88105///
89106pub fn stop_collector_container ( ) {
90- println ! ( "Trying to shutdown ") ;
107+ otel_debug ! ( name : "stop_collector_container ") ;
91108 if let Some ( container) = COLLECTOR_ARC
92109 . get ( )
93110 . and_then ( |arc_lock| arc_lock. lock ( ) . unwrap ( ) . take ( ) )
94111 {
95112 container. stop ( ) ;
96- println ! ( "Collector container stopped. ") ;
113+ otel_debug ! ( name : "stop_collector_container:: stopped") ;
97114 }
98115}
99116
@@ -119,13 +136,6 @@ impl Image for Collector {
119136 fn volumes ( & self ) -> Box < dyn Iterator < Item = ( & String , & String ) > + ' _ > {
120137 Box :: new ( self . volumes . iter ( ) )
121138 }
122-
123- fn expose_ports ( & self ) -> Vec < u16 > {
124- vec ! [
125- // 4317, // gRPC port, defined in Dockerfile
126- // 4318, // HTTP port, defined in Dockerfile
127- ]
128- }
129139}
130140
131141impl Default for Collector {
0 commit comments