@@ -42,7 +42,7 @@ use opentelemetry_semantic_conventions::SCHEMA_URL;
42
42
use tracing_subscriber:: EnvFilter ;
43
43
use uuid:: Uuid ;
44
44
45
- const ADDR : & str = "http://localhost:4317" ;
45
+ const ENDPOINT_ADDR : & str = "http://localhost:4317" ;
46
46
47
47
fn fn_writer ( _msg : String ) -> HyperlightResult < i32 > {
48
48
Ok ( 0 )
@@ -52,9 +52,9 @@ fn fn_writer(_msg: String) -> HyperlightResult<i32> {
52
52
53
53
#[ tokio:: main]
54
54
async fn main ( ) -> Result < ( ) , Box < dyn Error + Send + Sync + ' static > > {
55
- init_tracing_subscriber ( ADDR ) ?;
55
+ init_tracing_subscriber ( ENDPOINT_ADDR ) ?;
56
56
57
- Ok ( run_example ( ) ?)
57
+ Ok ( run_example ( true ) ?)
58
58
}
59
59
60
60
fn init_tracing_subscriber ( addr : & str ) -> Result < ( ) , Box < dyn Error + Send + Sync + ' static > > {
@@ -81,15 +81,22 @@ fn init_tracing_subscriber(addr: &str) -> Result<(), Box<dyn Error + Send + Sync
81
81
82
82
let otel_layer = OpenTelemetryLayer :: new ( tracer) ;
83
83
84
+ // Try using the environment otherwise set default filters
85
+ let filter = EnvFilter :: try_from_default_env ( ) . unwrap_or_else ( |_| {
86
+ EnvFilter :: from_default_env ( )
87
+ . add_directive ( "hyperlight_host=info" . parse ( ) . unwrap ( ) )
88
+ . add_directive ( "tracing=info" . parse ( ) . unwrap ( ) )
89
+ } ) ;
90
+
84
91
tracing_subscriber:: registry ( )
85
- . with ( EnvFilter :: from_default_env ( ) )
92
+ . with ( filter )
86
93
. with ( otel_layer)
87
94
. try_init ( ) ?;
88
95
89
96
Ok ( ( ) )
90
97
}
91
98
92
- fn run_example ( ) -> HyperlightResult < ( ) > {
99
+ fn run_example ( wait_input : bool ) -> HyperlightResult < ( ) > {
93
100
// Get the path to a simple guest binary.
94
101
let hyperlight_guest_path =
95
102
simple_guest_as_string ( ) . expect ( "Cannot find the guest binary at the expected location." ) ;
@@ -187,9 +194,12 @@ fn run_example() -> HyperlightResult<()> {
187
194
join_handles. push ( handle) ;
188
195
}
189
196
190
- println ! ( "Press enter to exit..." ) ;
191
- let mut input = String :: new ( ) ;
192
- stdin ( ) . read_line ( & mut input) ?;
197
+ if wait_input {
198
+ println ! ( "Press enter to exit..." ) ;
199
+ let mut input = String :: new ( ) ;
200
+ stdin ( ) . read_line ( & mut input) ?;
201
+ }
202
+
193
203
* should_exit. try_lock ( ) . unwrap ( ) = true ;
194
204
for join_handle in join_handles {
195
205
let result = join_handle. join ( ) ;
@@ -199,3 +209,52 @@ fn run_example() -> HyperlightResult<()> {
199
209
200
210
Ok ( ( ) )
201
211
}
212
+
213
+ #[ cfg( test) ]
214
+ mod test {
215
+ use hyperlight_host:: { HyperlightError , Result } ;
216
+ use tokio:: io:: AsyncReadExt ;
217
+ use tokio:: net:: { TcpListener , TcpStream } ;
218
+
219
+ use super :: * ;
220
+
221
+ const TESTER_ADDR : & str = "127.0.0.1:4317" ;
222
+
223
+ async fn handle ( mut stream : TcpStream ) -> Result < ( ) > {
224
+ let mut buf = Vec :: with_capacity ( 128 ) ;
225
+ let size = stream. read_buf ( & mut buf) . await ?;
226
+
227
+ if size > 0 {
228
+ Ok ( ( ) )
229
+ } else {
230
+ Err ( HyperlightError :: Error ( "Cannot read req body" . to_string ( ) ) )
231
+ }
232
+ }
233
+
234
+ async fn check_otl_connection ( addr : & str ) -> Result < ( ) > {
235
+ let listener = TcpListener :: bind ( addr) . await ?;
236
+
237
+ let ( stream, _) = listener. accept ( ) . await ?;
238
+
239
+ handle ( stream) . await
240
+ }
241
+
242
+ #[ tokio:: test]
243
+ async fn test_subscriber ( ) {
244
+ // Create task that generates spans
245
+ let task = tokio:: spawn ( async move {
246
+ let _ = init_tracing_subscriber ( ENDPOINT_ADDR ) ;
247
+
248
+ // No need to wait for input, just generate some spans and exit
249
+ let _ = run_example ( false ) ;
250
+ } ) ;
251
+
252
+ // Create server that listens and checks to see if traces are received
253
+ let result = check_otl_connection ( TESTER_ADDR ) . await ;
254
+
255
+ // Abort task in case it doesn't finish
256
+ task. abort ( ) ;
257
+
258
+ assert ! ( result. is_ok( ) ) ;
259
+ }
260
+ }
0 commit comments