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

fix: missing stack trace for parsing error in logrusentry #689

Merged
merged 8 commits into from
Apr 2, 2024
68 changes: 50 additions & 18 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package sentrylogrus
import (
"errors"
"net/http"
"reflect"
"time"

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

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

// The identifier of the Logrus SDK.
Expand Down Expand Up @@ -188,36 +190,66 @@ func (h *Hook) entryToEvent(l *logrus.Entry) *sentry.Event {
}

func (h *Hook) exceptions(err error) []sentry.Exception {
if err == nil {
return nil
}

if !h.hub.Client().Options().AttachStacktrace {
return []sentry.Exception{{
Type: "error",
Type: reflect.TypeOf(err).String(),
Value: err.Error(),
}}
}
excs := []sentry.Exception{}
var last *sentry.Exception
for ; err != nil; err = errors.Unwrap(err) {
exc := sentry.Exception{
Type: "error",

var excs []sentry.Exception
for err != nil {
// Add the current error to the exception slice with its details
excs = append(excs, sentry.Exception{
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
Stacktrace: sentry.ExtractStacktrace(err),
})

// Attempt to unwrap the error using the standard library's Unwrap method.
// If errors.Unwrap returns nil, it means either there is no error to unwrap,
// or the error does not implement the Unwrap method.
unwrappedErr := errors.Unwrap(err)

if unwrappedErr == nil {
break
}
if last != nil && exc.Value == last.Value {
if last.Stacktrace == nil {
last.Stacktrace = exc.Stacktrace
continue
}
if exc.Stacktrace == nil {
continue
}
}
excs = append(excs, exc)
last = &excs[len(excs)-1]

err = unwrappedErr
}

// Add a trace of the current stack to the most recent error in a chain if
// it doesn't have a stack trace yet.
if excs[0].Stacktrace == nil {
excs[0].Stacktrace = sentry.NewStacktrace()
}

if len(excs) <= 1 {
return excs
}

// reverse
for i, j := 0, len(excs)-1; i < j; i, j = i+1, j-1 {
excs[i], excs[j] = excs[j], excs[i]
}

for i := range excs {
excs[i].Mechanism = &sentry.Mechanism{
Data: map[string]any{
"is_exception_group": true,
"exception_id": i,
},
}
if i == 0 {
continue
}
excs[i].Mechanism.Data["parent_id"] = i - 1
}

return excs
}
ribice marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading
Loading