Skip to content

Commit

Permalink
Add option to add error breadcrumb in sentry log (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier authored and evalphobia committed Feb 4, 2019
1 parent ce87e9f commit ab0fa2e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ Other configuration options are:
- `StacktraceConfiguration.Skip` how many stack frames to skip before stacktrace starts recording.
- `StacktraceConfiguration.Context` the number of lines to include around a stack frame for context.
- `StacktraceConfiguration.InAppPrefixes` the prefixes that will be matched against the stack frame to identify it as in_app
- `StacktraceConfiguration.IncludeErrorBreadcrumb` whether to create a breadcrumb with the full text of error
41 changes: 37 additions & 4 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

"github.com/getsentry/raven-go"
raven "github.com/getsentry/raven-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -83,6 +83,8 @@ type StackTraceConfiguration struct {
SendExceptionType bool
// whether the exception type and message should be switched.
SwitchExceptionTypeAndMessage bool
// whether to include a breadcrumb with the full error stack
IncludeErrorBreadcrumb bool
}

// NewSentryHook creates a hook to be added to an instance of logger
Expand Down Expand Up @@ -163,13 +165,26 @@ func setAsync(hook *SentryHook) *SentryHook {
func (hook *SentryHook) Fire(entry *logrus.Entry) error {
hook.mu.RLock() // Allow multiple go routines to log simultaneously
defer hook.mu.RUnlock()
packet := raven.NewPacket(entry.Message)

df := newDataField(entry.Data)

err, hasError := df.getError()
var crumbs *Breadcrumbs
if hasError && hook.StacktraceConfiguration.IncludeErrorBreadcrumb {
crumbs = &Breadcrumbs{
Values: []Value{{
Timestamp: int64(time.Now().Unix()),
Type: "error",
Message: fmt.Sprintf("%+v", err),
}},
}
}

packet := raven.NewPacketWithExtra(entry.Message, nil, crumbs)
packet.Timestamp = raven.Timestamp(entry.Time)
packet.Level = severityMap[entry.Level]
packet.Platform = "go"

df := newDataField(entry.Data)

// set special fields
if hook.serverName != "" {
packet.ServerName = hook.serverName
Expand Down Expand Up @@ -389,3 +404,21 @@ func formatData(value interface{}) (formatted interface{}) {
return value
}
}

// utility classes for breadcrumb support
type Breadcrumbs struct {
Values []Value `json:"values"`
}

type Value struct {
Timestamp int64 `json:"timestamp"`
Type string `json:"type"`
Message string `json:"message"`
Category string `json:"category"`
Level string `json:"string"`
Data interface{} `json:"data"`
}

func (b *Breadcrumbs) Class() string {
return "breadcrumbs"
}

0 comments on commit ab0fa2e

Please sign in to comment.