@@ -77,33 +77,32 @@ impl ReduceState {
77
77
fn new ( e : LogEvent , strategies : & IndexMap < String , MergeStrategy > ) -> Self {
78
78
Self {
79
79
stale_since : Instant :: now ( ) ,
80
- // TODO: all_fields alternative that consumes
81
80
fields : e
82
- . all_fields ( )
81
+ . into_iter ( )
83
82
. filter_map ( |( k, v) | {
84
83
if let Some ( strat) = strategies. get ( & k) {
85
- match get_value_merger ( v. clone ( ) , strat) {
84
+ match get_value_merger ( v, strat) {
86
85
Ok ( m) => Some ( ( k, m) ) ,
87
86
Err ( err) => {
88
87
warn ! ( "failed to create merger for field '{}': {}" , k, err) ;
89
88
None
90
89
}
91
90
}
92
91
} else {
93
- Some ( ( k, v. clone ( ) . into ( ) ) )
92
+ Some ( ( k, v. into ( ) ) )
94
93
}
95
94
} )
96
95
. collect ( ) ,
97
96
}
98
97
}
99
98
100
99
fn add_event ( & mut self , e : LogEvent , strategies : & IndexMap < String , MergeStrategy > ) {
101
- for ( k, v) in e. all_fields ( ) {
100
+ for ( k, v) in e. into_iter ( ) {
102
101
let strategy = strategies. get ( & k) ;
103
102
match self . fields . entry ( k) {
104
103
hash_map:: Entry :: Vacant ( entry) => {
105
104
if let Some ( strat) = strategy {
106
- match get_value_merger ( v. clone ( ) , strat) {
105
+ match get_value_merger ( v, strat) {
107
106
Ok ( m) => {
108
107
entry. insert ( m) ;
109
108
}
@@ -291,6 +290,7 @@ mod test {
291
290
topology:: config:: { TransformConfig , TransformContext } ,
292
291
Event ,
293
292
} ;
293
+ use serde_json:: json;
294
294
295
295
#[ test]
296
296
fn reduce_from_condition ( ) {
@@ -493,4 +493,86 @@ identifier_fields = [ "request_id" ]
493
493
Value :: from( 7 )
494
494
) ;
495
495
}
496
+
497
+ #[ test]
498
+ fn arrays ( ) {
499
+ let mut reduce = toml:: from_str :: < ReduceConfig > (
500
+ r#"
501
+ identifier_fields = [ "request_id" ]
502
+
503
+ merge_strategies.foo = "array"
504
+ merge_strategies.bar = "concat"
505
+
506
+ [ends_when]
507
+ "test_end.exists" = true
508
+ "# ,
509
+ )
510
+ . unwrap ( )
511
+ . build ( TransformContext :: new_test ( ) )
512
+ . unwrap ( ) ;
513
+
514
+ let mut outputs = Vec :: new ( ) ;
515
+
516
+ let mut e = Event :: from ( "test message 1" ) ;
517
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( [ 1 , 3 ] ) ) ;
518
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( [ 1 , 3 ] ) ) ;
519
+ e. as_mut_log ( ) . insert ( "request_id" , "1" ) ;
520
+ reduce. transform_into ( & mut outputs, e) ;
521
+
522
+ let mut e = Event :: from ( "test message 2" ) ;
523
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( [ 2 , 4 ] ) ) ;
524
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( [ 2 , 4 ] ) ) ;
525
+ e. as_mut_log ( ) . insert ( "request_id" , "2" ) ;
526
+ reduce. transform_into ( & mut outputs, e) ;
527
+
528
+ let mut e = Event :: from ( "test message 3" ) ;
529
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( [ 5 , 7 ] ) ) ;
530
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( [ 5 , 7 ] ) ) ;
531
+ e. as_mut_log ( ) . insert ( "request_id" , "1" ) ;
532
+ reduce. transform_into ( & mut outputs, e) ;
533
+
534
+ let mut e = Event :: from ( "test message 4" ) ;
535
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( "done" ) ) ;
536
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( "done" ) ) ;
537
+ e. as_mut_log ( ) . insert ( "request_id" , "1" ) ;
538
+ e. as_mut_log ( ) . insert ( "test_end" , "yep" ) ;
539
+ reduce. transform_into ( & mut outputs, e) ;
540
+
541
+ assert_eq ! ( outputs. len( ) , 1 ) ;
542
+ assert_eq ! (
543
+ outputs. first( ) . unwrap( ) . as_log( ) [ & "foo" . into( ) ] ,
544
+ json!( [ [ 1 , 3 ] , [ 5 , 7 ] , "done" ] ) . into( )
545
+ ) ;
546
+
547
+ assert_eq ! ( outputs. len( ) , 1 ) ;
548
+ assert_eq ! (
549
+ outputs. first( ) . unwrap( ) . as_log( ) [ & "bar" . into( ) ] ,
550
+ json!( [ 1 , 3 , 5 , 7 , "done" ] ) . into( )
551
+ ) ;
552
+
553
+ outputs. clear ( ) ;
554
+
555
+ let mut e = Event :: from ( "test message 5" ) ;
556
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( [ 6 , 8 ] ) ) ;
557
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( [ 6 , 8 ] ) ) ;
558
+ e. as_mut_log ( ) . insert ( "request_id" , "2" ) ;
559
+ reduce. transform_into ( & mut outputs, e) ;
560
+
561
+ let mut e = Event :: from ( "test message 6" ) ;
562
+ e. as_mut_log ( ) . insert ( "foo" , json ! ( "done" ) ) ;
563
+ e. as_mut_log ( ) . insert ( "bar" , json ! ( "done" ) ) ;
564
+ e. as_mut_log ( ) . insert ( "request_id" , "2" ) ;
565
+ e. as_mut_log ( ) . insert ( "test_end" , "yep" ) ;
566
+ reduce. transform_into ( & mut outputs, e) ;
567
+
568
+ assert_eq ! ( outputs. len( ) , 1 ) ;
569
+ assert_eq ! (
570
+ outputs. first( ) . unwrap( ) . as_log( ) [ & "foo" . into( ) ] ,
571
+ json!( [ [ 2 , 4 ] , [ 6 , 8 ] , "done" ] ) . into( )
572
+ ) ;
573
+ assert_eq ! (
574
+ outputs. first( ) . unwrap( ) . as_log( ) [ & "bar" . into( ) ] ,
575
+ json!( [ 2 , 4 , 6 , 8 , "done" ] ) . into( )
576
+ ) ;
577
+ }
496
578
}
0 commit comments