@@ -18,31 +18,39 @@ package transformers
1818
1919import (
2020 "context"
21- "fmt"
2221 "strings"
2322
2423 v1 "github.com/dataflow-operator/dataflow/api/v1"
2524 "github.com/dataflow-operator/dataflow/internal/types"
25+ "github.com/go-logr/logr"
2626 "github.com/tidwall/gjson"
2727)
2828
2929// RouterTransformer routes messages to different sinks based on conditions
3030type RouterTransformer struct {
3131 config * v1.RouterTransformation
32+ logger logr.Logger
3233}
3334
3435// NewRouterTransformer creates a new router transformer
3536func NewRouterTransformer (config * v1.RouterTransformation ) * RouterTransformer {
3637 return & RouterTransformer {
3738 config : config ,
39+ logger : logr .Discard (),
3840 }
3941}
4042
43+ // SetLogger sets the logger for the transformer (used by processor to inject logr)
44+ func (r * RouterTransformer ) SetLogger (logger logr.Logger ) {
45+ r .logger = logger
46+ }
47+
4148// Transform routes messages based on conditions
4249// Returns messages with routing metadata
4350func (r * RouterTransformer ) Transform (ctx context.Context , message * types.Message ) ([]* types.Message , error ) {
44- fmt .Printf ("DEBUG Router: Processing message, routes count: %d\n " , len (r .config .Routes ))
45- fmt .Printf ("DEBUG Router: Message data: %s\n " , string (message .Data ))
51+ r .logger .V (1 ).Info ("Router processing message" ,
52+ "routesCount" , len (r .config .Routes ),
53+ "dataSize" , len (message .Data ))
4654
4755 for i , route := range r .config .Routes {
4856 // Check if condition contains comparison operator (==)
@@ -51,19 +59,24 @@ func (r *RouterTransformer) Transform(ctx context.Context, message *types.Messag
5159 var expectedValue string
5260 var isComparison bool
5361
54- fmt .Printf ("DEBUG Router: Checking route %d, condition: '%s'\n " , i , condition )
62+ r .logger .V (1 ).Info ("Router checking route" ,
63+ "routeIndex" , i ,
64+ "condition" , condition )
5565
5666 // Parse condition like "$.type == 'order'" or "$.type"
5767 if idx := findComparisonOperator (condition ); idx >= 0 {
5868 // Trim spaces from field path
5969 fieldPath = strings .TrimSpace (condition [:idx ])
6070 expectedValue = extractStringValue (condition [idx :])
6171 isComparison = true
62- fmt .Printf ("DEBUG Router: Parsed comparison - fieldPath: '%s', expectedValue: '%s'\n " , fieldPath , expectedValue )
72+ r .logger .V (1 ).Info ("Router parsed comparison" ,
73+ "fieldPath" , fieldPath ,
74+ "expectedValue" , expectedValue )
6375 } else {
6476 fieldPath = condition
6577 isComparison = false
66- fmt .Printf ("DEBUG Router: No comparison operator found, using fieldPath: '%s'\n " , fieldPath )
78+ r .logger .V (1 ).Info ("Router using field path (no comparison)" ,
79+ "fieldPath" , fieldPath )
6780 }
6881
6982 // Remove $. prefix if present (gjson doesn't need it for root fields)
@@ -77,19 +90,25 @@ func (r *RouterTransformer) Transform(ctx context.Context, message *types.Messag
7790 result := gjson .GetBytes (message .Data , fieldPath )
7891
7992 if ! result .Exists () {
80- fmt .Printf ("DEBUG Router: Field '%s' does not exist in message\n " , fieldPath )
93+ r .logger .V (1 ).Info ("Router field does not exist" ,
94+ "fieldPath" , fieldPath )
8195 continue
8296 }
8397
84- fmt .Printf ("DEBUG Router: Field '%s' exists, value: '%s' (raw: %v)\n " , fieldPath , result .String (), result .Value ())
98+ r .logger .V (1 ).Info ("Router field exists" ,
99+ "fieldPath" , fieldPath ,
100+ "value" , result .String ())
85101
86102 // Check if condition is true
87103 var isTrue bool
88104 if isComparison {
89105 // For comparison, check if value matches expected
90106 value := result .String ()
91107 isTrue = value == expectedValue
92- fmt .Printf ("DEBUG Router: Comparison result: value='%s' == expected='%s' = %v\n " , value , expectedValue , isTrue )
108+ r .logger .V (1 ).Info ("Router comparison result" ,
109+ "value" , value ,
110+ "expectedValue" , expectedValue ,
111+ "match" , isTrue )
93112 } else {
94113 // For simple existence check, use truthiness
95114 value := result .Value ()
@@ -116,14 +135,16 @@ func (r *RouterTransformer) Transform(ctx context.Context, message *types.Messag
116135 }
117136 newMsg .Metadata ["routed_condition" ] = route .Condition
118137 newMsg .Timestamp = message .Timestamp
119- // Debug: log routing decision
120- fmt .Printf ("DEBUG Router: Message routed to condition '%s', value='%s', expected='%s'\n " ,
121- route .Condition , result .String (), expectedValue )
138+ r .logger .V (1 ).Info ("Router message routed" ,
139+ "condition" , route .Condition ,
140+ "value" , result .String (),
141+ "expectedValue" , expectedValue )
122142 return []* types.Message {newMsg }, nil
123143 } else if isComparison {
124- // Debug: log why condition didn't match
125- fmt .Printf ("DEBUG Router: Condition '%s' didn't match: value='%s', expected='%s'\n " ,
126- route .Condition , result .String (), expectedValue )
144+ r .logger .V (1 ).Info ("Router condition did not match" ,
145+ "condition" , route .Condition ,
146+ "value" , result .String (),
147+ "expectedValue" , expectedValue )
127148 }
128149 }
129150
0 commit comments