fix(sentry)_: remove stacktrace trimming (#6162)

* fix_: no trim stacktrace

* fix_: sentry test
This commit is contained in:
Igor Sirotin 2024-12-04 14:19:16 +00:00 committed by GitHub
parent bed099c1e8
commit 233f2f9a2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1 additions and 207 deletions

View File

@ -57,11 +57,5 @@ func defaultConfig() *sentry.ClientOptions {
func beforeSend(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
event.Modules = nil // Clear modules as we know all dependencies by commit hash
event.ServerName = "" // Clear server name as it might be sensitive
// Cleanup the stacktrace from last Recover/LogOnPanic frames
for _, exception := range event.Exception {
trimStacktrace(exception.Stacktrace)
}
return event
}

View File

@ -32,12 +32,8 @@ func TestBeforeSend(t *testing.T) {
// Call the beforeSend function
result := beforeSend(event, nil)
// Verify that the stacktrace frames are correctly trimmed
require.NotNil(t, result)
require.Len(t, result.Exception[0].Stacktrace.Frames, 1)
require.Equal(t, "OtherFunction", result.Exception[0].Stacktrace.Frames[0].Function)
// Verify that Modules and ServerName are empty
require.NotNil(t, result)
require.Empty(t, result.Modules)
require.Empty(t, result.ServerName)
}

View File

@ -1,65 +0,0 @@
package sentry
import (
"slices"
"github.com/getsentry/sentry-go"
)
var stacktraceFilters = []struct {
Module string
Functions []string
}{
{
Module: "github.com/status-im/status-go/internal/sentry",
Functions: []string{"Recover", "RecoverError"},
},
{
Module: "github.com/status-im/status-go/common",
Functions: []string{"LogOnPanic"},
},
{
Module: "github.com/status-im/status-go/mobile/callog",
Functions: []string{"Recover"},
},
}
func trimStacktrace(stacktrace *sentry.Stacktrace) {
if stacktrace == nil {
return
}
if len(stacktrace.Frames) <= 1 {
return
}
trim := 0
// Trim max 2 frames from the end
for i := len(stacktrace.Frames) - 1; i >= 0; i-- {
if !matchFilter(stacktrace.Frames[i]) {
// break as soon as we find a frame that doesn't match
break
}
trim++
if trim == 2 {
break
}
}
stacktrace.Frames = stacktrace.Frames[:len(stacktrace.Frames)-trim]
}
func matchFilter(frame sentry.Frame) bool {
for _, filter := range stacktraceFilters {
if frame.Module != filter.Module {
continue
}
if !slices.Contains(filter.Functions, frame.Function) {
continue
}
return true
}
return false
}

View File

@ -1,131 +0,0 @@
package sentry
import (
"testing"
"github.com/getsentry/sentry-go"
"github.com/stretchr/testify/require"
)
func TestTrimStacktrace(t *testing.T) {
tests := []struct {
name string
stacktrace *sentry.Stacktrace
expected []sentry.Frame
}{
{
name: "nil stacktrace",
stacktrace: nil,
expected: nil,
},
{
name: "single frame",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
},
},
{
name: "multiple frames with matching filters",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/mobile/callog", Function: "Recover"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "RecoverError"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
},
},
{
name: "matching module but not function",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Init"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Init"},
},
},
{
name: "multiple frames without matching filters",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc2"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc3"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc2"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc3"},
},
},
{
name: "matching filters only at the end",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
{Module: "github.com/status-im/status-go/common", Function: "LogOnPanic"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc"},
},
},
{
name: "remove at most 2 frames",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc1"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc2"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc3"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
{Module: "github.com/status-im/status-go/mobile/callog", Function: "Recover"},
{Module: "github.com/status-im/status-go/common", Function: "LogOnPanic"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc1"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc2"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc3"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "Recover"},
},
},
{
name: "break if non-matching frame found",
stacktrace: &sentry.Stacktrace{
Frames: []sentry.Frame{
{Module: "github.com/status-im/status-go/mobile/callog", Function: "Recover"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "RecoverError"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc1"},
},
},
expected: []sentry.Frame{
{Module: "github.com/status-im/status-go/mobile/callog", Function: "Recover"},
{Module: "github.com/status-im/status-go/internal/sentry", Function: "RecoverError"},
{Module: "github.com/status-im/status-go/other", Function: "OtherFunc1"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trimStacktrace(tt.stacktrace)
if tt.stacktrace != nil {
require.Equal(t, tt.expected, tt.stacktrace.Frames)
}
})
}
}