diff --git a/xray/plugin.go b/xray/plugin.go index 9c4a366..2382cac 100644 --- a/xray/plugin.go +++ b/xray/plugin.go @@ -1,7 +1,9 @@ package xray import ( + "reflect" "runtime/debug" + "strings" "sync" "github.com/shogo82148/aws-xray-yasdk-go/xray/schema" @@ -38,6 +40,8 @@ func getPlugins() []Plugin { return plugins } +var _ Plugin = (*xrayPlugin)(nil) + // xrayPlugin injects information about X-Ray YA-SDK. type xrayPlugin struct { sdkVersion string @@ -69,10 +73,18 @@ func getVersion() string { if !ok { return Version } + + // get the package path of the sdk + typ := reflect.TypeOf(xrayPlugin{}) + pkg := typ.PkgPath() + + version := Version + depth := 0 for _, dep := range info.Deps { - if dep.Path == "github.com/shogo82148/aws-xray-yasdk-go" { - return dep.Version + // search the most specific module + if strings.HasPrefix(pkg, dep.Path) && len(dep.Path) > depth { + version = dep.Version } } - return Version + return version } diff --git a/xray/plugin_test.go b/xray/plugin_test.go new file mode 100644 index 0000000..e844b58 --- /dev/null +++ b/xray/plugin_test.go @@ -0,0 +1,25 @@ +package xray + +import ( + "sync" + "testing" +) + +func TestAddPlugin(t *testing.T) { + before := len(getPlugins()) + + // test of races + const n = 10 + var wg sync.WaitGroup + wg.Add(n) + for i := 0; i < n; i++ { + go AddPlugin(&xrayPlugin{}) + go getPlugins() + } + + after := len(getPlugins()) + + if after-before != n { + t.Errorf("unexpected plugin count: want %d, got %d", n, after-before) + } +}