Skip to content

Commit

Permalink
add test cases and remove unnecessary error
Browse files Browse the repository at this point in the history
  • Loading branch information
lujiajing1126 committed Nov 15, 2023
1 parent c517e86 commit b0bbdc0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
11 changes: 5 additions & 6 deletions tools/go-agent/instrument/plugins/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ import (
//go:embed templates
var templatesFS embed.FS

var (
errNoVersionFound = errors.New("no version found in the path of the source file")
)

type Instrument struct {
realInst instrument.Instrument
methodFilters []*instrument.Point
Expand Down Expand Up @@ -110,7 +106,7 @@ func (i *Instrument) CouldHandle(opts *api.CompileOptions) bool {
}
// check the version of the framework could handler
version, err := i.tryToFindThePluginVersion(opts, ins)
if err != nil && !errors.Is(err, errNoVersionFound) {
if err != nil {
logrus.Warnf("ignore the plugin %s, because %s", ins.Name(), err)
continue
}
Expand Down Expand Up @@ -566,7 +562,10 @@ func (i *Instrument) tryToFindThePluginVersion(opts *api.CompileOptions, ins ins
// arg example: github.com/!shopify/[email protected]/acl.go
_, afterPkg, found := strings.Cut(arg, escapedBasePkg)
if !found {
return "", errors.Wrapf(errNoVersionFound, "package %s, go file path: %s", basePkg, arg)
// This could happen if the module is replaced by a local one
// For example, in the E2E
logrus.Warnf("could not found the go version of the package %s, go file path: %s", basePkg, arg)
return "", nil
}

if !strings.HasPrefix(afterPkg, "@") {
Expand Down
27 changes: 26 additions & 1 deletion tools/go-agent/instrument/plugins/instrument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"embed"
"testing"

"github.com/stretchr/testify/require"

"github.com/apache/skywalking-go/plugins/core/instrument"
"github.com/apache/skywalking-go/tools/go-agent/instrument/api"
)
Expand Down Expand Up @@ -52,12 +54,35 @@ func TestInstrument_tryToFindThePluginVersion(t *testing.T) {
NewTestInstrument("github.com/Shopify/sarama"),
"1.34.1",
},
{
"plugin for go stdlib",
&api.CompileOptions{
AllArgs: []string{
"/opt/homebrew/Cellar/go/1.21.4/libexec/src/runtime/metrics/sample.go",
},
},
NewTestInstrument("runtime/metrics"),
"",
},
{
"plugin for replaced module",
&api.CompileOptions{
AllArgs: []string{
"/home/user/skywalking-go/toolkit/trace/api.go",
},
},
NewTestInstrument("github.com/apache/skywalking-go/toolkit"),
"",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &Instrument{}
got, _ := i.tryToFindThePluginVersion(tt.opts, tt.ins)
got, err := i.tryToFindThePluginVersion(tt.opts, tt.ins)
if err != nil {
require.NoError(t, err)
}
if got != tt.want {
t.Errorf("tryToFindThePluginVersion() got = %v, want %v", got, tt.want)
}
Expand Down

0 comments on commit b0bbdc0

Please sign in to comment.