@@ -117,9 +117,8 @@ func (context *Context) Run(addressData RunAddressData, _ time.Duration) (res Re
117117
118118 runTimer , err := context .timer .NewNode (wafRunTag ,
119119 timer .WithComponents (
120- wafPersistentEncoderTag ,
121- wafEphemeralEncoderTag ,
122- wafDurationExtTag ,
120+ wafEncodeTag ,
121+ wafDecodeTag ,
123122 wafDurationTag ,
124123 ),
125124 )
@@ -129,22 +128,28 @@ func (context *Context) Run(addressData RunAddressData, _ time.Duration) (res Re
129128
130129 runTimer .Start ()
131130 defer func () {
132- context .metrics .add ("_dd.appsec.waf.run" , runTimer .Stop ())
131+ context .metrics .add (wafRunTag , runTimer .Stop ())
133132 context .metrics .merge (runTimer .Stats ())
134133 }()
135134
136- persistentData , persistentEncoder , err := context .encodeOneAddressType (addressData .Persistent , runTimer .MustLeaf (wafPersistentEncoderTag ))
135+ wafEncodeTimer := runTimer .MustLeaf (wafEncodeTag )
136+ wafEncodeTimer .Start ()
137+ persistentData , persistentEncoder , err := context .encodeOneAddressType (addressData .Persistent , wafEncodeTimer )
137138 if err != nil {
139+ wafEncodeTimer .Stop ()
138140 return res , err
139141 }
140142
141143 // The WAF releases ephemeral address data at the max of each run call, so we need not keep the Go values live beyond
142144 // that in the same way we need for persistent data. We hence use a separate encoder.
143- ephemeralData , ephemeralEncoder , err := context .encodeOneAddressType (addressData .Ephemeral , runTimer . MustLeaf ( wafEphemeralEncoderTag ) )
145+ ephemeralData , ephemeralEncoder , err := context .encodeOneAddressType (addressData .Ephemeral , wafEncodeTimer )
144146 if err != nil {
147+ wafEncodeTimer .Stop ()
145148 return res , err
146149 }
147150
151+ wafEncodeTimer .Stop ()
152+
148153 // ddwaf_run cannot run concurrently and we are going to mutate the context.cgoRefs, so we need to lock the context
149154 context .mutex .Lock ()
150155 defer context .mutex .Unlock ()
@@ -157,8 +162,8 @@ func (context *Context) Run(addressData RunAddressData, _ time.Duration) (res Re
157162 // into C ddwaf_objects. libddwaf's API requires to keep this data for the lifetime of the ddwaf_context.
158163 defer context .cgoRefs .append (persistentEncoder .cgoRefs )
159164
160- wafExtTimer := runTimer .MustLeaf (wafDurationExtTag )
161- res , err = context .run (persistentData , ephemeralData , wafExtTimer , runTimer .SumRemaining ())
165+ wafDecodeTimer := runTimer .MustLeaf (wafDecodeTag )
166+ res , err = context .run (persistentData , ephemeralData , wafDecodeTimer , runTimer .SumRemaining ())
162167
163168 runTimer .AddTime (wafDurationTag , res .TimeSpent )
164169
@@ -207,15 +212,15 @@ func merge[K comparable, V any](a, b map[K][]V) (merged map[K][]V) {
207212// If the addressData is empty, it returns nil for the WAF object and an empty ref pool.
208213// At this point, if the encoder does not timeout, the only error we can get is an error in case the top level object
209214// is a nil map, but this behaviour is expected since either persistent or ephemeral addresses are allowed to be null
210- // one at a time. In this case, EncodeAddresses will return nil contrary to Encode which will return a nil wafObject,
215+ // one at a time. In this case, Encode will return nil contrary to Encode which will return a nil wafObject,
211216// which is what we need to send to ddwaf_run to signal that the address data is empty.
212217func (context * Context ) encodeOneAddressType (addressData map [string ]any , timer timer.Timer ) (* bindings.WafObject , encoder , error ) {
213218 encoder := newLimitedEncoder (timer )
214219 if addressData == nil {
215220 return nil , encoder , nil
216221 }
217222
218- data , _ := encoder .EncodeAddresses (addressData )
223+ data , _ := encoder .Encode (addressData )
219224 if len (encoder .truncations ) > 0 {
220225 context .mutex .Lock ()
221226 defer context .mutex .Unlock ()
@@ -232,18 +237,18 @@ func (context *Context) encodeOneAddressType(addressData map[string]any, timer t
232237
233238// run executes the ddwaf_run call with the provided data on this context. The caller is responsible for locking the
234239// context appropriately around this call.
235- func (context * Context ) run (persistentData , ephemeralData * bindings.WafObject , wafTimer timer.Timer , timeBudget time.Duration ) (Result , error ) {
240+ func (context * Context ) run (persistentData , ephemeralData * bindings.WafObject , wafDecodeTimer timer.Timer , timeBudget time.Duration ) (Result , error ) {
236241 result := new (bindings.WafResult )
237242 defer wafLib .WafResultFree (result )
238243
239- wafTimer .Start ()
240- defer wafTimer .Stop ()
241-
242244 // The value of the timeout cannot exceed 2^55
243245 // cf. https://en.cppreference.com/w/cpp/chrono/duration
244246 timeout := uint64 (timeBudget .Microseconds ()) & 0x008FFFFFFFFFFFFF
245247 ret := wafLib .WafRun (context .cContext , persistentData , ephemeralData , result , timeout )
246248
249+ wafDecodeTimer .Start ()
250+ defer wafDecodeTimer .Stop ()
251+
247252 return unwrapWafResult (ret , result )
248253}
249254
0 commit comments