Skip to content

Commit

Permalink
fix: report _dd.appsec.waf.duration even when the WAF does not match …
Browse files Browse the repository at this point in the history
…on anything (#87)
  • Loading branch information
eliottness authored Mar 26, 2024
1 parent 21fdf8f commit 52b767f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ func unwrapWafResult(ret bindings.WafReturnCode, result *bindings.WafResult) (re
res.Derivatives, err = decodeMap(&result.Derivatives)
}

res.TimeSpent = time.Duration(result.TotalRuntime) * time.Nanosecond

if ret == bindings.WafOK {
return res, err
}
Expand All @@ -282,7 +284,6 @@ func unwrapWafResult(ret bindings.WafReturnCode, result *bindings.WafResult) (re
}
}

res.TimeSpent = time.Duration(result.TotalRuntime)
return res, err
}

Expand Down
2 changes: 1 addition & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
func (stats Stats) Metrics() map[string]any {
tags := make(map[string]any, len(stats.Timers)+len(stats.Truncations)+1)
for k, v := range stats.Timers {
tags[k] = uint64(v.Microseconds())
tags[k] = float64(v.Nanoseconds()) / float64(time.Microsecond) // The metrics should be in microseconds
}

tags[wafTimeoutTag] = stats.TimeoutCount
Expand Down
18 changes: 16 additions & 2 deletions waf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ func TestTimeout(t *testing.T) {
"my.input": "Arachni",
}

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

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

t.Run("not-empty-metrics-no-match", func(t *testing.T) {
context := NewContextWithBudget(waf, time.Hour)
require.NotNil(t, context)
defer context.Close()

_, err := context.Run(RunAddressData{Persistent: map[string]any{"my.input": "curl/7.88"}}, 0)
require.NoError(t, err)
require.NotEmpty(t, context.Stats())
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.decode"])
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.encode"])
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.duration_ext"])
require.NotZero(t, context.Stats().Timers["_dd.appsec.waf.duration"])
})

t.Run("timeout-persistent-encoder", func(t *testing.T) {
context := NewContextWithBudget(waf, time.Millisecond)
require.NotNil(t, context)
Expand Down

0 comments on commit 52b767f

Please sign in to comment.