Skip to content

Commit

Permalink
Merge pull request #25 from Shopify/bugsnag-hook-ctx
Browse files Browse the repository at this point in the history
Pass along context from log entry to bugsnag hook
  • Loading branch information
tgwizard authored Mar 12, 2019
2 parents d5e742e + 198145b commit f4f17f5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
name = "github.com/stretchr/testify"
version = ">= 1.2.1"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = ">= 1.4.0"

[[override]]
name = "github.com/gorilla/mux"
branch = "subrouter-match-err"
Expand Down
3 changes: 3 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func ContextLog(ctx Valuer, err []error, entry *logrus.Entry) *logrus.Entry {

if ctx != nil {
entry = entry.WithFields(getLoggableValues(ctx))
if ctx, ok := ctx.(context.Context); ok {
entry = entry.WithContext(ctx)
}
}

if len(err) != 0 {
Expand Down
6 changes: 5 additions & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
)

type contextKeyType string

func buildLogger() (Logger, *bytes.Buffer) {
buf := bytes.NewBuffer(nil)
logrusLogger := logrus.New()
Expand Down Expand Up @@ -110,7 +112,8 @@ func TestContextLog(t *testing.T) {

GlobalFields["testKey"] = "value"

ctx := context.Background()
ctx := context.WithValue(context.Background(), contextKeyType("ctxValue"), "value")

ctx = WithField(ctx, "bar", "baz")
entry := logger(ctx, nil).WithField("a", "b")
assert.Equal(t, logrus.Fields{
Expand All @@ -119,6 +122,7 @@ func TestContextLog(t *testing.T) {
"a": "b",
"testKey": "value",
}, entry.Data)
assert.Equal(t, ctx, entry.Context)
}

func TestLogIfError(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion logrusbugsnag/bugsnag.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func (hook *Hook) Fire(entry *logrus.Entry) error {
}

errWithStack := bugsnag_errors.New(notifyErr, skipFrames)
bugsnagErr := bugsnag.Notify(errWithStack, metadata)

// Pass in our own metadata as well as the entry context, which might contain more data that Bugsnag can extract.
bugsnagErr := bugsnag.Notify(errWithStack, metadata, entry.Context)
if bugsnagErr != nil {
return ErrBugsnagSendFailed{bugsnagErr}
}
Expand Down

0 comments on commit f4f17f5

Please sign in to comment.