diff --git a/internal/graph/graph.go b/internal/graph/graph.go index d2397a93..9cc74bbb 100644 --- a/internal/graph/graph.go +++ b/internal/graph/graph.go @@ -34,6 +34,8 @@ var ( // Removes package name and method arugments for Go function names. // See tests for examples. goRegExp = regexp.MustCompile(`^(?:[\w\-\.]+\/)+(.+)`) + // Removes potential module versions in a package path. + goVerRegExp = regexp.MustCompile(`^(.*?)/v(?:[2-9]|[1-9][0-9]+)([./].*)$`) // Strips C++ namespace prefix from a C++ function / method name. // NOTE: Make sure to keep the template parameters in the name. Normally, // template parameters are stripped from the C++ names but when @@ -440,6 +442,7 @@ func newTree(prof *profile.Profile, o *Options) (g *Graph) { // ShortenFunctionName returns a shortened version of a function's name. func ShortenFunctionName(f string) string { f = cppAnonymousPrefixRegExp.ReplaceAllString(f, "") + f = goVerRegExp.ReplaceAllString(f, `${1}${2}`) for _, re := range []*regexp.Regexp{goRegExp, javaRegExp, cppRegExp} { if matches := re.FindStringSubmatch(f); len(matches) >= 2 { return strings.Join(matches[1:], "") diff --git a/internal/graph/graph_test.go b/internal/graph/graph_test.go index d4bd3c99..bdcb984e 100644 --- a/internal/graph/graph_test.go +++ b/internal/graph/graph_test.go @@ -451,6 +451,38 @@ func TestShortenFunctionName(t *testing.T) { "github.com/blah/blah/vendor/gopkg.in/redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm", "redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm", }, + { + "github.com/foo/bar/v4.(*Foo).Bar", + "bar.(*Foo).Bar", + }, + { + "github.com/foo/bar/v4/baz.Foo.Bar", + "baz.Foo.Bar", + }, + { + "github.com/foo/bar/v123.(*Foo).Bar", + "bar.(*Foo).Bar", + }, + { + "github.com/foobar/v0.(*Foo).Bar", + "v0.(*Foo).Bar", + }, + { + "github.com/foobar/v1.(*Foo).Bar", + "v1.(*Foo).Bar", + }, + { + "example.org/v2xyz.Foo", + "v2xyz.Foo", + }, + { + "github.com/foo/bar/v4/v4.(*Foo).Bar", + "v4.(*Foo).Bar", + }, + { + "github.com/foo/bar/v4/foo/bar/v4.(*Foo).Bar", + "v4.(*Foo).Bar", + }, { "java.util.concurrent.ThreadPoolExecutor$Worker.run", "ThreadPoolExecutor$Worker.run",