Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP-108640 Added ignoredCriticalfPatterns #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions error_reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"sync"

"cloud.google.com/go/errorreporting"
Expand All @@ -13,7 +14,7 @@ type ErrorReporter interface {
Close() error
FlushReports()
Report(errReport ErrorReport)
WrapLogger(logging Logging, errorAffectsLabel string) Logging
WrapLogger(logging Logging, errorAffectsLabel string, ignoredCriticalfPatterns []*regexp.Regexp) Logging
}

type googleErrorReporter struct {
Expand Down Expand Up @@ -71,12 +72,13 @@ func (g *googleErrorReporter) Close() error {
}

type errorForwardingLogger struct {
wrappedLogger Logging
errorReporter ErrorReporter
errorAffectsLabel string
labelsLock *sync.RWMutex
labels map[string]string
req *http.Request
wrappedLogger Logging
errorReporter ErrorReporter
errorAffectsLabel string
ignoredCriticalfPatterns []*regexp.Regexp
labelsLock *sync.RWMutex
labels map[string]string
req *http.Request
}

func (e errorForwardingLogger) Debugf(format string, args ...interface{}) {
Expand Down Expand Up @@ -135,6 +137,14 @@ func (f forwardedError) Error() string {

func (e *errorForwardingLogger) forwardError(err error) {
var affects string
if len(e.ignoredCriticalfPatterns) > 0 {
errString := err.Error()
for _, pattern := range e.ignoredCriticalfPatterns {
if pattern.MatchString(errString) {
return
}
}
}
if len(e.errorAffectsLabel) > 0 {
e.labelsLock.RLock()
affects = e.labels[e.errorAffectsLabel]
Expand All @@ -150,15 +160,16 @@ func (e *errorForwardingLogger) forwardError(err error) {
/**
* Unlike NewGoogleErrorReporter this function may be used liberally, and across threads/requests
*/
func (g *googleErrorReporter) WrapLogger(logging Logging, errorAffectsLabel string) Logging {
func (g *googleErrorReporter) WrapLogger(logging Logging, errorAffectsLabel string, ignoredCriticalfPatterns []*regexp.Regexp) Logging {
if _, alreadyWrapped := logging.(*errorForwardingLogger); alreadyWrapped {
panic("bug! this logger is already wrapped!")
}
return &errorForwardingLogger{
wrappedLogger: logging,
errorReporter: g,
errorAffectsLabel: errorAffectsLabel,
labelsLock: &sync.RWMutex{},
labels: make(map[string]string),
wrappedLogger: logging,
errorReporter: g,
errorAffectsLabel: errorAffectsLabel,
ignoredCriticalfPatterns: ignoredCriticalfPatterns,
labelsLock: &sync.RWMutex{},
labels: make(map[string]string),
}
}
10 changes: 7 additions & 3 deletions error_reporting_mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package appwrap

import "github.com/stretchr/testify/mock"
import (
"regexp"

"github.com/stretchr/testify/mock"
)

type ErrorReporterMock struct {
mock.Mock
Expand All @@ -19,7 +23,7 @@ func (m *ErrorReporterMock) Close() error {
return args.Error(0)
}

func (m *ErrorReporterMock) WrapLogger(logging Logging, errorAffectsLabel string) Logging {
args := m.Called(logging, errorAffectsLabel)
func (m *ErrorReporterMock) WrapLogger(logging Logging, errorAffectsLabel string, ignoredCriticalfPatterns []*regexp.Regexp) Logging {
args := m.Called(logging, errorAffectsLabel, ignoredCriticalfPatterns)
return args.Get(0).(Logging)
}
39 changes: 35 additions & 4 deletions error_reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appwrap
import (
"errors"
"net/http"
"regexp"
"sync"

"cloud.google.com/go/errorreporting"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (e *ErrorReportingTest) TestWrapLogger_CantWrapAgain(c *C) {
reporter := &googleErrorReporter{}

assert.Panics(c, func() {
_ = reporter.WrapLogger(log, "")
_ = reporter.WrapLogger(log, "", nil)
})
}

Expand All @@ -83,7 +84,7 @@ func (e *ErrorReportingTest) TestDebugf_OnlyForwards(c *C) {

wrapMe.On("Debugf", "winner, winner, chicken din%s", "ner")

wrappedLog := reporter.WrapLogger(wrapMe, "")
wrappedLog := reporter.WrapLogger(wrapMe, "", nil)

wrappedLog.Debugf("winner, winner, chicken din%s", "ner")

Expand All @@ -98,7 +99,7 @@ func (e *ErrorReportingTest) TestInfof_OnlyForwards(c *C) {

wrapMe.On("Infof", "winner, winner, chicken din%s", "ner")

wrappedLog := reporter.WrapLogger(wrapMe, "")
wrappedLog := reporter.WrapLogger(wrapMe, "", nil)

wrappedLog.Infof("winner, winner, chicken din%s", "ner")

Expand All @@ -113,7 +114,7 @@ func (e *ErrorReportingTest) TestWarningf_OnlyForwards(c *C) {

wrapMe.On("Warningf", "winner, winner, chicken din%s", "ner")

wrappedLog := reporter.WrapLogger(wrapMe, "")
wrappedLog := reporter.WrapLogger(wrapMe, "", nil)

wrappedLog.Warningf("winner, winner, chicken din%s", "ner")

Expand Down Expand Up @@ -162,6 +163,36 @@ func (e *ErrorReportingTest) TestCriticalf_ForwardsAndReports(c *C) {
mockReporter.AssertExpectations(c)
}

func (e *ErrorReportingTest) TestCriticalf_Ignores(c *C) {
wrapMe := &LoggingMock{Log: &NullLogger{}}
mockReporter := &ErrorReporterMock{}
forwardLog := &errorForwardingLogger{
wrappedLogger: wrapMe,
errorReporter: mockReporter,
errorAffectsLabel: "",
ignoredCriticalfPatterns: []*regexp.Regexp{
regexp.MustCompile(".*context canceled.*"),
},
labelsLock: &sync.RWMutex{},
labels: make(map[string]string),
}
contextErr := "this says context canceled"
notIgnoredErr := "show me this error"

wrapMe.On("Criticalf", notIgnoredErr)
wrapMe.On("Criticalf", contextErr)
mockReporter.On("Report", ErrorReport{
Err: forwardedError{msg: notIgnoredErr},
Req: nil,
ErrorAffectsKey: "",
})
forwardLog.Criticalf(notIgnoredErr)
forwardLog.Criticalf(contextErr)

wrapMe.AssertExpectations(c)
mockReporter.AssertExpectations(c)
}

func (e *ErrorReportingTest) TestAddLabels_ErrorAffectsKeyIsSetToValue(c *C) {
wrapMe := &LoggingMock{Log: &NullLogger{}}
mockReporter := &ErrorReporterMock{}
Expand Down