Skip to content

Commit

Permalink
get the package path of the sdk by the reflect package
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Apr 26, 2022
1 parent 04f0efa commit a13045e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
18 changes: 15 additions & 3 deletions xray/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xray

import (
"reflect"
"runtime/debug"
"strings"
"sync"

"github.com/shogo82148/aws-xray-yasdk-go/xray/schema"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions xray/plugin_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit a13045e

Please sign in to comment.