@@ -1399,6 +1399,25 @@ impl TraceExporter {
1399
1399
fn get_agent_url ( & self ) -> Uri {
1400
1400
self . output_format . add_path ( & self . endpoint . url )
1401
1401
}
1402
+
1403
+ #[ cfg( test) ]
1404
+ /// Test only function to check if the stats computation is active and the worker is running
1405
+ pub fn is_stats_worker_active ( & self ) -> bool {
1406
+ if !matches ! (
1407
+ * * self . client_side_stats. load( ) ,
1408
+ StatsComputationStatus :: Enabled { .. }
1409
+ ) {
1410
+ return false ;
1411
+ }
1412
+
1413
+ if let Ok ( workers) = self . workers . try_lock ( ) {
1414
+ if let Some ( stats_worker) = & workers. stats {
1415
+ return matches ! ( stats_worker, PausableWorker :: Running { .. } ) ;
1416
+ }
1417
+ }
1418
+
1419
+ false
1420
+ }
1402
1421
}
1403
1422
1404
1423
#[ derive( Debug , Default , Clone ) ]
@@ -1498,7 +1517,7 @@ mod tests {
1498
1517
then. status ( 200 ) . body ( "" ) ;
1499
1518
} ) ;
1500
1519
1501
- let mock_info = server. mock ( |when, then| {
1520
+ let _mock_info = server. mock ( |when, then| {
1502
1521
when. method ( GET ) . path ( "/info" ) ;
1503
1522
then. status ( 200 )
1504
1523
. header ( "content-type" , "application/json" )
@@ -1528,7 +1547,7 @@ mod tests {
1528
1547
let data = rmp_serde:: to_vec_named ( & vec ! [ trace_chunk] ) . unwrap ( ) ;
1529
1548
1530
1549
// Wait for the info fetcher to get the config
1531
- while mock_info . hits ( ) == 0 {
1550
+ while agent_info :: get_agent_info ( ) . is_none ( ) {
1532
1551
exporter
1533
1552
. runtime
1534
1553
. lock ( )
@@ -1544,10 +1563,20 @@ mod tests {
1544
1563
// Error received because server is returning an empty body.
1545
1564
assert ! ( result. is_err( ) ) ;
1546
1565
1566
+ // Wait for the stats worker to be active before shutting down to avoid potential flaky
1567
+ // tests on CI where we shutdown before the stats worker had time to start
1568
+ let start_time = std:: time:: Instant :: now ( ) ;
1569
+ while !exporter. is_stats_worker_active ( ) {
1570
+ if start_time. elapsed ( ) > Duration :: from_secs ( 10 ) {
1571
+ panic ! ( "Timeout waiting for stats worker to become active" ) ;
1572
+ }
1573
+ std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
1574
+ }
1575
+
1547
1576
exporter. shutdown ( None ) . unwrap ( ) ;
1548
1577
1549
1578
// Wait for the mock server to process the stats
1550
- for _ in 0 ..500 {
1579
+ for _ in 0 ..1000 {
1551
1580
if mock_traces. hits ( ) > 0 && mock_stats. hits ( ) > 0 {
1552
1581
break ;
1553
1582
} else {
@@ -1585,7 +1614,7 @@ mod tests {
1585
1614
then. delay ( Duration :: from_secs ( 10 ) ) . status ( 200 ) . body ( "" ) ;
1586
1615
} ) ;
1587
1616
1588
- let mock_info = server. mock ( |when, then| {
1617
+ let _mock_info = server. mock ( |when, then| {
1589
1618
when. method ( GET ) . path ( "/info" ) ;
1590
1619
then. status ( 200 )
1591
1620
. header ( "content-type" , "application/json" )
@@ -1618,8 +1647,9 @@ mod tests {
1618
1647
1619
1648
let data = rmp_serde:: to_vec_named ( & vec ! [ trace_chunk] ) . unwrap ( ) ;
1620
1649
1621
- // Wait for the info fetcher to get the config
1622
- while mock_info. hits ( ) == 0 {
1650
+ // Wait for agent_info to be present so that sending a trace will trigger the stats worker
1651
+ // to start
1652
+ while agent_info:: get_agent_info ( ) . is_none ( ) {
1623
1653
exporter
1624
1654
. runtime
1625
1655
. lock ( )
@@ -1633,6 +1663,16 @@ mod tests {
1633
1663
1634
1664
exporter. send ( data. as_ref ( ) , 1 ) . unwrap ( ) ;
1635
1665
1666
+ // Wait for the stats worker to be active before shutting down to avoid potential flaky
1667
+ // tests on CI where we shutdown before the stats worker had time to start
1668
+ let start_time = std:: time:: Instant :: now ( ) ;
1669
+ while !exporter. is_stats_worker_active ( ) {
1670
+ if start_time. elapsed ( ) > Duration :: from_secs ( 10 ) {
1671
+ panic ! ( "Timeout waiting for stats worker to become active" ) ;
1672
+ }
1673
+ std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
1674
+ }
1675
+
1636
1676
exporter
1637
1677
. shutdown ( Some ( Duration :: from_millis ( 5 ) ) )
1638
1678
. unwrap_err ( ) ; // The shutdown should timeout
0 commit comments