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

Adding context.Context hub extractor for sentry-logrus integration #522

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions logrus/hubextractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sentrylogrus

import (
"github.com/sirupsen/logrus"

"github.com/getsentry/sentry-go"
)

var DefaultContextExtractor ContextHubFunc = func(entry *logrus.Entry) *sentry.Hub {
if ctx := entry.Context; ctx != nil {
hub := sentry.GetHubFromContext(ctx)
if hub != nil {
return hub
}
}
return nil
}
34 changes: 28 additions & 6 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/http"
"time"

sentry "github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"

sentry "github.com/getsentry/sentry-go"
)

// These default log field keys are used to pass specific metadata in a way that
Expand Down Expand Up @@ -37,10 +38,11 @@ const (
// It is not safe to configure the hook while logging is happening. Please
// perform all configuration before using it.
type Hook struct {
hub *sentry.Hub
fallback FallbackFunc
keys map[string]string
levels []logrus.Level
hub *sentry.Hub
fallback FallbackFunc
contextHub ContextHubFunc
keys map[string]string
levels []logrus.Level
}

var _ logrus.Hook = &Hook{}
Expand Down Expand Up @@ -86,6 +88,18 @@ func (h *Hook) SetFallback(fb FallbackFunc) {
h.fallback = fb
}

// A ContextHubFunc can be used to extract current sentry.Hub from context.
// It useful when you need capture event with started tracing
type ContextHubFunc func(*logrus.Entry) *sentry.Hub

// SetContextHub set up a context hub extractor, which will be called in Fire() method.
// If extractor return non-nil *sentry.Hub, this sentry.Hub will be used for capturing
// events. It might be used, when you have already filled sentry.Hub, like with set Contexts
// for distributing tracing
func (h *Hook) SetContextHub(ch ContextHubFunc) {
h.contextHub = ch
}

// SetKey sets an alternate field key. Use this if the default values conflict
// with other loggers, for instance. You may pass "" for new, to unset an
// existing alternate.
Expand Down Expand Up @@ -117,7 +131,15 @@ func (h *Hook) Levels() []logrus.Level {
// Fire sends entry to Sentry.
func (h *Hook) Fire(entry *logrus.Entry) error {
event := h.entryToEvent(entry)
if id := h.hub.CaptureEvent(event); id == nil {

hub := h.hub
if h.contextHub != nil {
if contextHub := h.contextHub(entry); contextHub != nil {
hub = contextHub
}
}

if id := hub.CaptureEvent(event); id == nil {
if h.fallback != nil {
return h.fallback(entry)
}
Expand Down
37 changes: 37 additions & 0 deletions logrus/logrusentry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentrylogrus

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -489,3 +490,39 @@ func Test_exceptions(t *testing.T) {
})
}
}

func Test_hubExtractor(t *testing.T) {
opts := sentry.ClientOptions{}
opts.Dsn = testDSN

sentry.Init(opts)
client := sentry.CurrentHub().Client()

hook := NewFromClient([]logrus.Level{logrus.ErrorLevel}, client)

defaultEntry := &logrus.Entry{
Level: logrus.ErrorLevel,
}

if err := hook.Fire(defaultEntry); err != nil {
t.Error("errored as default")
}

hook.SetContextHub(DefaultContextExtractor)

hub := sentry.CurrentHub().Clone()
hub.BindClient(nil)
ctx := sentry.SetHubOnContext(context.Background(), hub)

entryWithContext := &logrus.Entry{
Level: logrus.ErrorLevel,
Context: ctx,
}

err := hook.Fire(entryWithContext)
if err == nil {
t.Error("must error, 'cause hub is invalid for sending")
} else if err.Error() != "failed to send to sentry" {
t.Error("not right error")
}
}