Skip to content

Commit

Permalink
Merge pull request #10 from budougumi0617/use_struct_name
Browse files Browse the repository at this point in the history
✨ use struct name in segment name
  • Loading branch information
budougumi0617 authored Jan 12, 2021
2 parents 7dacf04 + 7d968e3 commit 00ad0f3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"net/http"
)

type S struct{}
type FooBar struct{}

func (s *S) SampleMethod(ctx context.Context) {
func (f *FooBar) SampleMethod(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}
Expand Down Expand Up @@ -68,10 +68,10 @@ import (
"github.com/newrelic/go-agent/v3/newrelic"
)

type S struct{}
type FooBar struct{}

func (s *S) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("sample_method").End()
func (f *FooBar) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("foo_bar_sample_method").End()
fmt.Println("Hello, playground")
fmt.Println("end function")
}
Expand Down
13 changes: 13 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ func Process(filename string, src []byte) ([]byte, error) {
return false
}
if fd.Body != nil {
var prefix string
if fd.Recv != nil && len(fd.Recv.List) > 0 {
if rn, ok := fd.Recv.List[0].Type.(*ast.StarExpr); ok {
if idt, ok := rn.X.(*ast.Ident); ok {
prefix = genSegName(idt.Name)
}
} else if idt, ok := fd.Recv.List[0].Type.(*ast.Ident); ok {
prefix = genSegName(idt.Name)
}
}
sn := genSegName(fd.Name.Name)
if len(prefix) != 0 {
sn = prefix + "_" + sn
}
vn, t := parseParams(fd.Type)
var ds ast.Stmt
switch t {
Expand Down
27 changes: 21 additions & 6 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ import (
"net/http"
)
type S struct{}
type Foo struct{}
func (s *S) SampleMethod(ctx context.Context) {
func (f *Foo) SampleMethod(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}
type BarBar struct{}
func (b BarBar) BazMethod(ctx context.Context) {
fmt.Println("Hello, playground")
fmt.Println("end function")
}
Expand Down Expand Up @@ -54,10 +61,18 @@ import (
"github.com/newrelic/go-agent/v3/newrelic"
)
type S struct{}
type Foo struct{}
func (s *S) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("sample_method").End()
func (f *Foo) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("foo_sample_method").End()
fmt.Println("Hello, playground")
fmt.Println("end function")
}
type BarBar struct{}
func (b BarBar) BazMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("bar_bar_baz_method").End()
fmt.Println("Hello, playground")
fmt.Println("end function")
}
Expand Down Expand Up @@ -117,7 +132,7 @@ import (
type S struct{}
func (s *S) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("sample_method").End()
defer newrelic.FromContext(ctx).StartSegment("s_sample_method").End()
fmt.Println("Hello, playground")
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/want/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type S struct{}

func (s *S) SampleMethod(ctx context.Context) {
defer newrelic.FromContext(ctx).StartSegment("sample_method").End()
defer newrelic.FromContext(ctx).StartSegment("s_sample_method").End()
fmt.Println("Hello, playground")
fmt.Println("end function")
}
Expand Down

0 comments on commit 00ad0f3

Please sign in to comment.