@@ -11,10 +11,8 @@ use tracing_subscriber::Layer;
11
11
const INSTRUMENTATION_LIBRARY_NAME : & str = "opentelemetry-appender-tracing" ;
12
12
13
13
/// Visitor to record the fields from the event record.
14
- #[ derive( Default ) ]
15
- struct EventVisitor {
16
- log_record_attributes : Vec < ( Key , AnyValue ) > ,
17
- log_record_body : Option < AnyValue > ,
14
+ struct EventVisitor < ' a , LR : LogRecord > {
15
+ log_record : & ' a mut LR ,
18
16
}
19
17
20
18
/// Logs from the log crate have duplicated attributes that we removed here.
@@ -37,59 +35,61 @@ fn get_filename(filepath: &str) -> &str {
37
35
filepath
38
36
}
39
37
40
- impl EventVisitor {
38
+ impl < ' a , LR : LogRecord > EventVisitor < ' a , LR > {
39
+ fn new ( log_record : & ' a mut LR ) -> Self {
40
+ EventVisitor { log_record }
41
+ }
41
42
fn visit_metadata ( & mut self , meta : & Metadata ) {
42
- self . log_record_attributes
43
- . push ( ( "name" . into ( ) , meta. name ( ) . into ( ) ) ) ;
43
+ self . log_record
44
+ . add_attribute ( Key :: new ( "name" ) , AnyValue :: from ( meta. name ( ) ) ) ;
44
45
45
46
#[ cfg( feature = "experimental_metadata_attributes" ) ]
46
47
self . visit_experimental_metadata ( meta) ;
47
48
}
48
49
49
50
#[ cfg( feature = "experimental_metadata_attributes" ) ]
50
51
fn visit_experimental_metadata ( & mut self , meta : & Metadata ) {
51
- self . log_record_attributes
52
- . push ( ( "log.target" . into ( ) , meta. target ( ) . to_owned ( ) . into ( ) ) ) ;
52
+ self . log_record . add_attribute (
53
+ Key :: new ( "log.target" ) ,
54
+ AnyValue :: from ( meta. target ( ) . to_owned ( ) ) ,
55
+ ) ;
53
56
54
57
if let Some ( module_path) = meta. module_path ( ) {
55
- self . log_record_attributes
56
- . push ( ( "code.namespace" . into ( ) , module_path. to_owned ( ) . into ( ) ) ) ;
58
+ self . log_record . add_attribute (
59
+ Key :: new ( "code.namespace" ) ,
60
+ AnyValue :: from ( module_path. to_owned ( ) ) ,
61
+ ) ;
57
62
}
58
63
59
64
if let Some ( filepath) = meta. file ( ) {
60
- self . log_record_attributes
61
- . push ( ( "code.filepath" . into ( ) , filepath. to_owned ( ) . into ( ) ) ) ;
62
- self . log_record_attributes . push ( (
63
- "code.filename" . into ( ) ,
64
- get_filename ( filepath) . to_owned ( ) . into ( ) ,
65
- ) ) ;
65
+ self . log_record . add_attribute (
66
+ Key :: new ( "code.filepath" ) ,
67
+ AnyValue :: from ( filepath. to_owned ( ) ) ,
68
+ ) ;
69
+ self . log_record . add_attribute (
70
+ Key :: new ( "code.filename" ) ,
71
+ AnyValue :: from ( get_filename ( filepath) . to_owned ( ) ) ,
72
+ ) ;
66
73
}
67
74
68
75
if let Some ( line) = meta. line ( ) {
69
- self . log_record_attributes
70
- . push ( ( "code.lineno" . into ( ) , line. into ( ) ) ) ;
71
- }
72
- }
73
-
74
- fn push_to_otel_log_record < LR : LogRecord > ( self , log_record : & mut LR ) {
75
- if let Some ( body) = self . log_record_body {
76
- log_record. set_body ( body) ;
76
+ self . log_record
77
+ . add_attribute ( Key :: new ( "code.lineno" ) , AnyValue :: from ( line) ) ;
77
78
}
78
- log_record. add_attributes ( self . log_record_attributes ) ;
79
79
}
80
80
}
81
81
82
- impl tracing:: field:: Visit for EventVisitor {
82
+ impl < ' a , LR : LogRecord > tracing:: field:: Visit for EventVisitor < ' a , LR > {
83
83
fn record_debug ( & mut self , field : & tracing:: field:: Field , value : & dyn std:: fmt:: Debug ) {
84
84
#[ cfg( feature = "experimental_metadata_attributes" ) ]
85
85
if is_duplicated_metadata ( field. name ( ) ) {
86
86
return ;
87
87
}
88
88
if field. name ( ) == "message" {
89
- self . log_record_body = Some ( format ! ( "{value :?}" ) . into ( ) ) ;
89
+ self . log_record . set_body ( format ! ( "{:?}" , value ) . into ( ) ) ;
90
90
} else {
91
- self . log_record_attributes
92
- . push ( ( field. name ( ) . into ( ) , format ! ( "{value:?}" ) . into ( ) ) ) ;
91
+ self . log_record
92
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
93
93
}
94
94
}
95
95
@@ -98,27 +98,27 @@ impl tracing::field::Visit for EventVisitor {
98
98
if is_duplicated_metadata ( field. name ( ) ) {
99
99
return ;
100
100
}
101
- self . log_record_attributes
102
- . push ( ( field. name ( ) . into ( ) , value. to_owned ( ) . into ( ) ) ) ;
101
+ self . log_record
102
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value. to_owned ( ) ) ) ;
103
103
}
104
104
105
105
fn record_bool ( & mut self , field : & tracing_core:: Field , value : bool ) {
106
- self . log_record_attributes
107
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
106
+ self . log_record
107
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
108
108
}
109
109
110
110
fn record_f64 ( & mut self , field : & tracing:: field:: Field , value : f64 ) {
111
- self . log_record_attributes
112
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
111
+ self . log_record
112
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
113
113
}
114
114
115
115
fn record_i64 ( & mut self , field : & tracing:: field:: Field , value : i64 ) {
116
116
#[ cfg( feature = "experimental_metadata_attributes" ) ]
117
117
if is_duplicated_metadata ( field. name ( ) ) {
118
118
return ;
119
119
}
120
- self . log_record_attributes
121
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
120
+ self . log_record
121
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
122
122
}
123
123
124
124
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
@@ -173,11 +173,10 @@ where
173
173
log_record. set_severity_number ( severity_of_level ( meta. level ( ) ) ) ;
174
174
log_record. set_severity_text ( meta. level ( ) . to_string ( ) . into ( ) ) ;
175
175
176
- let mut visitor = EventVisitor :: default ( ) ;
176
+ let mut visitor = EventVisitor :: new ( & mut log_record ) ;
177
177
visitor. visit_metadata ( meta) ;
178
178
// Visit fields.
179
179
event. record ( & mut visitor) ;
180
- visitor. push_to_otel_log_record ( & mut log_record) ;
181
180
182
181
self . logger . emit ( log_record) ;
183
182
}
0 commit comments