diff --git a/main.go b/main.go index e6cea895..073fee7f 100644 --- a/main.go +++ b/main.go @@ -19,16 +19,10 @@ import ( "os" "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" ) func main() { - shared.ParseOptions() - if shared.PrintVersion { - shared.PrintTheVersion() - os.Exit(0) - } - err := shared.InitOptions() + err := tool.Init() if err != nil { log.Printf("failed to init options: %v", err) os.Exit(1) diff --git a/test/flags_test.go b/test/flags_test.go index 5611a79f..f50ffa1f 100644 --- a/test/flags_test.go +++ b/test/flags_test.go @@ -15,7 +15,6 @@ package test import ( - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" "testing" ) @@ -24,27 +23,27 @@ func TestFlags(t *testing.T) { UseApp(AppName) RunInstrument(t, "-debuglog", "-disablerules=") - ExpectInstrumentContains(t, shared.DebugLogFile, "fmt@") + ExpectInstrumentContains(t, DebugLogFile, "fmt@") RunInstrument(t, "-debuglog", "-disablerules=fmt,net/http") - ExpectInstrumentNotContains(t, shared.DebugLogFile, "fmt@") - ExpectInstrumentNotContains(t, shared.DebugLogFile, "net/http@") + ExpectInstrumentNotContains(t, DebugLogFile, "fmt@") + ExpectInstrumentNotContains(t, DebugLogFile, "net/http@") RunInstrument(t, "-debuglog", "-disablerules=*") - ExpectInstrumentNotContains(t, shared.DebugLogFile, "fmt@") - ExpectInstrumentNotContains(t, shared.DebugLogFile, "net/http@") + ExpectInstrumentNotContains(t, DebugLogFile, "fmt@") + ExpectInstrumentNotContains(t, DebugLogFile, "net/http@") RunInstrument(t, "-debuglog", "-disablerules=testrule") - ExpectInstrumentNotContains(t, shared.DebugLogFile, "fmt@") + ExpectInstrumentNotContains(t, DebugLogFile, "fmt@") RunInstrumentFallible(t, "-debuglog", "--", "-thisisnotvalid") - ExpectPreprocessContains(t, shared.DebugLogFile, "failed to") + ExpectPreprocessContains(t, DebugLogFile, "failed to") RunInstrument(t, "-version") ExpectStdoutContains(t, "version") RunInstrumentFallible(t, "-debuglog", "--", "notevenaflag") - ExpectPreprocessContains(t, shared.DebugLogFile, "failed to") + ExpectPreprocessContains(t, DebugLogFile, "failed to") RunInstrument(t, "-debuglog", "-verbose", "--", diff --git a/test/helloworld_test.go b/test/helloworld_test.go index ea661735..0699e49c 100644 --- a/test/helloworld_test.go +++ b/test/helloworld_test.go @@ -17,8 +17,6 @@ package test import ( "regexp" "testing" - - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" ) const HelloworldAppName = "helloworld" @@ -61,18 +59,18 @@ func TestRunHelloworld(t *testing.T) { func TestBuildHelloworldWithVendor1(t *testing.T) { UseApp(HelloworldAppName) - util.RunCmd("go", "mod", "vendor") + RunCmd([]string{"go", "mod", "vendor"}) RunInstrument(t, "-debuglog") } func TestBuildHelloworldWithVendor2(t *testing.T) { UseApp(HelloworldAppName) - util.RunCmd("go", "mod", "vendor") + RunCmd([]string{"go", "mod", "vendor"}) RunInstrument(t, "-debuglog", "--", "-mod=vendor") } func TestBuildHelloworldWithVendor3(t *testing.T) { UseApp(HelloworldAppName) - util.RunCmd("go", "mod", "vendor") + RunCmd([]string{"go", "mod", "vendor"}) RunInstrument(t, "-debuglog", "--", "-mod", "vendor") } diff --git a/test/infra.go b/test/infra.go index 10bab782..dbf27c25 100644 --- a/test/infra.go +++ b/test/infra.go @@ -25,12 +25,15 @@ import ( "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/verifier" "github.com/alibaba/opentelemetry-go-auto-instrumentation/test/version" - - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" ) -const execName = "otelbuild" +const ( + execName = "otelbuild" + TempBuildDir = ".otel-build" + TPreprocess = "preprocess" + TInstrument = "instrument" + DebugLogFile = "debug.log" +) func RunCmd(args []string) *exec.Cmd { path := args[0] @@ -48,22 +51,23 @@ func RunCmd(args []string) *exec.Cmd { return cmd } -func ReadInstrumentLog(t *testing.T, fileName string) string { - path := filepath.Join(shared.TempBuildDir, shared.TInstrument, fileName) - content, err := util.ReadFile(path) +func ReadFile(t *testing.T, fileName string) string { + path := filepath.Join(TempBuildDir, fileName) + content, err := os.ReadFile(path) if err != nil { t.Fatal(err) } - return content + return string(content) +} + +func ReadInstrumentLog(t *testing.T, fileName string) string { + path := filepath.Join(TempBuildDir, TInstrument, fileName) + return ReadFile(t, path) } func ReadPreprocessLog(t *testing.T, fileName string) string { - path := filepath.Join(shared.TempBuildDir, shared.TPreprocess, fileName) - content, err := util.ReadFile(path) - if err != nil { - t.Fatal(err) - } - return content + path := filepath.Join(TempBuildDir, TPreprocess, fileName) + return ReadFile(t, path) } func readStdoutLog(t *testing.T) string { @@ -75,7 +79,6 @@ func readStderrLog(t *testing.T) string { } func RunGoBuild(t *testing.T, args ...string) { - util.Assert(pwd != "", "pwd is empty") cmd := RunCmd(append([]string{"go", "build"}, args...)) err := cmd.Run() if err != nil { @@ -84,7 +87,6 @@ func RunGoBuild(t *testing.T, args ...string) { } func RunInstrumentFallible(t *testing.T, args ...string) { - util.Assert(pwd != "", "pwd is empty") path := filepath.Join(filepath.Dir(pwd), execName) cmd := RunCmd(append([]string{path}, args...)) err := cmd.Run() @@ -94,14 +96,13 @@ func RunInstrumentFallible(t *testing.T, args ...string) { } func RunInstrument(t *testing.T, args ...string) { - util.Assert(pwd != "", "pwd is empty") path := filepath.Join(filepath.Dir(pwd), execName) cmd := RunCmd(append([]string{path}, args...)) err := cmd.Run() if err != nil { stderr := readStderrLog(t) - log1 := ReadPreprocessLog(t, shared.DebugLogFile) - log2 := ReadInstrumentLog(t, shared.DebugLogFile) + log1 := ReadPreprocessLog(t, DebugLogFile) + log2 := ReadInstrumentLog(t, DebugLogFile) text := fmt.Sprintf("failed to run instrument: %v\n", err) text += fmt.Sprintf("stderr: %v\n", stderr) text += fmt.Sprintf("preprocess: %v\n", log1) @@ -188,25 +189,25 @@ func ExpectStderrContains(t *testing.T, expect string) { } func ExpectInstrumentContains(t *testing.T, log string, rule string) { - path := filepath.Join(shared.TempBuildDir, shared.TInstrument, log) + path := filepath.Join(TempBuildDir, TInstrument, log) content := readLog(t, path) ExpectContains(t, content, rule) } func ExpectInstrumentNotContains(t *testing.T, log string, rule string) { - path := filepath.Join(shared.TempBuildDir, shared.TInstrument, log) + path := filepath.Join(TempBuildDir, TInstrument, log) content := readLog(t, path) ExpectNotContains(t, content, rule) } func ExpectPreprocessContains(t *testing.T, log string, rule string) { - path := filepath.Join(shared.TempBuildDir, shared.TPreprocess, log) + path := filepath.Join(TempBuildDir, TPreprocess, log) content := readLog(t, path) ExpectContains(t, content, rule) } func ExpectPreprocessNotContains(t *testing.T, log string, rule string) { - path := filepath.Join(shared.TempBuildDir, shared.TPreprocess, log) + path := filepath.Join(TempBuildDir, TPreprocess, log) content := readLog(t, path) ExpectNotContains(t, content, rule) } diff --git a/tool/driver.go b/tool/driver.go index 4085e7d3..6a3de030 100644 --- a/tool/driver.go +++ b/tool/driver.go @@ -20,10 +20,10 @@ import ( "os" "path/filepath" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/instrument" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/preprocess" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/instrument" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/preprocess" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" ) func initLogs(names ...string) error { @@ -99,6 +99,17 @@ func initEnv() (err error) { return nil } +func Init() error { + shared.ParseOptions() + + if shared.PrintVersion { + shared.PrintTheVersion() + os.Exit(0) + } + + return shared.InitOptions() +} + func Run() (err error) { // Where our story begins err = initEnv() diff --git a/tool/instrument/inst_file.go b/tool/internal/instrument/inst_file.go similarity index 97% rename from tool/instrument/inst_file.go rename to tool/internal/instrument/inst_file.go index e7066fd7..d0f92c58 100644 --- a/tool/instrument/inst_file.go +++ b/tool/internal/instrument/inst_file.go @@ -20,9 +20,9 @@ import ( "path/filepath" "strings" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" ) func (rp *RuleProcessor) applyFileRules(bundle *resource.RuleBundle) (err error) { diff --git a/tool/instrument/inst_func.go b/tool/internal/instrument/inst_func.go similarity index 99% rename from tool/instrument/inst_func.go rename to tool/internal/instrument/inst_func.go index 5645fdbf..94d8c69a 100644 --- a/tool/instrument/inst_func.go +++ b/tool/internal/instrument/inst_func.go @@ -23,9 +23,9 @@ import ( "strings" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/dave/dst" ) diff --git a/tool/instrument/inst_struct.go b/tool/internal/instrument/inst_struct.go similarity index 98% rename from tool/instrument/inst_struct.go rename to tool/internal/instrument/inst_struct.go index c826a191..e1015433 100644 --- a/tool/instrument/inst_struct.go +++ b/tool/internal/instrument/inst_struct.go @@ -18,8 +18,8 @@ import ( "fmt" "log" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" ) func (rp *RuleProcessor) applyStructRules(bundle *resource.RuleBundle) error { diff --git a/tool/instrument/instrument.go b/tool/internal/instrument/instrument.go similarity index 99% rename from tool/instrument/instrument.go rename to tool/internal/instrument/instrument.go index c09ad1b6..87846a61 100644 --- a/tool/instrument/instrument.go +++ b/tool/internal/instrument/instrument.go @@ -23,9 +23,9 @@ import ( "time" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/dave/dst" ) diff --git a/tool/instrument/optimize.go b/tool/internal/instrument/optimize.go similarity index 99% rename from tool/instrument/optimize.go rename to tool/internal/instrument/optimize.go index 46d6e30b..67b34ae4 100644 --- a/tool/instrument/optimize.go +++ b/tool/internal/instrument/optimize.go @@ -20,8 +20,8 @@ import ( "strings" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/dave/dst" ) diff --git a/tool/instrument/template.go b/tool/internal/instrument/template.go similarity index 100% rename from tool/instrument/template.go rename to tool/internal/instrument/template.go diff --git a/tool/instrument/trampoline.go b/tool/internal/instrument/trampoline.go similarity index 99% rename from tool/instrument/trampoline.go rename to tool/internal/instrument/trampoline.go index 3a1ee768..a8944b37 100644 --- a/tool/instrument/trampoline.go +++ b/tool/internal/instrument/trampoline.go @@ -22,9 +22,9 @@ import ( "strconv" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/dave/dst" ) diff --git a/tool/preprocess/dependency.go b/tool/internal/preprocess/dependency.go similarity index 99% rename from tool/preprocess/dependency.go rename to tool/internal/preprocess/dependency.go index b2f616e6..eb23f03f 100644 --- a/tool/preprocess/dependency.go +++ b/tool/internal/preprocess/dependency.go @@ -26,9 +26,9 @@ import ( "strings" "syscall" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "golang.org/x/mod/modfile" ) diff --git a/tool/preprocess/match.go b/tool/internal/preprocess/match.go similarity index 99% rename from tool/preprocess/match.go rename to tool/internal/preprocess/match.go index 3977d95c..811b7022 100644 --- a/tool/preprocess/match.go +++ b/tool/internal/preprocess/match.go @@ -21,9 +21,9 @@ import ( "github.com/dave/dst" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" ) func findAvailableRules() []api.InstRule { diff --git a/tool/preprocess/preprocess.go b/tool/internal/preprocess/preprocess.go similarity index 99% rename from tool/preprocess/preprocess.go rename to tool/internal/preprocess/preprocess.go index e0b4c82d..39642408 100644 --- a/tool/preprocess/preprocess.go +++ b/tool/internal/preprocess/preprocess.go @@ -23,12 +23,12 @@ import ( "runtime" "time" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/version" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/version" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/resource" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/resource" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" ) // ----------------------------------------------------------------------------- diff --git a/tool/resource/bundle.go b/tool/internal/resource/bundle.go similarity index 99% rename from tool/resource/bundle.go rename to tool/internal/resource/bundle.go index 89612c93..90995771 100644 --- a/tool/resource/bundle.go +++ b/tool/internal/resource/bundle.go @@ -26,8 +26,8 @@ import ( "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" ) const ( diff --git a/tool/resource/resource.go b/tool/internal/resource/resource.go similarity index 98% rename from tool/resource/resource.go rename to tool/internal/resource/resource.go index c79fbda3..715a660b 100644 --- a/tool/resource/resource.go +++ b/tool/internal/resource/resource.go @@ -20,10 +20,10 @@ import ( "path/filepath" "strings" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/shared" "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" ) diff --git a/tool/shared/ast.go b/tool/internal/shared/ast.go similarity index 99% rename from tool/shared/ast.go rename to tool/internal/shared/ast.go index 23dbbf32..26417839 100644 --- a/tool/shared/ast.go +++ b/tool/internal/shared/ast.go @@ -22,7 +22,7 @@ import ( "os" "path/filepath" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "github.com/dave/dst" "github.com/dave/dst/decorator" ) diff --git a/tool/shared/flags.go b/tool/internal/shared/flags.go similarity index 99% rename from tool/shared/flags.go rename to tool/internal/shared/flags.go index 27f0df79..0e55cd5b 100644 --- a/tool/shared/flags.go +++ b/tool/internal/shared/flags.go @@ -21,7 +21,7 @@ import ( "path/filepath" "strconv" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" ) const ( diff --git a/tool/shared/shared.go b/tool/internal/shared/shared.go similarity index 99% rename from tool/shared/shared.go rename to tool/internal/shared/shared.go index 7a7c83cb..01f7758a 100644 --- a/tool/shared/shared.go +++ b/tool/internal/shared/shared.go @@ -24,7 +24,7 @@ import ( "regexp" "strings" - "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util" + "github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/internal/util" "golang.org/x/mod/semver" ) diff --git a/tool/util/util.go b/tool/internal/util/util.go similarity index 100% rename from tool/util/util.go rename to tool/internal/util/util.go diff --git a/tool/version/version.go b/tool/internal/version/version.go similarity index 100% rename from tool/version/version.go rename to tool/internal/version/version.go