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

Add option to enable person context using special fields #47

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
37 changes: 31 additions & 6 deletions hook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rollrus

import (
"context"
"fmt"
"runtime"
"strings"
Expand All @@ -19,6 +20,7 @@ type Hook struct {
*rollbar.Client
triggers []logrus.Level
ignoredErrors []error
setPerson bool
ignoreErrorFunc func(error) bool
ignoreFunc func(error, map[string]interface{}) bool

Expand Down Expand Up @@ -86,24 +88,47 @@ func (r *Hook) report(entry *logrus.Entry, cause error, m map[string]interface{}

r.reported = true

ctx := context.TODO()

if r.setPerson {
userID, userName, userEmail := "", "", ""

if userIDInterface, exists := m["user_id"]; !exists {
userID = fmt.Sprint(userIDInterface)
}

if userIDInterface, exists := m["user_name"]; !exists {
userName = fmt.Sprint(userIDInterface)
}

if userIDInterface, exists := m["user_email"]; !exists {
userEmail = fmt.Sprint(userIDInterface)
}

if userID != "" {
ctx = rollbar.NewPersonContext(ctx, &rollbar.Person{Id: userID, Username: userName, Email: userEmail})
}
}

switch {
case level == logrus.FatalLevel || level == logrus.PanicLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtras(rollbar.CRIT, cause, skip, m)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.CRIT, cause, skip, m)
r.Client.Wait()
case level == logrus.ErrorLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtras(rollbar.ERR, cause, skip, m)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.ERR, cause, skip, m)
case level == logrus.WarnLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtras(rollbar.WARN, cause, skip, m)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.WARN, cause, skip, m)
case level == logrus.InfoLevel:
r.Client.MessageWithExtras(rollbar.INFO, entry.Message, m)
r.Client.MessageWithExtrasAndContext(ctx, rollbar.INFO, entry.Message, m)
case level == logrus.DebugLevel:
r.Client.MessageWithExtras(rollbar.DEBUG, entry.Message, m)
r.Client.MessageWithExtrasAndContext(ctx, rollbar.DEBUG, entry.Message, m)
case level == logrus.TraceLevel:
r.Client.MessageWithExtras(rollbar.DEBUG, entry.Message, m)
r.Client.MessageWithExtrasAndContext(ctx, rollbar.DEBUG, entry.Message, m)
}

}

// convertFields converts from log.Fields to map[string]interface{} so that we can
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ func WithIgnoreFunc(fn func(err error, fields map[string]interface{}) bool) Opti
h.ignoreFunc = fn
}
}

// WithPersonFunc is an OptionFunc that will set the person in the Rollbar alert
// using the special error fields user_id, user_name and user_email
func WithPersonFunc(setPerson bool) OptionFunc {
return func(h *Hook) {
h.setPerson = setPerson
}
}