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

Revert "Add ability to skip frames (#852)" #882

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
23 changes: 9 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,9 @@
return client.options
}

type EventOptions struct {
SkipFrames int
}

// CaptureMessage captures an arbitrary message.
func (client *Client) CaptureMessage(message string, hint *EventHint, scope EventModifier, opts ...EventOptions) *EventID {
event := client.EventFromMessage(message, LevelInfo, opts...)
func (client *Client) CaptureMessage(message string, hint *EventHint, scope EventModifier) *EventID {
event := client.EventFromMessage(message, LevelInfo)
return client.CaptureEvent(event, hint, scope)
}

Expand All @@ -444,7 +440,7 @@

// CaptureEvent captures an event on the currently active client if any.
//
// The event must already be assembled. Typically, code would instead use
// The event must already be assembled. Typically code would instead use
// the utility methods like CaptureException. The return value is the
// event ID. In case Sentry is disabled or event was dropped, the return value will be nil.
func (client *Client) CaptureEvent(event *Event, hint *EventHint, scope EventModifier) *EventID {
Expand All @@ -453,7 +449,7 @@

// Recover captures a panic.
// Returns EventID if successfully, or nil if there's no error to recover from.
func (client *Client) Recover(err interface{}, hint *EventHint, scope EventModifier, opts ...EventOptions) *EventID {
func (client *Client) Recover(err interface{}, hint *EventHint, scope EventModifier) *EventID {
if err == nil {
err = recover()
}
Expand All @@ -463,7 +459,7 @@
// is store the Context in the EventHint and there nil means the Context is
// not available.
// nolint: staticcheck
return client.RecoverWithContext(nil, err, hint, scope, opts...)
return client.RecoverWithContext(nil, err, hint, scope)
}

// RecoverWithContext captures a panic and passes relevant context object.
Expand All @@ -473,7 +469,6 @@
err interface{},
hint *EventHint,
scope EventModifier,
opts ...EventOptions,
) *EventID {
if err == nil {
err = recover()
Expand All @@ -496,9 +491,9 @@
case error:
event = client.EventFromException(err, LevelFatal)
case string:
event = client.EventFromMessage(err, LevelFatal, opts...)
event = client.EventFromMessage(err, LevelFatal)
default:
event = client.EventFromMessage(fmt.Sprintf("%#v", err), LevelFatal, opts...)
event = client.EventFromMessage(fmt.Sprintf("%#v", err), LevelFatal)
}
return client.CaptureEvent(event, hint, scope)
}
Expand All @@ -519,7 +514,7 @@
}

// EventFromMessage creates an event from the given message string.
func (client *Client) EventFromMessage(message string, level Level, opts ...EventOptions) *Event {
func (client *Client) EventFromMessage(message string, level Level) *Event {
if message == "" {
err := usageError{fmt.Errorf("%s called with empty message", callerFunctionName())}
return client.EventFromException(err, level)
Expand All @@ -530,7 +525,7 @@

if client.options.AttachStacktrace {
event.Threads = []Thread{{
Stacktrace: NewStacktrace(opts...),
Stacktrace: NewStacktrace(),

Check warning on line 528 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L528

Added line #L528 was not covered by tests
Crashed: false,
Current: true,
}}
Expand Down
36 changes: 15 additions & 21 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@ type Stacktrace struct {
}

// NewStacktrace creates a stacktrace using runtime.Callers.
func NewStacktrace(opts ...EventOptions) *Stacktrace {
func NewStacktrace() *Stacktrace {
pcs := make([]uintptr, 100)
n := runtime.Callers(1, pcs)

if n == 0 {
return nil
}

skipFrames := 0
if len(opts) > 0 {
skipFrames = opts[0].SkipFrames
}

runtimeFrames := extractFrames(pcs[:n])
frames := createFrames(runtimeFrames, skipFrames)
frames := createFrames(runtimeFrames)

stacktrace := Stacktrace{
Frames: frames,
Expand Down Expand Up @@ -67,7 +62,7 @@ func ExtractStacktrace(err error) *Stacktrace {
}

runtimeFrames := extractFrames(pcs)
frames := createFrames(runtimeFrames, 0)
frames := createFrames(runtimeFrames)

stacktrace := Stacktrace{
Frames: frames,
Expand Down Expand Up @@ -275,25 +270,30 @@ func extractFrames(pcs []uintptr) []runtime.Frame {
for {
callerFrame, more := callersFrames.Next()

// Prepend the frame
frames = append([]runtime.Frame{callerFrame}, frames...)
frames = append(frames, callerFrame)

if !more {
break
}
}

// TODO don't append and reverse, put in the right place from the start.
// reverse
for i, j := 0, len(frames)-1; i < j; i, j = i+1, j-1 {
frames[i], frames[j] = frames[j], frames[i]
}

return frames
}

// createFrames creates Frame objects while filtering out frames that are not
// meant to be reported to Sentry, those are frames internal to the SDK or Go.
func createFrames(frames []runtime.Frame, skip int) []Frame {
if len(frames) == 0 || skip >= len(frames) {
func createFrames(frames []runtime.Frame) []Frame {
if len(frames) == 0 {
return nil
}

var result []Frame
result := make([]Frame, 0, len(frames))

for _, frame := range frames {
function := frame.Function
Expand All @@ -307,12 +307,6 @@ func createFrames(frames []runtime.Frame, skip int) []Frame {
}
}

if skip >= len(result) {
return []Frame{}
}

result = result[:len(result)-skip]

// Fix issues grouping errors with the new fully qualified function names
// introduced from Go 1.21
result = cleanupFunctionNamePrefix(result)
Expand Down Expand Up @@ -342,12 +336,12 @@ func shouldSkipFrame(module string) bool {
var goRoot = strings.ReplaceAll(build.Default.GOROOT, "\\", "/")

func setInAppFrame(frame *Frame) {
frame.InApp = true

if strings.HasPrefix(frame.AbsPath, goRoot) ||
strings.Contains(frame.Module, "vendor") ||
strings.Contains(frame.Module, "third_party") {
frame.InApp = false
} else {
frame.InApp = true
}
}

Expand Down
2 changes: 1 addition & 1 deletion stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestCreateFrames(t *testing.T) {
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := createFrames(tt.in, 0)
got := createFrames(tt.in)
if diff := cmp.Diff(tt.out, got); diff != "" {
t.Errorf("filterFrames() mismatch (-want +got):\n%s", diff)
}
Expand Down
Loading