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 <[email protected]>
  • Loading branch information
aknuds1 committed Oct 8, 2021
1 parent 4c56601 commit be82604
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
* [ENHANCEMENT] Add limiter package. #41
* [ENHANCEMENT] Add grpcclient, grpcencoding and grpcutil packages. #39
* [ENHANCEMENT] Replace go-kit/kit/log with go-kit/log. #52
* [ENHANCEMENT] Add spanlogger package. #42
* [ENHANCEMENT] Add spanlogger package. #42
* [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 be82604

Please sign in to comment.