@@ -90,7 +90,14 @@ impl LogToMetric {
90
90
}
91
91
92
92
impl Transform for LogToMetric {
93
+ // Only used in tests
93
94
fn transform ( & mut self , event : Event ) -> Option < Event > {
95
+ let mut output = Vec :: new ( ) ;
96
+ self . transform_into ( & mut output, event) ;
97
+ output. pop ( )
98
+ }
99
+
100
+ fn transform_into ( & mut self , output : & mut Vec < Event > , event : Event ) {
94
101
let event = event. into_log ( ) ;
95
102
96
103
for metric in self . config . metrics . iter ( ) {
@@ -99,17 +106,16 @@ impl Transform for LogToMetric {
99
106
if let Some ( val) = event. get ( & counter. field ) {
100
107
if counter. increment_by_value {
101
108
if let Ok ( val) = val. to_string_lossy ( ) . parse :: < f32 > ( ) {
102
- return Some ( Event :: Metric ( Metric :: Counter {
109
+ output . push ( Event :: Metric ( Metric :: Counter {
103
110
name : counter. sanitized_name . to_string ( ) ,
104
111
val : val as u32 ,
105
112
sampling : None ,
106
113
} ) ) ;
107
114
} else {
108
115
trace ! ( "failed to parse counter value" ) ;
109
- return None ;
110
116
}
111
117
} else {
112
- return Some ( Event :: Metric ( Metric :: Counter {
118
+ output . push ( Event :: Metric ( Metric :: Counter {
113
119
name : counter. sanitized_name . to_string ( ) ,
114
120
val : 1 ,
115
121
sampling : None ,
@@ -120,21 +126,18 @@ impl Transform for LogToMetric {
120
126
MetricConfig :: Gauge ( gauge) => {
121
127
if let Some ( val) = event. get ( & gauge. field ) {
122
128
if let Ok ( val) = val. to_string_lossy ( ) . parse ( ) {
123
- return Some ( Event :: Metric ( Metric :: Gauge {
129
+ output . push ( Event :: Metric ( Metric :: Gauge {
124
130
name : gauge. sanitized_name . to_string ( ) ,
125
131
val,
126
132
direction : None ,
127
133
} ) ) ;
128
134
} else {
129
135
trace ! ( "failed to parse gauge value" ) ;
130
- return None ;
131
136
}
132
137
}
133
138
}
134
139
}
135
140
}
136
-
137
- None
138
141
}
139
142
}
140
143
@@ -328,4 +331,51 @@ mod tests {
328
331
let mut transform = LogToMetric :: new ( & config) ;
329
332
assert ! ( transform. transform( log) . is_none( ) ) ;
330
333
}
334
+
335
+ #[ test]
336
+ fn multiple_metrics ( ) {
337
+ let config: LogToMetricConfig = toml:: from_str (
338
+ r##"
339
+ [[metrics]]
340
+ type = "counter"
341
+ field = "status"
342
+ labels = {status = "#{event.status}", host = "#{event.host}"}
343
+
344
+ [[metrics]]
345
+ type = "counter"
346
+ field = "backtrace"
347
+ name = "exception_total"
348
+ labels = {host = "#{event.host}"}
349
+ "## ,
350
+ )
351
+ . unwrap ( ) ;
352
+
353
+ let mut log = Event :: from ( "i am a log" ) ;
354
+ log. as_mut_log ( )
355
+ . insert_explicit ( "status" . into ( ) , "42" . into ( ) ) ;
356
+ log. as_mut_log ( )
357
+ . insert_explicit ( "backtrace" . into ( ) , "message" . into ( ) ) ;
358
+
359
+ let mut transform = LogToMetric :: new ( & config) ;
360
+
361
+ let mut output = Vec :: new ( ) ;
362
+ transform. transform_into ( & mut output, log) ;
363
+ assert_eq ! ( 2 , output. len( ) ) ;
364
+ assert_eq ! (
365
+ output. pop( ) . unwrap( ) . into_metric( ) ,
366
+ Metric :: Counter {
367
+ name: "exception_total" . into( ) ,
368
+ val: 1 ,
369
+ sampling: None
370
+ }
371
+ ) ;
372
+ assert_eq ! (
373
+ output. pop( ) . unwrap( ) . into_metric( ) ,
374
+ Metric :: Counter {
375
+ name: "status_total" . into( ) ,
376
+ val: 1 ,
377
+ sampling: None
378
+ }
379
+ ) ;
380
+ }
331
381
}
0 commit comments