Skip to content

Commit

Permalink
Remove potential Go module versions from shortened names (#571)
Browse files Browse the repository at this point in the history
* Expand shortened name if package name appears to be a module version

* Correct version regexp, use more generic names in tests, remove an empty line

* 80 character columns

* Remove first matching Go module version from path

* Test and fix multi-version case

Co-authored-by: Maggie Nolan <nolanmar@google.com>
  • Loading branch information
zikaeroh and nolanmar511 authored Oct 16, 2020
1 parent 67992a1 commit 8ef5528
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:], "")
Expand Down
32 changes: 32 additions & 0 deletions internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8ef5528

Please sign in to comment.