Skip to content

Commit

Permalink
metrics: Count context.DeadlineExceeded as timeout (#10594)
Browse files Browse the repository at this point in the history
Fixes the response.errors.timeout counter.

---------

Signed-off-by: Marc Lopez Rubio <[email protected]>
  • Loading branch information
marclop authored Apr 4, 2023
1 parent d4b7b71 commit 32a167b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelogs/8.7.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ https://github.com/elastic/apm-server/compare/v8.7.0\...v8.7.1[View commits]
==== Bug fixes
- Fix indexing failures for metric, error and log documents when `agent.activation_method` is set {pull}10552[10552]
- Use droppedspan outcome in service destination aggregation {pull}10592[10592]
- metrics: Count context.DeadlineExceeded as timeout {pull}10594[10594]

[float]
[[release-notes-8.7.0]]
Expand Down
2 changes: 1 addition & 1 deletion internal/beater/middleware/timeout_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TimeoutMiddleware() Middleware {
h(c)

err := c.Request.Context().Err()
if errors.Is(err, context.Canceled) {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
c.Result.SetDefault(request.IDResponseErrorsTimeout)
c.Result.Err = tErr
c.Result.Body = tErr.Error()
Expand Down
54 changes: 36 additions & 18 deletions internal/beater/middleware/timeout_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,49 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/elastic/apm-server/internal/beater/request"
)

func TestTimeoutMiddleware(t *testing.T) {
var err error
m := TimeoutMiddleware()
h := request.Handler(func(c *request.Context) {
ctx := c.Request.Context()
ctx, cancel := context.WithCancel(ctx)
r := c.Request.WithContext(ctx)
c.Request = r
cancel()
})

h, err = m(h)
assert.NoError(t, err)
test := func(t *testing.T, handler request.Handler) {
var err error
h, err := TimeoutMiddleware()(handler)
assert.NoError(t, err)

c := request.NewContext()
r, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)
c.Reset(httptest.NewRecorder(), r)
h(c)
c := request.NewContext()
r, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)
c.Reset(httptest.NewRecorder(), r)
h(c)

assert.Equal(t, http.StatusServiceUnavailable, c.Result.StatusCode)
assert.Equal(t, http.StatusServiceUnavailable, c.Result.StatusCode)
}
t.Run("Cancelled", func(t *testing.T) {
test(t, request.Handler(func(c *request.Context) {
ctx := c.Request.Context()
ctx, cancel := context.WithCancel(ctx)
r := c.Request.WithContext(ctx)
c.Request = r
cancel()
}))
})
t.Run("DeadlineExceeded", func(t *testing.T) {
var cancel func()
defer func() {
if cancel != nil {
cancel()
}
}()
test(t, request.Handler(func(c *request.Context) {
ctx := c.Request.Context()
ctx, cancel = context.WithTimeout(ctx, time.Nanosecond)
r := c.Request.WithContext(ctx)
c.Request = r
time.Sleep(time.Second)
}))
})
}

0 comments on commit 32a167b

Please sign in to comment.