Skip to content

Commit

Permalink
Merge pull request #58 from grafana/feat/add-close-with-log-on-err
Browse files Browse the repository at this point in the history
runutil: Add CloseWithLogOnErr function
  • Loading branch information
aknuds1 authored Oct 11, 2021
2 parents a9ba3a8 + cfe7bef commit 3a88ec0
Show file tree
Hide file tree
Showing 4 changed files with 90 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
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()))
}
58 changes: 58 additions & 0 deletions runutil/runutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package runutil

import (
"fmt"
"os"
"testing"

"github.com/go-kit/log/level"
"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.Equal(t, []interface{}{
"level", level.WarnValue(), "msg", "detected close error", "err", "closing failed: an error",
}, logger.keyvals)
})

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 3a88ec0

Please sign in to comment.