2
2
3
3
use anyhow:: Result ;
4
4
use ctor:: dtor;
5
+ use integration_test_runner:: logs_asserter:: { read_logs_from_json, LogsAsserter } ;
5
6
use integration_test_runner:: test_utils;
6
7
use opentelemetry_otlp:: LogExporter ;
7
8
use opentelemetry_sdk:: logs:: LoggerProvider ;
8
9
use opentelemetry_sdk:: { logs as sdklogs, Resource } ;
9
10
use std:: fs:: File ;
10
11
use std:: io:: Read ;
12
+ use std:: os:: unix:: fs:: MetadataExt ;
11
13
12
14
fn init_logs ( is_simple : bool ) -> Result < sdklogs:: LoggerProvider > {
13
15
let exporter_builder = LogExporter :: builder ( ) ;
@@ -88,26 +90,26 @@ mod logtests {
88
90
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 4 ) ]
89
91
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
90
92
pub async fn logs_batch_tokio_multi_thread ( ) -> Result < ( ) > {
91
- logs_batch_tokio_helper ( ) . await
93
+ logs_tokio_helper ( false ) . await
92
94
}
93
95
94
96
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
95
97
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
96
98
pub async fn logs_batch_tokio_multi_with_one_worker ( ) -> Result < ( ) > {
97
- logs_batch_tokio_helper ( ) . await
99
+ logs_tokio_helper ( false ) . await
98
100
}
99
101
100
102
#[ tokio:: test( flavor = "current_thread" ) ]
101
103
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
102
104
pub async fn logs_batch_tokio_current ( ) -> Result < ( ) > {
103
- logs_batch_tokio_helper ( ) . await
105
+ logs_tokio_helper ( false ) . await
104
106
}
105
107
106
- async fn logs_batch_tokio_helper ( ) -> Result < ( ) > {
107
- use crate :: { assert_logs_results , init_logs} ;
108
+ async fn logs_tokio_helper ( is_simple : bool ) -> Result < ( ) > {
109
+ use crate :: { assert_logs_results_contains , init_logs} ;
108
110
test_utils:: start_collector_container ( ) . await ?;
109
111
110
- let logger_provider = init_logs ( false ) . unwrap ( ) ;
112
+ let logger_provider = init_logs ( is_simple ) . unwrap ( ) ;
111
113
let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
112
114
let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
113
115
// generate a random uuid and store it to expected guid
@@ -119,58 +121,37 @@ mod logtests {
119
121
120
122
let _ = logger_provider. shutdown ( ) ;
121
123
tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
122
- assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
124
+ assert_logs_results_contains ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
123
125
Ok ( ( ) )
124
126
}
125
127
126
128
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 4 ) ]
127
129
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-client" ) ) ]
128
130
pub async fn logs_simple_tokio_multi_thread ( ) -> Result < ( ) > {
129
- logs_simple_tokio_helper ( ) . await
131
+ logs_tokio_helper ( true ) . await
130
132
}
131
133
132
134
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
133
135
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-client" ) ) ]
134
136
pub async fn logs_simple_tokio_multi_with_one_worker ( ) -> Result < ( ) > {
135
- logs_simple_tokio_helper ( ) . await
137
+ logs_tokio_helper ( true ) . await
136
138
}
137
139
138
140
// Ignored, to be investigated
139
141
#[ ignore]
140
142
#[ tokio:: test( flavor = "current_thread" ) ]
141
143
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-client" ) ) ]
142
144
pub async fn logs_simple_tokio_current ( ) -> Result < ( ) > {
143
- logs_simple_tokio_helper ( ) . await
144
- }
145
-
146
- async fn logs_simple_tokio_helper ( ) -> Result < ( ) > {
147
- use crate :: { assert_logs_results, init_logs} ;
148
- test_utils:: start_collector_container ( ) . await ?;
149
-
150
- let logger_provider = init_logs ( true ) . unwrap ( ) ;
151
- let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
152
- let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
153
- info ! ( "Tracing initialized" ) ;
154
- // generate a random uuid and store it to expected guid
155
- let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
156
- {
157
- let _guard = tracing:: subscriber:: set_default ( subscriber) ;
158
- info ! ( target: "my-target" , uuid = expected_uuid, "hello from {}. My price is {}." , "banana" , 2.99 ) ;
159
- }
160
-
161
- let _ = logger_provider. shutdown ( ) ;
162
- tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
163
- assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
164
- Ok ( ( ) )
145
+ logs_tokio_helper ( true ) . await
165
146
}
166
147
167
148
#[ test]
168
149
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
169
150
pub fn logs_batch_non_tokio_main ( ) -> Result < ( ) > {
170
- logs_batch_non_tokio_helper ( )
151
+ logs_non_tokio_helper ( false )
171
152
}
172
153
173
- fn logs_batch_non_tokio_helper ( ) -> Result < ( ) > {
154
+ fn logs_non_tokio_helper ( is_simple : bool ) -> Result < ( ) > {
174
155
// Initialize the logger provider inside a tokio runtime
175
156
// as this allows tonic client to capture the runtime,
176
157
// but actual export occurs from the dedicated std::thread
@@ -179,7 +160,7 @@ mod logtests {
179
160
let logger_provider = rt. block_on ( async {
180
161
// While we're here setup our collector container too, as this needs tokio to run
181
162
test_utils:: start_collector_container ( ) . await ?;
182
- init_logs ( false )
163
+ init_logs ( is_simple )
183
164
} ) ?;
184
165
let layer = layer:: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
185
166
let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
@@ -192,43 +173,18 @@ mod logtests {
192
173
193
174
let _ = logger_provider. shutdown ( ) ;
194
175
std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
195
- assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
176
+ assert_logs_results_contains ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
196
177
Ok ( ( ) )
197
178
}
198
179
199
180
#[ test]
200
181
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
201
182
pub fn logs_simple_non_tokio_main ( ) -> Result < ( ) > {
202
- logs_simple_non_tokio_helper ( )
203
- }
204
-
205
- fn logs_simple_non_tokio_helper ( ) -> Result < ( ) > {
206
- // Initialize the logger provider inside a tokio runtime
207
- // as this allows tonic client to capture the runtime,
208
- // but actual export occurs from the main non-tokio thread.
209
- let rt = tokio:: runtime:: Runtime :: new ( ) ?;
210
- let logger_provider = rt. block_on ( async {
211
- // While we're here setup our collector container too, as this needs tokio to run
212
- test_utils:: start_collector_container ( ) . await ?;
213
- init_logs ( true )
214
- } ) ?;
215
- let layer = layer:: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
216
- let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
217
- // generate a random uuid and store it to expected guid
218
- let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
219
- {
220
- let _guard = tracing:: subscriber:: set_default ( subscriber) ;
221
- info ! ( target: "my-target" , uuid = expected_uuid, "hello from {}. My price is {}." , "banana" , 2.99 ) ;
222
- }
223
-
224
- let _ = logger_provider. shutdown ( ) ;
225
- std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
226
- assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
227
- Ok ( ( ) )
183
+ logs_non_tokio_helper ( true )
228
184
}
229
185
}
230
186
231
- pub fn assert_logs_results ( result : & str , expected_content : & str ) -> Result < ( ) > {
187
+ pub fn assert_logs_results_contains ( result : & str , expected_content : & str ) -> Result < ( ) > {
232
188
let file = File :: open ( result) ?;
233
189
let mut contents = String :: new ( ) ;
234
190
let mut reader = std:: io:: BufReader :: new ( & file) ;
@@ -237,6 +193,16 @@ pub fn assert_logs_results(result: &str, expected_content: &str) -> Result<()> {
237
193
Ok ( ( ) )
238
194
}
239
195
196
+ pub fn assert_logs_results ( result : & str , expected : & str ) -> Result < ( ) > {
197
+ let left = read_logs_from_json ( File :: open ( expected) ?) ?;
198
+ let right = read_logs_from_json ( File :: open ( result) ?) ?;
199
+
200
+ LogsAsserter :: new ( left, right) . assert ( ) ;
201
+
202
+ assert ! ( File :: open( result) . unwrap( ) . metadata( ) . unwrap( ) . size( ) > 0 ) ;
203
+ Ok ( ( ) )
204
+ }
205
+
240
206
///
241
207
/// Make sure we stop the collector container, otherwise it will sit around hogging our
242
208
/// ports and subsequent test runs will fail.
0 commit comments