@@ -35,12 +35,23 @@ fn init_logs() -> Result<sdklogs::LoggerProvider> {
35
35
36
36
#[ cfg( test) ]
37
37
mod 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
+
38
46
use super :: * ;
39
47
use integration_test_runner:: logs_asserter:: { read_logs_from_json, LogsAsserter } ;
48
+ use integration_test_runner:: test_utils;
49
+ use opentelemetry_appender_tracing:: layer;
40
50
use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
41
51
use std:: { fs:: File , time:: Duration } ;
42
52
use tracing:: info;
43
53
use tracing_subscriber:: layer:: SubscriberExt ;
54
+ use uuid:: Uuid ;
44
55
45
56
#[ test]
46
57
#[ should_panic( expected = "assertion `left == right` failed: body does not match" ) ]
@@ -68,18 +79,28 @@ mod logtests {
68
79
69
80
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 4 ) ]
70
81
#[ 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
+ }
77
91
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 < ( ) > {
78
99
use crate :: { assert_logs_results, init_logs} ;
79
100
test_utils:: start_collector_container ( ) . await ?;
80
101
81
102
let logger_provider = init_logs ( ) . unwrap ( ) ;
82
- let layer = layer :: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
103
+ let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
83
104
let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
84
105
// generate a random uuid and store it to expected guid
85
106
let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
@@ -89,44 +110,40 @@ mod logtests {
89
110
}
90
111
91
112
let _ = logger_provider. shutdown ( ) ;
92
-
93
113
tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
94
-
95
114
assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
96
-
97
115
Ok ( ( ) )
98
116
}
99
117
100
118
#[ test]
101
119
#[ cfg( any( feature = "tonic-client" , feature = "reqwest-blocking-client" ) ) ]
102
120
pub fn logs_batch_non_tokio_main ( ) -> Result < ( ) > {
121
+ logs_batch_non_tokio_helper ( )
122
+ }
123
+
124
+ fn logs_batch_non_tokio_helper ( ) -> Result < ( ) > {
103
125
// Initialize the logger provider inside a tokio runtime
104
126
// as this allows tonic client to capture the runtime,
105
127
// but actual export occurs from the dedicated std::thread
106
128
// created by BatchLogProcessor.
107
-
108
- use uuid:: Uuid ;
109
129
let rt = tokio:: runtime:: Runtime :: new ( ) ?;
110
130
let logger_provider = rt. block_on ( async {
111
131
// While we're here setup our collector container too, as this needs tokio to run
112
132
test_utils:: start_collector_container ( ) . await ?;
113
133
init_logs ( )
114
134
} ) ?;
115
-
116
- info ! ( "LoggerProvider created" ) ;
117
- let layer = OpenTelemetryTracingBridge :: new ( & logger_provider) ;
135
+ let layer = layer:: OpenTelemetryTracingBridge :: new ( & logger_provider) ;
118
136
let subscriber = tracing_subscriber:: registry ( ) . with ( layer) ;
119
137
// generate a random uuid and store it to expected guid
120
138
let expected_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
121
139
{
122
140
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 ) ;
124
142
}
143
+
125
144
let _ = logger_provider. shutdown ( ) ;
126
- info ! ( "Sleeping for 5 seconds to allow collector to store logs to file" ) ;
127
145
std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
128
146
assert_logs_results ( test_utils:: LOGS_FILE , expected_uuid. as_str ( ) ) ?;
129
-
130
147
Ok ( ( ) )
131
148
}
132
149
}
0 commit comments