From 8ce3556d31e46a7846042f1750d5644137f8e97d Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Tue, 7 Jul 2015 11:41:45 -0500 Subject: [PATCH 1/2] Added a new SentryHook initialization func for setting tags. --- hooks/sentry/sentry.go | 15 +++++++++++++-- hooks/sentry/sentry_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/hooks/sentry/sentry.go b/hooks/sentry/sentry.go index e7e45b2..4d184b2 100644 --- a/hooks/sentry/sentry.go +++ b/hooks/sentry/sentry.go @@ -2,8 +2,8 @@ package logrus_sentry import ( "fmt" - "time" "net/http" + "time" "github.com/Sirupsen/logrus" "github.com/getsentry/raven-go" @@ -68,7 +68,18 @@ type SentryHook struct { // and initializes the raven client. // This method sets the timeout to 100 milliseconds. func NewSentryHook(DSN string, levels []logrus.Level) (*SentryHook, error) { - client, err := raven.NewClient(DSN, nil) + client, err := raven.New(DSN) + if err != nil { + return nil, err + } + return &SentryHook{100 * time.Millisecond, client, levels}, nil +} + +// NewWithTagsSentryHook creates a hook with tags to be added to an instance +// of logger and initializes the raven client. This method sets the timeout to +// 100 milliseconds. +func NewWithTagsSentryHook(DSN string, tags map[string]string, levels []logrus.Level) (*SentryHook, error) { + client, err := raven.NewWithTags(DSN, tags) if err != nil { return nil, err } diff --git a/hooks/sentry/sentry_test.go b/hooks/sentry/sentry_test.go index 5f3696b..5f59f69 100644 --- a/hooks/sentry/sentry_test.go +++ b/hooks/sentry/sentry_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "reflect" "strings" "testing" @@ -98,3 +99,34 @@ func TestSentryHandler(t *testing.T) { } }) } + +func TestSentryTags(t *testing.T) { + WithTestDSN(t, func(dsn string, pch <-chan *raven.Packet) { + logger := getTestLogger() + tags := map[string]string{ + "site": "test", + } + levels := []logrus.Level{ + logrus.ErrorLevel, + } + + hook, err := NewWithTagsSentryHook(dsn, tags, levels) + if err != nil { + t.Fatal(err.Error()) + } + + logger.Hooks.Add(hook) + + logger.Error(message) + packet := <-pch + expected := raven.Tags{ + raven.Tag{ + Key: "site", + Value: "test", + }, + } + if !reflect.DeepEqual(packet.Tags, expected) { + t.Errorf("message should have been %s, was %s", message, packet.Message) + } + }) +} From afe474b84f0d7f6a2878b66be93e46155c40d2e4 Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Tue, 7 Jul 2015 11:52:22 -0500 Subject: [PATCH 2/2] updated readme with usage example --- hooks/sentry/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/hooks/sentry/README.md b/hooks/sentry/README.md index 4e1c147..8b1f9a1 100644 --- a/hooks/sentry/README.md +++ b/hooks/sentry/README.md @@ -31,6 +31,22 @@ func main() { } ``` +If you wish to initialize a SentryHook with tags, you can use the `NewWithTagsSentryHook` constructor to provide default tags: + +```go +tags := map[string]string{ + "site": "example.com", +} +levels := []logrus.Level{ + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel, +} +hook, err := logrus_sentry.NewWithTagsSentryHook(YOUR_DSN, tags, levels) + +``` + + ## Special fields Some logrus fields have a special meaning in this hook,