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(stacktrace): support package name parsing for Go 1.20+ #730

Merged
merged 2 commits into from
Oct 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ func callerFunctionName() string {
// It replicates https://golang.org/pkg/debug/gosym/#Sym.PackageName, avoiding a
// dependency on debug/gosym.
func packageName(name string) string {
// A prefix of "type." and "go." is a compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.") {
if isCompilerGeneratedSymbol(name) {
return ""
}

Expand Down
15 changes: 15 additions & 0 deletions stacktrace_below_go1.20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !go1.20

package sentry

import "strings"

func isCompilerGeneratedSymbol(name string) bool {
// In versions of Go below 1.20 a prefix of "type." and "go." is a
// compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.") {
return true
}
return false
}
32 changes: 32 additions & 0 deletions stacktrace_below_go1.20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !go1.20

package sentry

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestFilterCompilerGeneratedSymbols(t *testing.T) {
tests := []struct {
symbol string
expectedPackageName string
}{
{"type..eq.[9]debug/elf.intName", ""},
{"type..hash.debug/elf.ProgHeader", ""},
{"type..eq.runtime._panic", ""},
{"type..hash.struct { runtime.gList; runtime.n int32 }", ""},
{"go.(*struct { sync.Mutex; math/big.table [64]math/big", ""},
{"github.com/getsentry/sentry-go.Test.func2.1.1", "github.com/getsentry/sentry-go"},
}

for _, tt := range tests {
t.Run(tt.symbol, func(t *testing.T) {
packageName := packageName(tt.symbol)
if diff := cmp.Diff(tt.expectedPackageName, packageName); diff != "" {
t.Errorf("Package name mismatch (-want +got):\n%s", diff)
}
})
}
}
15 changes: 15 additions & 0 deletions stacktrace_go1.20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build go1.20

package sentry

import "strings"

func isCompilerGeneratedSymbol(name string) bool {
// In versions of Go 1.20 and above a prefix of "type:" and "go:" is a
// compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go:") || strings.HasPrefix(name, "type:") {
return true
}
return false
}
32 changes: 32 additions & 0 deletions stacktrace_go1.20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build go1.20

package sentry

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestFilterCompilerGeneratedSymbols(t *testing.T) {
tests := []struct {
symbol string
expectedPackageName string
}{
{"type:.eq.[9]debug/elf.intName", ""},
{"type:.hash.debug/elf.ProgHeader", ""},
{"type:.eq.runtime._panic", ""},
{"type:.hash.struct { runtime.gList; runtime.n int32 }", ""},
{"go:(*struct { sync.Mutex; math/big.table [64]math/big", ""},
{"go.uber.org/zap/buffer.(*Buffer).AppendString", "go.uber.org/zap/buffer"},
}

for _, tt := range tests {
t.Run(tt.symbol, func(t *testing.T) {
packageName := packageName(tt.symbol)
if diff := cmp.Diff(tt.expectedPackageName, packageName); diff != "" {
t.Errorf("Package name mismatch (-want +got):\n%s", diff)
}
})
}
}