Skip to content

Commit

Permalink
refine: standardized error message (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
y1yang0 authored Jan 9, 2025
1 parent 10697b5 commit 2cb669a
Show file tree
Hide file tree
Showing 27 changed files with 707 additions and 581 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ to engage with us.

<img src="docs/dingtalk.png" height="200">

## Adopters
# Adopters

These are only part of the companies using this project, for reference only. If you are using this project, please [add your company here](https://github.com/alibaba/opentelemetry-go-auto-instrumentation/issues/225) to tell us your scenario to make this project better.

Expand Down
10 changes: 5 additions & 5 deletions test/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package test
import (
"testing"

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

func TestBuildProject(t *testing.T) {
Expand Down Expand Up @@ -72,8 +72,8 @@ func TestBuildProject5(t *testing.T) {
RunGoBuild(t, "go", "build", "m1")
// both test_fmt.json and default.json rules should be available
// because we always append new -rule to the default.json by default
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpectPreprocessContains(t, shared.DebugLogFile, "database/sql")
ExpectPreprocessContains(t, util.DebugLogFile, "fmt")
ExpectPreprocessContains(t, util.DebugLogFile, "database/sql")
}

func TestBuildProject6(t *testing.T) {
Expand All @@ -83,6 +83,6 @@ func TestBuildProject6(t *testing.T) {
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/test_fmt.json", "-verbose")
RunGoBuild(t, "go", "build", "m1")
// only test_fmt.json should be available because -disabledefault is set
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpectPreprocessNotContains(t, shared.DebugLogFile, "database/sql")
ExpectPreprocessContains(t, util.DebugLogFile, "fmt")
ExpectPreprocessNotContains(t, util.DebugLogFile, "database/sql")
}
10 changes: 5 additions & 5 deletions test/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package test
import (
"testing"

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

const AppName = "flags"
Expand All @@ -26,13 +26,13 @@ func TestFlags(t *testing.T) {
UseApp(AppName)

RunGoBuildFallible(t, "go", "build", "-thisisnotvalid")
ExpectPreprocessContains(t, shared.DebugLogFile, "failed to")
ExpectPreprocessContains(t, util.DebugLogFile, "Fatal Error")

RunVersion(t)
ExpectStdoutContains(t, "version")

RunGoBuildFallible(t, "go", "build", "notevenaflag")
ExpectPreprocessContains(t, shared.DebugLogFile, "failed to")
ExpectPreprocessContains(t, util.DebugLogFile, "Fatal Error")

RunSet(t, "-verbose")
RunGoBuild(t, "go", "build", `-ldflags=-X main.Placeholder=replaced`)
Expand All @@ -50,10 +50,10 @@ func TestFlagEnvOverwrite(t *testing.T) {
RunSet(t, "-verbose=false")
RunGoBuildWithEnv(t, []string{"OTELTOOL_VERBOSE=true"},
"go", "build")
ExpectPreprocessContains(t, shared.DebugLogFile, "Available")
ExpectPreprocessContains(t, util.DebugLogFile, "Available")

RunSet(t, "-verbose=true")
RunGoBuildWithEnv(t, []string{"OTELTOOL_VERBOSE=false"},
"go", "build")
ExpectPreprocessNotContains(t, shared.DebugLogFile, "Available")
ExpectPreprocessNotContains(t, util.DebugLogFile, "Available")
}
17 changes: 8 additions & 9 deletions test/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/alibaba/opentelemetry-go-auto-instrumentation/test/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"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func runCmd(args []string) *exec.Cmd {
}

func ReadInstrumentLog(t *testing.T, fileName string) string {
path := filepath.Join(shared.TempBuildDir, util.PInstrument, fileName)
path := filepath.Join(util.TempBuildDir, util.PInstrument, fileName)
content, err := util.ReadFile(path)
if err != nil {
t.Fatal(err)
Expand All @@ -64,7 +63,7 @@ func ReadInstrumentLog(t *testing.T, fileName string) string {
}

func ReadPreprocessLog(t *testing.T, fileName string) string {
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, fileName)
path := filepath.Join(util.TempBuildDir, util.PPreprocess, fileName)
content, err := util.ReadFile(path)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -111,7 +110,7 @@ func RunGoBuild(t *testing.T, args ...string) {
t.Log(stdout)
t.Log("\n\n\n")
t.Log(stderr)
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
log1 := ReadPreprocessLog(t, util.DebugLogFile)
text := fmt.Sprintf("failed to run instrument: %v\n", err)
text += fmt.Sprintf("text: %v\n", log1)
t.Fatal(text)
Expand All @@ -130,7 +129,7 @@ func RunGoBuildWithEnv(t *testing.T, envs []string, args ...string) {
t.Log(stdout)
t.Log("\n\n\n")
t.Log(stderr)
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
log1 := ReadPreprocessLog(t, util.DebugLogFile)
text := fmt.Sprintf("failed to run instrument: %v\n", err)
text += fmt.Sprintf("text: %v\n", log1)
t.Fatal(text)
Expand Down Expand Up @@ -212,25 +211,25 @@ func ExpectStderrContains(t *testing.T, expect string) {
}

func ExpectInstrumentContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
path := filepath.Join(util.TempBuildDir, util.PInstrument, log)
content := readLog(t, path)
ExpectContains(t, content, rule)
}

func ExpectInstrumentNotContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
path := filepath.Join(util.TempBuildDir, util.PInstrument, log)
content := readLog(t, path)
ExpectNotContains(t, content, rule)
}

func ExpectPreprocessContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
path := filepath.Join(util.TempBuildDir, util.PPreprocess, log)
content := readLog(t, path)
ExpectContains(t, content, rule)
}

func ExpectPreprocessNotContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
path := filepath.Join(util.TempBuildDir, util.PPreprocess, log)
content := readLog(t, path)
ExpectNotContains(t, content, rule)
}
Expand Down
2 changes: 0 additions & 2 deletions test/kitex/v0.5.1/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module kitex/v0.5.1

go 1.22.0

toolchain go1.22.7

replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../../opentelemetry-go-auto-instrumentation

replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../../opentelemetry-go-auto-instrumentation/test/verifier
Expand Down
4 changes: 2 additions & 2 deletions test/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"regexp"
"testing"

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

const WorldAppName = "world"
Expand All @@ -29,7 +29,7 @@ func TestCompileTheWorld(t *testing.T) {

RunGoBuild(t, "go", "build")
RunApp(t, WorldAppName)
text := ReadPreprocessLog(t, shared.DebugLogFile)
text := ReadPreprocessLog(t, util.DebugLogFile)

regex := `\"ImportPath\":\"([^"]+)\"`
r := regexp.MustCompile(regex)
Expand Down
70 changes: 45 additions & 25 deletions tool/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/config"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/errc"
"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/shared"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util"
)

Expand All @@ -48,17 +49,18 @@ Command:
`

func printUsage() {
usage = strings.ReplaceAll(usage, "{}", util.GetToolName())
name, _ := util.GetToolName()
usage = strings.ReplaceAll(usage, "{}", name)
fmt.Print(usage)
}

func initLog() error {
name := util.PPreprocess
path := shared.GetTempBuildDirWith(name)
logPath := filepath.Join(path, shared.DebugLogFile)
path := util.GetTempBuildDirWith(name)
logPath := filepath.Join(path, util.DebugLogFile)
_, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("failed to create log file: %w", err)
return errc.New(errc.ErrCreateFile, err.Error())
}
return nil
}
Expand All @@ -71,34 +73,32 @@ func initTempDir() error {
}

// Make temp build directory if not exists
if exist, _ := util.PathExists(shared.TempBuildDir); !exist {
err := os.MkdirAll(shared.TempBuildDir, 0777)
if util.PathNotExists(util.TempBuildDir) {
err := os.MkdirAll(util.TempBuildDir, 0777)
if err != nil {
return fmt.Errorf("failed to make working directory: %w", err)
return errc.New(errc.ErrMkdirAll, err.Error())
}
}
// Make sub-directory of temp build directory for each phase. Specifaclly,
// we always recreate the preprocess and instrument directories, but only
// create the configure directory if it does not exist. This is because
// the configure directory can be used across multiple runs.
exist, _ := util.PathExists(shared.GetTempBuildDirWith(util.PConfigure))
if !exist {
err := os.MkdirAll(shared.GetTempBuildDirWith(util.PConfigure), 0777)
if util.PathNotExists(util.GetTempBuildDirWith(util.PConfigure)) {
err := os.MkdirAll(util.GetTempBuildDirWith(util.PConfigure), 0777)
if err != nil {
return fmt.Errorf("failed to make log directory: %w", err)
return errc.New(errc.ErrMkdirAll, err.Error())
}
}
for _, subdir := range []string{util.PPreprocess, util.PInstrument} {
exist, _ = util.PathExists(shared.GetTempBuildDirWith(subdir))
if exist {
err := os.RemoveAll(shared.GetTempBuildDirWith(subdir))
if util.PathExists(util.GetTempBuildDirWith(subdir)) {
err := os.RemoveAll(util.GetTempBuildDirWith(subdir))
if err != nil {
return fmt.Errorf("failed to remove directory: %w", err)
return errc.New(errc.ErrRemoveAll, err.Error())
}
}
err := os.MkdirAll(shared.GetTempBuildDirWith(subdir), 0777)
err := os.MkdirAll(util.GetTempBuildDirWith(subdir), 0777)
if err != nil {
return fmt.Errorf("failed to make log directory: %w", err)
return errc.New(errc.ErrMkdirAll, err.Error())
}
}

Expand Down Expand Up @@ -126,27 +126,42 @@ func initEnv() error {
// Create temp build directory
err := initTempDir()
if err != nil {
return fmt.Errorf("failed to init temp dir: %w", err)
return err
}

// Create log files under temp build directory
if util.InPreprocess() {
err := initLog()
if err != nil {
return fmt.Errorf("failed to init logs: %w", err)
return err
}
}

// Prepare shared configuration
if util.InPreprocess() || util.InInstrument() {
err = config.InitConfig()
if err != nil {
return fmt.Errorf("failed to init config: %w", err)
return err
}
}
return nil
}

func fatal(err error) {
message := "===== Environments =====\n"
message += fmt.Sprintf("%-11s: %s\n", "Command", strings.Join(os.Args, " "))
message += fmt.Sprintf("%-11s: %s\n", "ErrorLog", util.GetLoggerPath())
message += fmt.Sprintf("%-11s: %s\n", "WorkDir", os.Getenv("PWD"))
path, _ := util.GetGoModPath()
message += fmt.Sprintf("%-11s: %s\n", "GoMod", path)
message += fmt.Sprintf("%-11s: %s, %s, %s\n", "Toolchain",
runtime.GOOS+"/"+runtime.GOARCH,
runtime.Version(), config.ToolVersion)
message += "===== Fatal Error ======\n"
message += err.Error()
util.LogFatal("\033[31m%s\033[0m", message) // log in red color
}

func main() {
if len(os.Args) < 2 {
printUsage()
Expand All @@ -155,8 +170,7 @@ func main() {

err := initEnv()
if err != nil {
util.LogFatal("failed to init env: %v", err)
os.Exit(1)
fatal(err)
}

subcmd := os.Args[1]
Expand All @@ -173,7 +187,13 @@ func main() {
printUsage()
}
if err != nil {
util.LogFatal("failed to run command %s: %v", subcmd, err)
os.Exit(1)
if subcmd != SubcommandRemix {
fatal(err)
} else {
// If error occurs in remix phase, we dont want to decoret the error
// message with the environments, just print the error message, the
// caller(preprocess) phase will decorate instead.
util.LogFatal(err.Error())
}
}
}
Loading

0 comments on commit 2cb669a

Please sign in to comment.