Skip to content

Commit

Permalink
Do not log context.Canceled error
Browse files Browse the repository at this point in the history
ref #1556
  • Loading branch information
louischan-oursky committed Oct 4, 2021
2 parents acabb78 + cf41343 commit c49782d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/lib/deps/middleware_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func (m *RequestMiddleware) Handle(next http.Handler) http.Handler {
appCtx, err := m.ConfigSource.ProvideContext(r)
if err != nil {
if errors.Is(err, configsource.ErrAppNotFound) {
logger.WithError(err).Error("app not found")
http.Error(w, configsource.ErrAppNotFound.Error(), http.StatusNotFound)
} else {
logger.WithError(err).Error("failed to resolve config")
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/log/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package log

import (
"context"
"errors"

"github.com/sirupsen/logrus"
)

// Ignore reports whether the entry should be logged.
func Ignore(entry *logrus.Entry) bool {
if err, ok := entry.Data[logrus.ErrorKey].(error); ok {
if errors.Is(err, context.Canceled) {
return true
}
}

return false
}
40 changes: 40 additions & 0 deletions pkg/util/log/ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package log

import (
"context"
"fmt"
"testing"

"github.com/sirupsen/logrus"

. "github.com/smartystreets/goconvey/convey"
)

func TestIgnore(t *testing.T) {
Convey("Ignore", t, func() {

Convey("Ignore entry with context.Canceled error", func() {
logger := logrus.New()
entry := logrus.NewEntry(logger)
entry = entry.WithError(context.Canceled)

So(Ignore(entry), ShouldBeTrue)
})

Convey("Ignore entry with wrapped context.Canceled error", func() {
logger := logrus.New()
entry := logrus.NewEntry(logger)

entry = entry.WithError(fmt.Errorf("wrap: %w", context.Canceled))

So(Ignore(entry), ShouldBeTrue)
})

Convey("Do not ignore any other entry", func() {
logger := logrus.New()
entry := logrus.NewEntry(logger)

So(Ignore(entry), ShouldBeFalse)
})
})
}
6 changes: 6 additions & 0 deletions pkg/util/sentry/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/getsentry/sentry-go"

"github.com/sirupsen/logrus"

"github.com/authgear/authgear-server/pkg/util/log"
)

var LogHookLevels = []logrus.Level{
Expand Down Expand Up @@ -35,6 +37,10 @@ func (h *LogHook) Fire(entry *logrus.Entry) error {
return nil
}

if log.Ignore(entry) {
return nil
}

event := makeEvent(entry)
h.hub.CaptureEvent(event)
return nil
Expand Down

0 comments on commit c49782d

Please sign in to comment.