Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass along context from log entry to bugsnag hook #25

Merged
merged 2 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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