Skip to content

Commit

Permalink
Illegal argument errors will no longer be logged at the ERROR level
Browse files Browse the repository at this point in the history
  • Loading branch information
getvictor committed Jan 24, 2025
1 parent ff43593 commit 75671e4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions changes/25759-illegal-argument-errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Illegal argument errors will no longer be logged at the ERROR level on the server. Since these are client errors, they will be logged at the DEBUG level instead. This will reduce the amount of noise in the server logs and help debugging other issues.
32 changes: 16 additions & 16 deletions server/contexts/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ func WithNoUser(ctx context.Context) context.Context {
return ctx
}

// WithNoError returns a context with logging.SkipError set to true so error won't be logged at level=error
func WithNoError(ctx context.Context) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetSkipError()
}
return ctx
}

// WithExtras returns a context with logging.Extras set as the values provided
func WithExtras(ctx context.Context, extras ...interface{}) context.Context {
if logCtx, ok := FromContext(ctx); ok {
Expand All @@ -87,7 +79,6 @@ type LoggingContext struct {
Extras []interface{}
SkipUser bool
ForceLevel func(kitlog.Logger) kitlog.Logger
SkipError bool
}

func (l *LoggingContext) SetForceLevel(level func(kitlog.Logger) kitlog.Logger) {
Expand All @@ -108,12 +99,6 @@ func (l *LoggingContext) SetSkipUser() {
l.SkipUser = true
}

func (l *LoggingContext) SetSkipError() {
l.l.Lock()
defer l.l.Unlock()
l.SkipError = true
}

func (l *LoggingContext) SetStartTime() {
l.l.Lock()
defer l.l.Unlock()
Expand All @@ -132,7 +117,7 @@ func (l *LoggingContext) Log(ctx context.Context, logger kitlog.Logger) {
defer l.l.Unlock()

switch {
case len(l.Errs) > 0 && !l.SkipError:
case l.setLevelError():
logger = level.Error(logger)
case l.ForceLevel != nil:
logger = l.ForceLevel(logger)
Expand Down Expand Up @@ -208,3 +193,18 @@ func (l *LoggingContext) Log(ctx context.Context, logger kitlog.Logger) {

_ = logger.Log(keyvals...)
}

func (l *LoggingContext) setLevelError() bool {
if len(l.Errs) == 0 {
return false
}

if len(l.Errs) == 1 {
var ew fleet.ErrWithIsServerError
if errors.As(l.Errs[0], &ew) && !ew.IsServerError() {
return false
}
}

return true
}
2 changes: 2 additions & 0 deletions server/docs/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Validate API inputs and return a 4XX status code if invalid. If you did not do a

Inputs corresponding to sortable or indexed DB fields should be preprocessed (trim spaces, normalize Unicode, etc.). Use utility method `fleet.Preprocess(input string) string`. [Backend sync where discussed](https://us-65885.app.gong.io/call?id=4055688254267958899).

Invalid inputs should NOT log a server error. Server errors should be reserved for unexpected/serious issues. `InvalidArgumentError` implements `IsServerError` method to indicate that it is a client error. [Backend sync where discussed](https://us-65885.app.gong.io/call?id=6515110653090875786&highlights=%5B%7B%22type%22%3A%22SHARE%22%2C%22from%22%3A340%2C%22to%22%3A1578%7D%5D).

### JSON unmarshaling

`PATCH` API calls often need to distinguish between a field being set to `null` and a field not being present in the JSON. Use the structs from `optjson` package to handle this. [Backend sync where discussed](https://us-65885.app.gong.io/call?id=4055688254267958899). [JSON unmarshaling article and example](https://victoronsoftware.com/posts/go-json-unmarshal/).
Expand Down
14 changes: 13 additions & 1 deletion server/fleet/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ type ErrWithRetryAfter interface {
RetryAfter() int
}

// ErrWithIsServerError is an interface for errors that explicitly specify
// whether they are server errors or not. By default, errors are treated as
// server errors.
type ErrWithIsServerError interface {
error
IsServerError() bool
}

type invalidArgWithStatusError struct {
InvalidArgumentError
code int
Expand Down Expand Up @@ -102,7 +110,7 @@ func (e *ErrorWithUUID) UUID() string {
}

// InvalidArgumentError is the error returned when invalid data is presented to
// a service method.
// a service method. It is a client error.
type InvalidArgumentError struct {
Errors []InvalidArgument

Expand All @@ -123,6 +131,10 @@ func NewInvalidArgumentError(name, reason string) *InvalidArgumentError {
return &invalid
}

func (e InvalidArgumentError) IsServerError() bool {
return false
}

func (e *InvalidArgumentError) Append(name, reason string) {
e.Errors = append(e.Errors, InvalidArgument{
name: name,
Expand Down
2 changes: 0 additions & 2 deletions server/service/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2378,8 +2378,6 @@ func bootstrapPackageMetadataEndpoint(ctx context.Context, request interface{},
meta, err := svc.GetMDMAppleBootstrapPackageMetadata(ctx, req.TeamID, req.ForUpdate)
switch {
case fleet.IsNotFound(err):
// Don't log this response as error -- it's expected to happen when the bootstrap package is missing, which is a common case.
logging.WithNoError(ctx)
return bootstrapPackageMetadataResponse{Err: fleet.NewInvalidArgumentError("team_id",
"bootstrap package for this team does not exist").WithStatus(http.StatusNotFound)}, nil
case err != nil:
Expand Down

0 comments on commit 75671e4

Please sign in to comment.