Skip to content

Commit 52b767f

Browse files
authored
fix: report _dd.appsec.waf.duration even when the WAF does not match on anything (#87)
1 parent 21fdf8f commit 52b767f

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ func unwrapWafResult(ret bindings.WafReturnCode, result *bindings.WafResult) (re
261261
res.Derivatives, err = decodeMap(&result.Derivatives)
262262
}
263263

264+
res.TimeSpent = time.Duration(result.TotalRuntime) * time.Nanosecond
265+
264266
if ret == bindings.WafOK {
265267
return res, err
266268
}
@@ -282,7 +284,6 @@ func unwrapWafResult(ret bindings.WafReturnCode, result *bindings.WafResult) (re
282284
}
283285
}
284286

285-
res.TimeSpent = time.Duration(result.TotalRuntime)
286287
return res, err
287288
}
288289

metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
func (stats Stats) Metrics() map[string]any {
3838
tags := make(map[string]any, len(stats.Timers)+len(stats.Truncations)+1)
3939
for k, v := range stats.Timers {
40-
tags[k] = uint64(v.Microseconds())
40+
tags[k] = float64(v.Nanoseconds()) / float64(time.Microsecond) // The metrics should be in microseconds
4141
}
4242

4343
tags[wafTimeoutTag] = stats.TimeoutCount

waf_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ func TestTimeout(t *testing.T) {
368368
"my.input": "Arachni",
369369
}
370370

371-
t.Run("not-empty-metricsStore", func(t *testing.T) {
372-
context := NewContextWithBudget(waf, time.Millisecond)
371+
t.Run("not-empty-metrics-match", func(t *testing.T) {
372+
context := NewContextWithBudget(waf, time.Hour)
373373
require.NotNil(t, context)
374374
defer context.Close()
375375

@@ -382,6 +382,20 @@ func TestTimeout(t *testing.T) {
382382
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.duration"])
383383
})
384384

385+
t.Run("not-empty-metrics-no-match", func(t *testing.T) {
386+
context := NewContextWithBudget(waf, time.Hour)
387+
require.NotNil(t, context)
388+
defer context.Close()
389+
390+
_, err := context.Run(RunAddressData{Persistent: map[string]any{"my.input": "curl/7.88"}}, 0)
391+
require.NoError(t, err)
392+
require.NotEmpty(t, context.Stats())
393+
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.decode"])
394+
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.encode"])
395+
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.duration_ext"])
396+
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.duration"])
397+
})
398+
385399
t.Run("timeout-persistent-encoder", func(t *testing.T) {
386400
context := NewContextWithBudget(waf, time.Millisecond)
387401
require.NotNil(t, context)

0 commit comments

Comments
 (0)