Skip to content

No log error CI #297

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions middleware/grpc_logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"errors"
"time"

"golang.org/x/net/context"
Expand Down Expand Up @@ -31,6 +32,9 @@ func (s GRPCServerLog) UnaryServerInterceptor(ctx context.Context, req interface
if err == nil && s.DisableRequestSuccessLog {
return resp, nil
}
if errors.Is(err, DoNotLogError{}) {
return resp, err
}

entry := user.LogWith(ctx, s.Log).WithFields(logging.Fields{"method": info.FullMethod, "duration": time.Since(begin)})
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func NewLogMiddleware(log logging.Interface, logRequestHeaders bool, logRequestA
}
}

// This can be used with `errors.Is` to see if the error marked itself as not to be logged.
// E.g. if the error is caused by overload, then we don't want to log it because that uses more resource.
type DoNotLogError struct{ Err error }

func (i DoNotLogError) Error() string { return i.Err.Error() }
func (i DoNotLogError) Unwrap() error { return i.Err }
func (i DoNotLogError) Is(target error) bool { _, ok := target.(DoNotLogError); return ok }

// logWithRequest information from the request and context as fields.
func (l Log) logWithRequest(r *http.Request) logging.Interface {
localLog := l.Log
Expand Down Expand Up @@ -83,6 +91,9 @@ func (l Log) Wrap(next http.Handler) http.Handler {
statusCode, writeErr := wrapped.getStatusCode(), wrapped.getWriteError()

if writeErr != nil {
if errors.Is(writeErr, DoNotLogError{}) {
return
}
if errors.Is(writeErr, context.Canceled) {
if l.LogRequestAtInfoLevel {
requestLog.Infof("%s %s %s, request cancelled: %s ws: %v; %s", r.Method, uri, time.Since(begin), writeErr, IsWSHandshakeRequest(r), headers)
Expand Down
6 changes: 6 additions & 0 deletions middleware/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func TestBadWriteLogging(t *testing.T) {
}, {
err: nil,
logContains: []string{"debug", "GET http://example.com/foo (200)"},
}, {
err: DoNotLogError{Err: errors.New("yolo")},
logContains: nil,
}} {
buf := bytes.NewBuffer(nil)
logrusLogger := logrus.New()
Expand All @@ -51,6 +54,9 @@ func TestBadWriteLogging(t *testing.T) {
}
loggingHandler.ServeHTTP(w, req)

if len(tc.logContains) == 0 {
require.Empty(t, buf)
}
for _, content := range tc.logContains {
require.True(t, bytes.Contains(buf.Bytes(), []byte(content)))
}
Expand Down