Skip to content

Commit

Permalink
runutil: Add CloseWithLogOnErr function
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
  • Loading branch information
aknuds1 committed Oct 11, 2021
1 parent a9ba3a8 commit 1371a61
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
* [ENHANCEMENT] Replace go-kit/kit/log with go-kit/log. #52
* [ENHANCEMENT] Add spanlogger package. #42
* [BUGFIX] spanlogger: Support multiple tenant IDs. #59
* [ENHANCEMENT] Add runutil.CloseWithLogOnErr function. #58
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/gogo/status v1.1.0
github.com/golang/snappy v0.0.4
github.com/google/go-cmp v0.5.5
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/hashicorp/consul/api v1.9.1
Expand Down
15 changes: 0 additions & 15 deletions runutil/capture.go

This file was deleted.

31 changes: 31 additions & 0 deletions runutil/runutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package runutil

import (
"fmt"
"io"
"os"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"

"github.com/grafana/dskit/multierror"
)

// CloseWithErrCapture closes closer and wraps any error with the provided message and assigns it to err.
func CloseWithErrCapture(err *error, closer io.Closer, msg string) {
merr := multierror.New(*err, errors.Wrap(closer.Close(), msg))
*err = merr.Err()
}

// CloseWithLogOnErr closes an io.Closer and logs any relevant error from it wrapped with the provided format string and
// args.
func CloseWithLogOnErr(logger log.Logger, closer io.Closer, format string, args ...interface{}) {
err := closer.Close()
if err == nil || errors.Is(err, os.ErrClosed) {
return
}

msg := fmt.Sprintf(format, args...)
level.Warn(logger).Log("msg", "detected close error", "err", fmt.Sprintf("%s: %s", msg, err.Error()))
}
62 changes: 62 additions & 0 deletions runutil/runutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package runutil

import (
"fmt"
"os"
"testing"

"github.com/go-kit/log/level"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)

func TestCloseWithLogOnErr(t *testing.T) {
t.Run("With non-close error", func(t *testing.T) {
closer := fakeCloser{err: fmt.Errorf("an error")}
logger := fakeLogger{}

CloseWithLogOnErr(&logger, closer, "closing failed")

assert.Empty(t, cmp.Diff([]interface{}{
"level", level.WarnValue(), "msg", "detected close error", "err", "closing failed: an error",
}, logger.keyvals, cmp.Comparer(func(a, b fmt.Stringer) bool {
t.Logf("Comparing %q and %q", a.String(), b.String())
return a.String() == b.String()
})))
})

t.Run("With no error", func(t *testing.T) {
closer := fakeCloser{}
logger := fakeLogger{}

CloseWithLogOnErr(&logger, closer, "closing failed")

assert.Empty(t, logger.keyvals)
})

t.Run("With closed error", func(t *testing.T) {
closer := fakeCloser{err: os.ErrClosed}
logger := fakeLogger{}

CloseWithLogOnErr(&logger, closer, "closing failed")

assert.Empty(t, logger.keyvals)
})
}

type fakeCloser struct {
err error
}

func (c fakeCloser) Close() error {
return c.err
}

type fakeLogger struct {
keyvals []interface{}
}

func (l *fakeLogger) Log(keyvals ...interface{}) error {
l.keyvals = keyvals
return nil
}

0 comments on commit 1371a61

Please sign in to comment.