@@ -5,6 +5,7 @@ use opentelemetry_sdk::export::logs::LogBatch;
55use opentelemetry_sdk:: logs:: LogResult ;
66use opentelemetry_sdk:: Resource ;
77use std:: sync:: atomic;
8+ use std:: sync:: { Arc , Mutex } ;
89
910/// An OpenTelemetry exporter that writes Logs to stdout on export.
1011pub struct LogExporter {
@@ -29,30 +30,39 @@ impl fmt::Debug for LogExporter {
2930 }
3031}
3132
32- #[ async_trait]
3333impl opentelemetry_sdk:: export:: logs:: LogExporter for LogExporter {
3434 /// Export spans to stdout
35- async fn export ( & mut self , batch : LogBatch < ' _ > ) -> LogResult < ( ) > {
36- if self . is_shutdown . load ( atomic:: Ordering :: SeqCst ) {
37- return Err ( "exporter is shut down" . into ( ) ) ;
38- } else {
39- println ! ( "Logs" ) ;
40- if self . resource_emitted {
41- print_logs ( batch) ;
35+ fn export (
36+ & mut self ,
37+ batch : LogBatch < ' _ > ,
38+ ) -> impl std:: future:: Future < Output = LogResult < ( ) > > + Send {
39+ let is_shutdown = self . is_shutdown . load ( atomic:: Ordering :: SeqCst ) ;
40+ let resource_emitted_arc = Arc :: new ( Mutex :: new ( self . resource_emitted ) ) ;
41+ let resource_emitted_arc_clone = Arc :: clone ( & resource_emitted_arc) ;
42+ let resource = self . resource . clone ( ) ;
43+ async move {
44+ if is_shutdown {
45+ Err ( "exporter is shut down" . into ( ) )
4246 } else {
43- self . resource_emitted = true ;
44- println ! ( "Resource" ) ;
45- if let Some ( schema_url) = self . resource . schema_url ( ) {
46- println ! ( "\t Resource SchemaUrl: {:?}" , schema_url) ;
47+ println ! ( "Logs" ) ;
48+ let mut resource_emitted_guard = resource_emitted_arc_clone. lock ( ) . unwrap ( ) ;
49+ if * resource_emitted_guard {
50+ print_logs ( batch) ;
51+ } else {
52+ println ! ( "Resource" ) ;
53+ if let Some ( schema_url) = resource. schema_url ( ) {
54+ println ! ( "\t Resource SchemaUrl: {:?}" , schema_url) ;
55+ }
56+ resource. iter ( ) . for_each ( |( k, v) | {
57+ println ! ( "\t -> {}={:?}" , k, v) ;
58+ } ) ;
59+
60+ print_logs ( batch) ;
61+ * resource_emitted_guard = true ;
4762 }
48- self . resource . iter ( ) . for_each ( |( k, v) | {
49- println ! ( "\t -> {}={:?}" , k, v) ;
50- } ) ;
5163
52- print_logs ( batch ) ;
64+ Ok ( ( ) )
5365 }
54-
55- Ok ( ( ) )
5666 }
5767 }
5868
0 commit comments