@@ -5,7 +5,6 @@ package processing
55
66import (
77 "crypto/tls"
8- "fmt"
98 "sync"
109 "time"
1110
@@ -103,31 +102,11 @@ func (fh *ForwardHandler) EnableRDNS(expiryPeriod time.Duration) {
103102// AddFields enables the addition of a custom set of top-level fields to the
104103// forwarded JSON.
105104func (fh * ForwardHandler ) AddFields (fields map [string ]string ) error {
106- j := ""
107- // We preprocess the JSON to be able to only use fast string operations
108- // later. This code progressively builds a JSON snippet by adding JSON
109- // key-value pairs for each added field, e.g. `, "foo":"bar"`.
110- for k , v := range fields {
111- // Escape the fields to make sure we do not mess up the JSON when
112- // encountering weird symbols in field names or values.
113- kval , err := util .EscapeJSON (k )
114- if err != nil {
115- fh .Logger .Warningf ("cannot escape value: %s" , v )
116- return err
117- }
118- vval , err := util .EscapeJSON (v )
119- if err != nil {
120- fh .Logger .Warningf ("cannot escape value: %s" , v )
121- return err
122- }
123- j += fmt .Sprintf (",%s:%s" , kval , vval )
105+ addedFields , err := util .PreprocessAddedFields (fields )
106+ if err != nil {
107+ return err
124108 }
125- // We finish the list of key-value pairs with a final brace:
126- // `, "foo":"bar"}`. This string can now just replace the final brace in a
127- // given JSON string. If there were no added fields, we just leave the
128- // output at the final brace.
129- j += "}"
130- fh .AddedFields = j
109+ fh .AddedFields = addedFields
131110 return nil
132111}
133112
0 commit comments