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

Added sentry hint with the original errs #37

Open
wants to merge 5 commits 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
28 changes: 23 additions & 5 deletions core.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package zapsentry

import (
"errors"
"reflect"
"strings"
"time"

"github.com/pkg/errors"

"github.com/getsentry/sentry-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
}
}

_ = c.client.CaptureEvent(event, nil, c.scope())
_ = c.client.CaptureEvent(event, c.createHint(), c.scope())
}

// We may be crashing the program, so should flush any buffered events.
Expand Down Expand Up @@ -188,25 +189,32 @@ func (c *core) addExceptionsFromError(
err error,
) []sentry.Exception {
for i := 0; i < maxErrorDepth && err != nil; i++ {
wrappedErr := err
err = errors.Cause(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, why not keep the select logic below, and just keep track of a - possibly nil - previousErr, initialized outside the loop?

this way we need mentally less "special-casing" of this specific lib.

if _, ok := processedErrors[getTypeOf(err)]; ok {
return exceptions
}

processedErrors[getTypeOf(err)] = struct{}{}

exception := sentry.Exception{Value: err.Error(), Type: reflect.TypeOf(err).String()}
if strings.HasSuffix(exception.Type, "errors.fundamental") {
// err was created with `errors.New(msg)` - make `msg` the type,
Comment on lines +201 to +202
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually see *errors.errString here (see https://goplay.tools/snippet/txizx5D1JSQ).

Is the comment maybe not up-to-date? Or the check?

Also, this type doesn't seem to have ever been renamed since its introduction 12 years ago, but it may still be a bit brittle to check directly against it. Maybe it would be enough to check for any error type inside of errors?

// and get the wrapped value as the value
exception.Type = exception.Value
exception.Value = wrappedErr.Error()
}

if !c.cfg.DisableStacktrace {
exception.Stacktrace = sentry.ExtractStacktrace(err)
// extract from wrappedErr - if err was not wrapped, its the same
exception.Stacktrace = sentry.ExtractStacktrace(wrappedErr)
}

exceptions = append(exceptions, exception)

switch previousProvider := err.(type) {
case interface{ Unwrap() error }:
err = previousProvider.Unwrap()
case interface{ Cause() error }:
err = previousProvider.Cause()
default:
err = nil
}
Expand Down Expand Up @@ -300,6 +308,16 @@ func (c *core) GetClient() *sentry.Client {
return c.client
}

func (c *core) createHint() *sentry.EventHint {
hint := sentry.EventHint{
Data: c.errs,
}
if len(c.errs) > 0 {
hint.OriginalException = c.errs[0]
}
return &hint
}

type core struct {
client *sentry.Client
cfg *Configuration
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/pkg/errors v0.9.1
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
Expand Down