@@ -35,7 +35,7 @@ fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
3535 let exporter = exporter_builder. build ( ) ?;
3636
3737 Ok ( opentelemetry_sdk:: trace:: TracerProvider :: builder ( )
38- . with_batch_exporter ( exporter, runtime :: Tokio )
38+ . with_batch_exporter ( exporter)
3939 . with_resource (
4040 Resource :: builder_empty ( )
4141 . with_service_name ( "basic-otlp-tracing-example" )
@@ -141,6 +141,50 @@ pub fn test_serde() -> Result<()> {
141141 Ok ( ( ) )
142142}
143143
144+ #[ test]
145+ #[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
146+ pub fn span_batch_non_tokio_main ( ) -> Result < ( ) > {
147+ // Initialize the tracer provider inside a tokio runtime
148+ // as this allows tonic client to capture the runtime,
149+ // but actual export occurs from the dedicated std::thread
150+ // created by BatchSpanProcessor.
151+
152+ use anyhow:: Ok ;
153+ let rt = tokio:: runtime:: Runtime :: new ( ) ?;
154+ let tracer_provider = rt. block_on ( async {
155+ // While we're here setup our collector container too, as this needs tokio to run
156+ let _ = test_utils:: start_collector_container ( ) . await ;
157+ init_tracer_provider ( )
158+ } ) ?;
159+
160+ let tracer = global:: tracer ( "ex.com/basic" ) ;
161+
162+ tracer. in_span ( "operation" , |cx| {
163+ let span = cx. span ( ) ;
164+ span. add_event (
165+ "Nice operation!" . to_string ( ) ,
166+ vec ! [ KeyValue :: new( "bogons" , 100 ) ] ,
167+ ) ;
168+ span. set_attribute ( KeyValue :: new ( ANOTHER_KEY , "yes" ) ) ;
169+
170+ tracer. in_span ( "Sub operation..." , |cx| {
171+ let span = cx. span ( ) ;
172+ span. set_attribute ( KeyValue :: new ( LEMONS_KEY , "five" ) ) ;
173+
174+ span. add_event ( "Sub span event" , vec ! [ ] ) ;
175+ } ) ;
176+ } ) ;
177+
178+ tracer_provider. shutdown ( ) ?;
179+
180+ // Give it a second to flush
181+ std:: thread:: sleep ( Duration :: from_secs ( 2 ) ) ;
182+
183+ // Validate results
184+ assert_traces_results ( test_utils:: TRACES_FILE , "./expected/traces.json" ) ?;
185+ Ok ( ( ) )
186+ }
187+
144188///
145189/// Make sure we stop the collector container, otherwise it will sit around hogging our
146190/// ports and subsequent test runs will fail.
0 commit comments