@@ -35,12 +35,23 @@ fn init_logs() -> Result<sdklogs::LoggerProvider> {
3535
3636#[ cfg( test) ]
3737mod logtests {
38+ // TODO: The tests in this mod works like below: Emit a log with a UUID,
39+ // then read the logs from the file and check if the UUID is present in the
40+ // logs. This makes it easy to validate with a single collector and its
41+ // output. This is a very simple test but good enough to validate that OTLP
42+ // Exporter did work! A more comprehensive test would be to validate the
43+ // entire Payload. The infra for it already exists (logs_asserter.rs), the
44+ // TODO here is to write a test that validates the entire payload.
45+
3846 use super :: * ;
3947 use integration_test_runner:: logs_asserter:: { read_logs_from_json, LogsAsserter } ;
48+ use integration_test_runner:: test_utils;
49+ use opentelemetry_appender_tracing:: layer;
4050 use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
4151 use std:: { fs:: File , time:: Duration } ;
4252 use tracing:: info;
4353 use tracing_subscriber:: layer:: SubscriberExt ;
54+ use uuid:: Uuid ;
4455
4556 #[ test]
4657 #[ should_panic( expected = "assertion `left == right` failed: body does not match" ) ]
@@ -68,18 +79,28 @@ mod logtests {
6879
6980 #[ tokio:: test( flavor = "multi_thread" , worker_threads = 4 ) ]
7081 #[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
71- pub async fn logs_batch ( ) -> Result < ( ) > {
72- use integration_test_runner:: test_utils;
73- use opentelemetry_appender_tracing:: layer;
74- use tracing:: info;
75- use tracing_subscriber:: layer:: SubscriberExt ;
76- use uuid:: Uuid ;
82+ pub async fn logs_batch_tokio_multi_thread ( ) -> Result < ( ) > {
83+ logs_batch_tokio_helper ( ) . await
84+ }
85+
86+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
87+ #[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
88+ pub async fn logs_batch_tokio_multi_with_one_worker ( ) -> Result < ( ) > {
89+ logs_batch_tokio_helper ( ) . await
90+ }
7791
92+ #[ tokio:: test( flavor = "current_thread" ) ]
93+ #[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
94+ pub async fn logs_batch_tokio_current ( ) -> Result < ( ) > {
95+ logs_batch_tokio_helper ( ) . await
96+ }
97+
98+ async fn logs_batch_tokio_helper ( ) -> Result < ( ) > {
7899 use crate :: { assert_logs_results, init_logs} ;
79100 test_utils:: start_collector_container ( ) . await ?;
80101
81102 let logger_provider = init_logs ( ) . unwrap ( ) ;
82- let layer = layer :: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
103+ let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
83104 let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
84105 // generate a random uuid and store it to expected guid
85106 let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
@@ -89,44 +110,40 @@ mod logtests {
89110 }
90111
91112 let _ = logger_provider. shutdown ( ) ;
92-
93113 tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
94-
95114 assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
96-
97115 Ok ( ( ) )
98116 }
99117
100118 #[ test]
101119 #[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
102120 pub fn logs_batch_non_tokio_main ( ) -> Result < ( ) > {
121+ logs_batch_non_tokio_helper ( )
122+ }
123+
124+ fn logs_batch_non_tokio_helper ( ) -> Result < ( ) > {
103125 // Initialize the logger provider inside a tokio runtime
104126 // as this allows tonic client to capture the runtime,
105127 // but actual export occurs from the dedicated std::thread
106128 // created by BatchLogProcessor.
107-
108- use uuid:: Uuid ;
109129 let rt = tokio:: runtime:: Runtime :: new ( ) ?;
110130 let logger_provider = rt. block_on ( async {
111131 // While we're here setup our collector container too, as this needs tokio to run
112132 test_utils:: start_collector_container ( ) . await ?;
113133 init_logs ( )
114134 } ) ?;
115-
116- info ! ( "LoggerProvider created" ) ;
117- let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
135+ let layer = layer:: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
118136 let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
119137 // generate a random uuid and store it to expected guid
120138 let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
121139 {
122140 let _guard = tracing:: subscriber:: set_default ( subscriber) ;
123- info ! ( target: "my-target" , uuid = expected_uuid, "hello from {}. My price is {}." , "banana" , 2.99 ) ;
141+ info ! ( target: "my-target" , uuid = expected_uuid, "hello from {}. My price is {}." , "banana" , 2.99 ) ;
124142 }
143+
125144 let _ = logger_provider. shutdown ( ) ;
126- info ! ( "Sleeping for 5 seconds to allow collector to store logs to file" ) ;
127145 std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
128146 assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
129-
130147 Ok ( ( ) )
131148 }
132149}
0 commit comments