@@ -767,12 +767,22 @@ mod tests {
767767 /// 1. Spans are created with correct attributes (correlation_id)
768768 /// 2. Nested spans from UninitializedSandbox::new are properly parented
769769 /// 3. Error events are emitted when sandbox creation fails
770+ ///
771+ /// NOTE: The `#[instrument]` callsite on `UninitializedSandbox::new` uses
772+ /// tracing's global interest cache. If another test thread registers that
773+ /// callsite first (with the no-op subscriber), the cached `Interest::never()`
774+ /// will suppress span creation on our thread. To work around this, we:
775+ /// 1. Make a warmup call to force-register the callsite
776+ /// 2. Call `rebuild_interest_cache()` to overwrite the cached interest with
777+ /// our subscriber's `Interest::sometimes()`
778+ /// 3. Clear recorded state and run the real test
770779 #[ test]
771780 #[ cfg( feature = "build-metadata" ) ]
772781 fn test_trace_trace ( ) {
773782 use hyperlight_testing:: tracing_subscriber:: TracingSubscriber ;
774783 use tracing:: Level ;
775784 use tracing_core:: Subscriber ;
785+ use tracing_core:: callsite:: rebuild_interest_cache;
776786 use uuid:: Uuid ;
777787
778788 /// Helper to extract a string value from nested JSON: obj["span"]["attributes"][key]
@@ -793,39 +803,68 @@ mod tests {
793803 let subscriber = TracingSubscriber :: new ( Level :: TRACE ) ;
794804
795805 tracing:: subscriber:: with_default ( subscriber. clone ( ) , || {
806+ // Warmup: force-register the #[instrument] callsite on
807+ // UninitializedSandbox::new by calling it once. This ensures the
808+ // callsite exists in the global registry regardless of whether
809+ // another thread already registered it.
810+ let bad_path = simple_guest_as_string ( ) . unwrap ( ) + "does_not_exist" ;
811+ let _ = UninitializedSandbox :: new ( GuestBinary :: FilePath ( bad_path. clone ( ) ) , None ) ;
812+
813+ // Rebuild the interest cache. Now that the callsite is guaranteed
814+ // to be registered, this will overwrite any cached Interest::never()
815+ // (from another thread's no-op subscriber) with our subscriber's
816+ // Interest::sometimes(), ensuring subsequent calls create spans.
817+ rebuild_interest_cache ( ) ;
818+
819+ // Clear all state from the warmup call
820+ subscriber. clear ( ) ;
821+
796822 let correlation_id = Uuid :: new_v4 ( ) . to_string ( ) ;
797823 let _span = tracing:: error_span!( "test_trace_logs" , %correlation_id) . entered ( ) ;
798824
799- // Verify we're in span 1 with correct name
800- let ( span_id , span_meta) = subscriber
825+ // Verify we're in a span with correct name
826+ let ( test_span_id , span_meta) = subscriber
801827 . current_span ( )
802828 . into_inner ( )
803829 . expect ( "Should be inside a span" ) ;
804- assert_eq ! ( span_id. into_u64( ) , 1 , "Should be in span 1" ) ;
805830 assert_eq ! ( span_meta. name( ) , "test_trace_logs" ) ;
806831
807832 // Verify correlation_id was recorded
808- let span_data = subscriber. get_span ( 1 ) ;
833+ let span_data = subscriber. get_span ( test_span_id . into_u64 ( ) ) ;
809834 let recorded_id =
810835 get_span_attr ( & span_data, "correlation_id" ) . expect ( "correlation_id not found" ) ;
811836 assert_eq ! ( recorded_id, correlation_id) ;
812837
813838 // Try to create a sandbox with a non-existent binary - this should fail
814839 // and emit an error event
815- let bad_path = simple_guest_as_string ( ) . unwrap ( ) + "does_not_exist" ;
816840 let result = UninitializedSandbox :: new ( GuestBinary :: FilePath ( bad_path) , None ) ;
817841 assert ! ( result. is_err( ) , "Sandbox creation should fail" ) ;
818842
819- // Verify we're still in span 1 ( our test span)
820- let ( span_id , _) = subscriber
843+ // Verify we're still in our test span
844+ let ( current_id , _) = subscriber
821845 . current_span ( )
822846 . into_inner ( )
823847 . expect ( "Should still be inside a span" ) ;
824- assert_eq ! ( span_id. into_u64( ) , 1 , "Should still be in span 1" ) ;
848+ assert_eq ! (
849+ current_id. into_u64( ) ,
850+ test_span_id. into_u64( ) ,
851+ "Should still be in the test span"
852+ ) ;
825853
826- // Verify span 2 was created by UninitializedSandbox::new
827- let inner_span_meta = subscriber. get_span_metadata ( 2 ) ;
828- assert_eq ! ( inner_span_meta. name( ) , "new" ) ;
854+ // Verify a span named "new" was created by UninitializedSandbox::new
855+ // (look up by name rather than hardcoded ID to avoid fragility)
856+ let all_spans = subscriber. get_all_spans ( ) ;
857+ let new_span_entry = all_spans
858+ . iter ( )
859+ . find ( |& ( & id, _) | {
860+ id != test_span_id. into_u64 ( )
861+ && subscriber. get_span_metadata ( id) . name ( ) == "new"
862+ } )
863+ . expect ( "Expected a span named 'new' from UninitializedSandbox::new" ) ;
864+ assert_eq ! (
865+ subscriber. get_span_metadata( * new_span_entry. 0 ) . name( ) ,
866+ "new"
867+ ) ;
829868
830869 // Verify the error event was emitted
831870 let events = subscriber. get_events ( ) ;
0 commit comments