Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine: make tool internal #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions test/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package test

import (
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared"
"testing"
)

Expand All @@ -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",
"--",
Expand Down
8 changes: 3 additions & 5 deletions test/helloworld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package test
import (
"regexp"
"testing"

"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util"
)

const HelloworldAppName = "helloworld"
Expand Down Expand Up @@ -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")
}
47 changes: 24 additions & 23 deletions test/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 15 additions & 4 deletions tool/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

// -----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tool/resource/bundle.go → tool/internal/resource/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion tool/shared/ast.go → tool/internal/shared/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Loading
Loading