@@ -342,8 +342,7 @@ mod tests {
342342 use std:: panic:: { catch_unwind, AssertUnwindSafe } ;
343343 use std:: str:: FromStr ;
344344 use std:: sync:: Mutex ;
345- use telemetry:: metrics;
346- use tracing:: error;
345+ use tracing:: { error, Level } ;
347346 use utils:: reschedule;
348347
349348 fn test_error_future < R : Runner > ( runner : R ) {
@@ -980,110 +979,6 @@ mod tests {
980979 } )
981980 }
982981
983- fn test_metrics_serve < R , L , Si , St > ( runner : R )
984- where
985- R : Runner ,
986- R :: Context : Spawner + Metrics + Network < L , Si , St > + Clock ,
987- L : Listener < Si , St > ,
988- Si : Sink ,
989- St : Stream ,
990- {
991- runner. start ( |context| async move {
992- // Register a test metric
993- let counter: Counter < u64 > = Counter :: default ( ) ;
994- context. register ( "test_counter" , "Test counter" , counter. clone ( ) ) ;
995- counter. inc ( ) ;
996-
997- // Define the server address
998- let address = SocketAddr :: from_str ( "127.0.0.1:8000" ) . unwrap ( ) ;
999-
1000- // Start the metrics server (serves one connection and exits)
1001- context
1002- . with_label ( "server" )
1003- . spawn ( move |context| async move {
1004- metrics:: server:: serve ( context, address) . await ;
1005- } ) ;
1006-
1007- // Helper functions to parse HTTP response
1008- async fn read_line < St : Stream > ( stream : & mut St ) -> Result < String , Error > {
1009- let mut line = Vec :: new ( ) ;
1010- loop {
1011- let mut byte = [ 0 ; 1 ] ;
1012- stream. recv ( & mut byte) . await ?;
1013- if byte[ 0 ] == b'\n' {
1014- if line. last ( ) == Some ( & b'\r' ) {
1015- line. pop ( ) ; // Remove trailing \r
1016- }
1017- break ;
1018- }
1019- line. push ( byte[ 0 ] ) ;
1020- }
1021- String :: from_utf8 ( line) . map_err ( |_| Error :: ReadFailed )
1022- }
1023-
1024- async fn read_headers < St : Stream > (
1025- stream : & mut St ,
1026- ) -> Result < HashMap < String , String > , Error > {
1027- let mut headers = HashMap :: new ( ) ;
1028- loop {
1029- let line = read_line ( stream) . await ?;
1030- if line. is_empty ( ) {
1031- break ;
1032- }
1033- let parts: Vec < & str > = line. splitn ( 2 , ": " ) . collect ( ) ;
1034- if parts. len ( ) == 2 {
1035- headers. insert ( parts[ 0 ] . to_string ( ) , parts[ 1 ] . to_string ( ) ) ;
1036- }
1037- }
1038- Ok ( headers)
1039- }
1040-
1041- async fn read_body < St : Stream > (
1042- stream : & mut St ,
1043- content_length : usize ,
1044- ) -> Result < String , Error > {
1045- let mut body = vec ! [ 0 ; content_length] ;
1046- stream. recv ( & mut body) . await ?;
1047- String :: from_utf8 ( body) . map_err ( |_| Error :: ReadFailed )
1048- }
1049-
1050- // Simulate a client connecting to the server
1051- let client_handle = context
1052- . with_label ( "client" )
1053- . spawn ( move |context| async move {
1054- let ( _, mut stream) = loop {
1055- match context. dial ( address) . await {
1056- Ok ( ( sink, stream) ) => break ( sink, stream) ,
1057- Err ( e) => {
1058- // The client may be polled before the server is ready, that's alright!
1059- error ! ( err =?e, "failed to connect" ) ;
1060- context. sleep ( Duration :: from_millis ( 10 ) ) . await ;
1061- }
1062- }
1063- } ;
1064-
1065- // Read and verify the HTTP status line
1066- let status_line = read_line ( & mut stream) . await . unwrap ( ) ;
1067- assert_eq ! ( status_line, "HTTP/1.1 200 OK" ) ;
1068-
1069- // Read and parse headers
1070- let headers = read_headers ( & mut stream) . await . unwrap ( ) ;
1071- let content_length = headers
1072- . get ( "Content-Length" )
1073- . unwrap ( )
1074- . parse :: < usize > ( )
1075- . unwrap ( ) ;
1076-
1077- // Read and verify the body
1078- let body = read_body ( & mut stream, content_length) . await . unwrap ( ) ;
1079- assert ! ( body. contains( "test_counter_total 1" ) ) ;
1080- } ) ;
1081-
1082- // Wait for the client task to complete
1083- client_handle. await . unwrap ( ) ;
1084- } ) ;
1085- }
1086-
1087982 #[ test]
1088983 fn test_deterministic_future ( ) {
1089984 let runner = deterministic:: Runner :: default ( ) ;
@@ -1233,12 +1128,6 @@ mod tests {
12331128 test_metrics_label ( executor) ;
12341129 }
12351130
1236- #[ test]
1237- fn test_deterministic_metrics_serve ( ) {
1238- let executor = deterministic:: Runner :: default ( ) ;
1239- test_metrics_serve ( executor) ;
1240- }
1241-
12421131 #[ test]
12431132 fn test_tokio_error_future ( ) {
12441133 let runner = tokio:: Runner :: default ( ) ;
@@ -1388,8 +1277,110 @@ mod tests {
13881277 }
13891278
13901279 #[ test]
1391- fn test_tokio_metrics_serve ( ) {
1280+ fn test_tokio_telemetry ( ) {
13921281 let executor = tokio:: Runner :: default ( ) ;
1393- test_metrics_serve ( executor) ;
1282+ executor. start ( |context| async move {
1283+ // Define the server address
1284+ let address = SocketAddr :: from_str ( "127.0.0.1:8000" ) . unwrap ( ) ;
1285+
1286+ // Configure telemetry
1287+ tokio:: telemetry:: init (
1288+ context. with_label ( "metrics" ) ,
1289+ Level :: INFO ,
1290+ Some ( address) ,
1291+ None ,
1292+ ) ;
1293+
1294+ // Register a test metric
1295+ let counter: Counter < u64 > = Counter :: default ( ) ;
1296+ context. register ( "test_counter" , "Test counter" , counter. clone ( ) ) ;
1297+ counter. inc ( ) ;
1298+
1299+ // Helper functions to parse HTTP response
1300+ async fn read_line < St : Stream > ( stream : & mut St ) -> Result < String , Error > {
1301+ let mut line = Vec :: new ( ) ;
1302+ loop {
1303+ let mut byte = [ 0 ; 1 ] ;
1304+ stream. recv ( & mut byte) . await ?;
1305+ if byte[ 0 ] == b'\n' {
1306+ if line. last ( ) == Some ( & b'\r' ) {
1307+ line. pop ( ) ; // Remove trailing \r
1308+ }
1309+ break ;
1310+ }
1311+ line. push ( byte[ 0 ] ) ;
1312+ }
1313+ String :: from_utf8 ( line) . map_err ( |_| Error :: ReadFailed )
1314+ }
1315+
1316+ async fn read_headers < St : Stream > (
1317+ stream : & mut St ,
1318+ ) -> Result < HashMap < String , String > , Error > {
1319+ let mut headers = HashMap :: new ( ) ;
1320+ loop {
1321+ let line = read_line ( stream) . await ?;
1322+ if line. is_empty ( ) {
1323+ break ;
1324+ }
1325+ let parts: Vec < & str > = line. splitn ( 2 , ": " ) . collect ( ) ;
1326+ if parts. len ( ) == 2 {
1327+ headers. insert ( parts[ 0 ] . to_string ( ) , parts[ 1 ] . to_string ( ) ) ;
1328+ }
1329+ }
1330+ Ok ( headers)
1331+ }
1332+
1333+ async fn read_body < St : Stream > (
1334+ stream : & mut St ,
1335+ content_length : usize ,
1336+ ) -> Result < String , Error > {
1337+ let mut body = vec ! [ 0 ; content_length] ;
1338+ stream. recv ( & mut body) . await ?;
1339+ String :: from_utf8 ( body) . map_err ( |_| Error :: ReadFailed )
1340+ }
1341+
1342+ // Simulate a client connecting to the server
1343+ let client_handle = context
1344+ . with_label ( "client" )
1345+ . spawn ( move |context| async move {
1346+ let ( mut sink, mut stream) = loop {
1347+ match context. dial ( address) . await {
1348+ Ok ( ( sink, stream) ) => break ( sink, stream) ,
1349+ Err ( e) => {
1350+ // The client may be polled before the server is ready, that's alright!
1351+ error ! ( err =?e, "failed to connect" ) ;
1352+ context. sleep ( Duration :: from_millis ( 10 ) ) . await ;
1353+ }
1354+ }
1355+ } ;
1356+
1357+ // Send a GET request to the server
1358+ let request = format ! (
1359+ "GET /metrics HTTP/1.1\r \n Host: {}\r \n Connection: close\r \n \r \n " ,
1360+ address
1361+ ) ;
1362+ sink. send ( request. as_bytes ( ) ) . await . unwrap ( ) ;
1363+
1364+ // Read and verify the HTTP status line
1365+ let status_line = read_line ( & mut stream) . await . unwrap ( ) ;
1366+ assert_eq ! ( status_line, "HTTP/1.1 200 OK" ) ;
1367+
1368+ // Read and parse headers
1369+ let headers = read_headers ( & mut stream) . await . unwrap ( ) ;
1370+ println ! ( "Headers: {:?}" , headers) ;
1371+ let content_length = headers
1372+ . get ( "content-length" )
1373+ . unwrap ( )
1374+ . parse :: < usize > ( )
1375+ . unwrap ( ) ;
1376+
1377+ // Read and verify the body
1378+ let body = read_body ( & mut stream, content_length) . await . unwrap ( ) ;
1379+ assert ! ( body. contains( "test_counter_total 1" ) ) ;
1380+ } ) ;
1381+
1382+ // Wait for the client task to complete
1383+ client_handle. await . unwrap ( ) ;
1384+ } ) ;
13941385 }
13951386}
0 commit comments