Skip to content

Commit

Permalink
Use new official sdk for sentry hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Jun 28, 2019
1 parent 0331655 commit ebd9f98
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 89 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@ import (
)

var (
dsn = "http://60a0257d7b5a429a8838e5f2ba873ec9:cb785a64cd3649ea987a1f2f5fad5e82@example.com/1"
dsn = "http://[email protected]/1"
)

func main() {
 filenameHook := filename.NewHook()
filenameHook.Field = "custom_source_field" // Customize source field name
log.AddHook(filenameHook)

sentryHook := sentry.NewHook(dsn, log.PanicLevel, log.FatalLevel, log.ErrorLevel)
log.AddHook(sentryHook)
log.AddHook(sentry.NewHook(sentry.Options{
Dsn: dsn,
}, log.PanicLevel, log.FatalLevel, log.ErrorLevel))

request, err := http.NewRequest("GET", "http://example.com", nil)
log.WithFields(log.Fields{
"error": err,
"sentry": sentry.NewSentry(
sentry.WithHttpRequest(request),
sentry.WithUser("22", "testuser", "[email protected]", "127.0.0.1"),
),
}).Info("Request info")
log.WithError(err).Error("Dead beef")
}

```
125 changes: 47 additions & 78 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
@@ -1,128 +1,97 @@
package sentry

import (
"fmt"
"net/http"

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

var (
levelsMap = map[logrus.Level]raven.Severity{
logrus.PanicLevel: raven.FATAL,
logrus.FatalLevel: raven.FATAL,
logrus.ErrorLevel: raven.ERROR,
logrus.WarnLevel: raven.WARNING,
logrus.InfoLevel: raven.INFO,
logrus.DebugLevel: raven.DEBUG,
levelsMap = map[logrus.Level]sentry.Level{
logrus.PanicLevel: sentry.LevelFatal,
logrus.FatalLevel: sentry.LevelFatal,
logrus.ErrorLevel: sentry.LevelError,
logrus.WarnLevel: sentry.LevelWarning,
logrus.InfoLevel: sentry.LevelInfo,
logrus.DebugLevel: sentry.LevelDebug,
logrus.TraceLevel: sentry.LevelDebug,
}
)

const (
SentryField = "sentry"
)
type Options sentry.ClientOptions

type Hook struct {
client *raven.Client
client *sentry.Client
levels []logrus.Level
Async bool
SentryField string
tags map[string]string
release string
environment string
}

func (hook *Hook) Levels() []logrus.Level {
return hook.levels
}

func (hook *Hook) Fire(entry *logrus.Entry) error {
culprit := ""
interfaces := []raven.Interface{
&raven.Message{Message: entry.Message},
}
if err, ok := entry.Data[logrus.ErrorKey]; ok && err != nil {
culprit = fmt.Sprintf("%s", err)
entry.Data[logrus.ErrorKey] = culprit
exceptions := []sentry.Exception{}

if err, ok := entry.Data[logrus.ErrorKey].(error); ok && err != nil {
stacktrace := sentry.ExtractStacktrace(err)
if stacktrace == nil {
stacktrace = sentry.NewStacktrace()
}
exceptions = append(exceptions, sentry.Exception{
Type: entry.Message,
Value: err.Error(),
Stacktrace: stacktrace,
})
}

if intfs, ok := entry.Data[hook.SentryField].([]raven.Interface); ok {
interfaces = append(interfaces, intfs...)
delete(entry.Data, hook.SentryField)
event := sentry.Event{
Level: levelsMap[entry.Level],
Message: entry.Message,
Extra: map[string]interface{}(entry.Data),
Tags: hook.tags,
Environment: hook.environment,
Release: hook.release,
Exception: exceptions,
}

packet := &raven.Packet{
Message: entry.Message,
Level: levelsMap[entry.Level],
Interfaces: interfaces,
Extra: raven.Extra(entry.Data),
Culprit: culprit,
}

_, ch := hook.client.Capture(packet, map[string]string{})
if !hook.Async || entry.Level == logrus.FatalLevel || entry.Level == logrus.PanicLevel {
return <-ch
}
hub := sentry.CurrentHub()
hook.client.CaptureEvent(&event, nil, hub.Scope())

return nil
}

func (hook *Hook) SetTags(tags map[string]string) {
hook.client.Tags = tags
hook.tags = tags
}

func (hook *Hook) AddTag(key, value string) {
hook.client.Tags[key] = value
hook.tags[key] = value
}

func (hook *Hook) SetRelease(release string) {
hook.client.SetRelease(release)
hook.release = release
}

func (hook *Hook) SetEnvironment(environment string) {
hook.client.SetEnvironment(environment)
hook.environment = environment
}

func NewHook(dsn string, levels ...logrus.Level) *Hook {
client, err := raven.New(dsn)
func NewHook(optinos Options, levels ...logrus.Level) (*Hook, error) {
client, err := sentry.NewClient(sentry.ClientOptions(optinos))
if err != nil {
logrus.WithError(err).Error("Set DSN error")
return nil, err
}

client.Tags = map[string]string{}

hook := Hook{
client: client,
levels: levels,
Async: true,
SentryField: SentryField,
client: client,
levels: levels,
tags: map[string]string{},
}
if len(hook.levels) == 0 {
hook.levels = logrus.AllLevels
}

return &hook
}

func NewSentry(interfaces ...raven.Interface) []raven.Interface {
return interfaces
}

func WithStacktrace(skip int, context int, appPackagePrefixes []string) *raven.Stacktrace {
return raven.NewStacktrace(skip, context, appPackagePrefixes)
}

func WithHttpRequest(request *http.Request) *raven.Http {
return raven.NewHttp(request)
}

func WithException(err error, stacktrace *raven.Stacktrace) *raven.Exception {
return raven.NewException(err, stacktrace)
}

func WithUser(id, username, email, ip string) *raven.User {
return &raven.User{
ID: id,
Username: username,
Email: email,
IP: ip,
}
return &hook, nil
}

0 comments on commit ebd9f98

Please sign in to comment.