Skip to content

Commit 67a803a

Browse files
authored
feat: support otel subcommands (#216)
* feat: support otel subcommands * update Makefile * fix es tests * reflect in README.md * rename back * fix fiberv2 * add -disabledefault
1 parent 0d37fdd commit 67a803a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+728
-650
lines changed

Diff for: Makefile

+7-9
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ MOD_NAME := github.com/alibaba/opentelemetry-go-auto-instrumentation
4141
TOOL_REL_NAME := otel
4242

4343
VERSION := $(MAIN_VERSION)_$(COMMIT_ID)
44-
45-
XVERSION := -X=$(MOD_NAME)/tool/shared.TheVersion=$(VERSION)
46-
XNAME := -X=$(MOD_NAME)/tool/shared.TheName=$(TOOL_REL_NAME)
44+
XVERSION := -X=$(MOD_NAME)/tool/config.ToolVersion=$(VERSION)
4745
STRIP_DEBUG := -s -w
48-
LDFLAGS := $(XVERSION) $(XNAME) $(STRIP_DEBUG)
49-
BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/
46+
LDFLAGS := $(XVERSION) $(STRIP_DEBUG)
47+
BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/cmd
5048

5149
OUTPUT_BASE = $(TOOL_REL_NAME)
5250
OUTPUT_DARWIN_AMD64 = $(OUTPUT_BASE)-darwin-amd64
@@ -55,15 +53,15 @@ OUTPUT_WINDOWS_AMD64 = $(OUTPUT_BASE)-windows-amd64.exe
5553
OUTPUT_DARWIN_ARM64 = $(OUTPUT_BASE)-darwin-arm64
5654
OUTPUT_LINUX_ARM64 = $(OUTPUT_BASE)-linux-arm64
5755

58-
.PHONY: all test clean
59-
60-
all: clean darwin_amd64 linux_amd64 windows_amd64 darwin_arm64 linux_arm64
61-
6256
.PHONY: build
6357
build:
6458
go mod tidy
6559
$(call BUILD_CMD,$(CURRENT_OS),$(CURRENT_ARCH),$(OUTPUT_BASE))
6660

61+
.PHONY: all test clean
62+
63+
all: clean darwin_amd64 linux_amd64 windows_amd64 darwin_arm64 linux_arm64
64+
6765
darwin_amd64:
6866
go mod tidy
6967
$(call BUILD_CMD,darwin,amd64,$(OUTPUT_DARWIN_AMD64))

Diff for: README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,32 @@ page.
2626

2727
### Build From Source
2828

29-
Checkout the source code and build the tool by running the following command:
29+
Checkout source code and build the tool by running one of following commands:
3030

3131
```bash
32-
$ make install
32+
$ make # build only
33+
$ make install # build and install
3334
```
3435

3536
# Getting Started
3637

37-
Add `otel` prefix to `go build` to build your project:
38+
The configuration for the tool can be set by the following command:
3839

3940
```bash
40-
$ otel go build
41-
$ otel go build -o app cmd/app
42-
$ otel go build -gcflags="-m" cmd/app
41+
$ otel set -verbose # print verbose logs
42+
$ otel set -debug # enable debug mode
43+
$ otel set -debug -verbose -rule=custom.json # set multiple configs
44+
$ otel set -disabledefault -rule=custom.json # disable default rules, use custom rules only
45+
$ otel set -rule=custom.json # use default and custom rules
46+
$ otel set -rule=a.json,b.json # use default, a and b rules
4347
```
44-
The arguments for the tool itself should be placed before `go build`:
48+
49+
Once all set up, add `otel` prefix to `go build` to build your project:
4550

4651
```bash
47-
$ otel -help # print help doc
48-
$ otel -debug go build # enable debug mode
49-
$ otel -verbose go build # print verbose log
50-
$ otel -rule=custom.json go build # use custom rule
52+
$ otel go build
53+
$ otel go build -o app cmd/app
54+
$ otel go build -gcflags="-m" cmd/app
5155
```
5256

5357
You can also explore [**these examples**](./example/) to get hands-on experience.

Diff for: docs/anim-logo.svg

+2-5
Loading

Diff for: docs/how-to-write-tests-for-plugins.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestExecutingCommands(t *testing.T, env ...string) {
102102
redisC, redisPort := initRedisContainer()
103103
defer clearRedisContainer(redisC)
104104
UseApp("redis/v9.0.5")
105-
RunInstrument(t, "-debuglog", "go", "build", "test_executing_commands.go")
105+
RunGoBuild(t, "go", "build", "test_executing_commands.go")
106106
env = append(env, "REDIS_PORT="+redisPort.Port())
107107
RunApp(t, "test_executing_commands", env...)
108108
}
@@ -125,15 +125,15 @@ testName, moduleName, minVersion, maxVersion, minGoVersion, maxGoVersion string,
125125

126126
You should build the test case with the `opentelemetry-go-auto-instrumentation` to make your test case able to produce
127127
telemetry data. Firstly you should call `UseApp` method to change directory to the directory of your test cases, and
128-
then call `RunInstrument` to do hybrid compilation. Finally, use the `RunApp` to run the instrumented test-case binary to
128+
then call `RunGoBuild` to do hybrid compilation. Finally, use the `RunApp` to run the instrumented test-case binary to
129129
verify the telemetry data.
130130

131131
```go
132132
func TestExecutingUnsupportedCommands(t *testing.T, env ...string) {
133133
redisC, redisPort := initRedisContainer()
134134
defer clearRedisContainer(redisC)
135135
UseApp("redis/v9.0.5")
136-
RunInstrument(t, "-debuglog", "go", "build", "test_executing_unsupported_commands.go")
136+
RunGoBuild(t, "go", "build", "test_executing_unsupported_commands.go")
137137
env = append(env, "REDIS_PORT="+redisPort.Port())
138138
RunApp(t, "test_executing_unsupported_commands", env...)
139139
}

Diff for: test/build_test.go

+29-24
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,65 @@ import (
2323
func TestBuildProject(t *testing.T) {
2424
const AppName = "build"
2525
UseApp(AppName)
26-
RunInstrument(t, "go", "build", "-o", "default", "cmd/foo.go")
27-
RunInstrument(t, "go", "build", "cmd/foo.go")
28-
RunInstrument(t, "go", "build", "cmd/foo.go", "cmd/bar.go")
29-
RunInstrument(t, "go", "build", "cmd")
26+
RunGoBuild(t, "go", "build", "-o", "default", "cmd/foo.go")
27+
RunGoBuild(t, "go", "build", "cmd/foo.go")
28+
RunGoBuild(t, "go", "build", "cmd/foo.go", "cmd/bar.go")
29+
RunGoBuild(t, "go", "build", "cmd")
3030
}
3131

3232
func TestBuildProject2(t *testing.T) {
3333
const AppName = "build"
3434
UseApp(AppName)
3535

36-
RunInstrument(t, "go", "build", ".")
37-
RunInstrument(t, "go", "build", "")
36+
RunGoBuild(t, "go", "build", ".")
37+
RunGoBuild(t, "go", "build", "")
3838
}
3939

4040
func TestBuildProject3(t *testing.T) {
4141
const AppName = "build"
4242
UseApp(AppName)
4343

44-
RunInstrument(t, "go", "build", "m1")
45-
RunInstrumentFallible(t, "go", "build", "m2") // not used in go.work
44+
RunGoBuild(t, "go", "build", "m1")
45+
RunGoBuildFallible(t, "go", "build", "m2") // not used in go.work
4646
}
4747

4848
func TestBuildProject4(t *testing.T) {
4949
const AppName = "build"
5050
UseApp(AppName)
5151

52-
RunInstrument(t, "-rule=+../../pkg/data/default.json", "go", "build", "m1")
53-
RunInstrumentFallible(t, "-rule=../../pkg/data/default.json", "go", "build", "m1") // duplicated default rules
54-
RunInstrumentFallible(t, "-rule=../../pkg/data/default", "go", "build", "m1")
55-
RunInstrument(t, "-rule=../../pkg/data/test_error.json,../../pkg/data/test_fmt.json", "go", "build", "m1")
56-
RunInstrument(t, "-rule=../../pkg/data/test_error.json,+../../pkg/data/test_fmt.json", "go", "build", "m1")
57-
RunInstrument(t, "-rule=+../../pkg/data/default.json,+../../pkg/data/test_fmt.json", "go", "build", "m1")
52+
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/default.json")
53+
RunGoBuild(t, "go", "build", "m1")
54+
RunSet(t, "-disabledefault=false", "-rule=../../pkg/data/default.json")
55+
RunGoBuildFallible(t, "go", "build", "m1") // duplicated default rules
56+
RunSet(t, "-rule=../../pkg/data/default")
57+
RunGoBuildFallible(t, "go", "build", "m1")
58+
RunSet(t, "-rule=../../pkg/data/test_error.json,../../pkg/data/test_fmt.json")
59+
RunGoBuild(t, "go", "build", "m1")
60+
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/test_error.json,../../pkg/data/test_fmt.json")
61+
RunGoBuild(t, "go", "build", "m1")
62+
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/default.json,../../pkg/data/test_fmt.json")
63+
RunGoBuild(t, "go", "build", "m1")
5864
}
5965

6066
func TestBuildProject5(t *testing.T) {
6167
const AppName = "build"
6268
UseApp(AppName)
6369

64-
RunInstrument(t, "-debuglog", "-rule=../../pkg/data/test_fmt.json",
65-
"-verbose", "go", "build", "m1")
70+
RunSet(t, "-disabledefault=false", "-verbose", "-rule=../../pkg/data/test_fmt.json")
71+
RunGoBuild(t, "go", "build", "m1")
6672
// both test_fmt.json and default.json rules should be available
6773
// because we always append new -rule to the default.json by default
68-
// (unless we use -rule=+... syntax) to explicitly disable default rules.
69-
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
70-
ExpectPreprocessContains(t, shared.DebugLogFile, "database/sql")
74+
ExpecPreprocessContains(t, shared.DebugLogFile, "fmt")
75+
ExpecPreprocessContains(t, shared.DebugLogFile, "database/sql")
7176
}
7277

7378
func TestBuildProject6(t *testing.T) {
7479
const AppName = "build"
7580
UseApp(AppName)
7681

77-
RunInstrument(t, "-debuglog", "-rule=+../../pkg/data/test_fmt.json",
78-
"-verbose", "go", "build", "m1")
79-
// only test_fmt.json rule should be available
80-
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
81-
ExpectPreprocessNotContains(t, shared.DebugLogFile, "database/sql")
82+
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/test_fmt.json", "-verbose")
83+
RunGoBuild(t, "go", "build", "m1")
84+
// only test_fmt.json should be available because -disabledefault is set
85+
ExpecPreprocessContains(t, shared.DebugLogFile, "fmt")
86+
ExpecPreprocessNotContains(t, shared.DebugLogFile, "database/sql")
8287
}

Diff for: test/databasesql_tests.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestMySql5xAccessDatabase(t *testing.T, env ...string) {
4646
mysqlC, mysqlPort := init5xMySqlContainer()
4747
defer testcontainers.CleanupContainer(t, mysqlC)
4848
UseApp("databasesql/mysql")
49-
RunInstrument(t, "-debuglog", "go", "build", "test_access_database.go")
49+
RunGoBuild(t, "go", "build", "test_access_database.go")
5050
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
5151
RunApp(t, "test_access_database", env...)
5252
}
@@ -55,7 +55,7 @@ func TestMySql5xFetchingDatabase(t *testing.T, env ...string) {
5555
mysqlC, mysqlPort := init5xMySqlContainer()
5656
defer testcontainers.CleanupContainer(t, mysqlC)
5757
UseApp("databasesql/mysql")
58-
RunInstrument(t, "-debuglog", "go", "build", "test_fetching_database.go")
58+
RunGoBuild(t, "go", "build", "test_fetching_database.go")
5959
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
6060
RunApp(t, "test_fetching_database", env...)
6161
}
@@ -64,7 +64,7 @@ func TestMySql5xModifyData(t *testing.T, env ...string) {
6464
mysqlC, mysqlPort := init5xMySqlContainer()
6565
defer testcontainers.CleanupContainer(t, mysqlC)
6666
UseApp("databasesql/mysql")
67-
RunInstrument(t, "-debuglog", "go", "build", "test_modify_data.go")
67+
RunGoBuild(t, "go", "build", "test_modify_data.go")
6868
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
6969
RunApp(t, "test_modify_data", env...)
7070
}
@@ -73,7 +73,7 @@ func TestMySql5xSingleRowQuery(t *testing.T, env ...string) {
7373
mysqlC, mysqlPort := init5xMySqlContainer()
7474
defer testcontainers.CleanupContainer(t, mysqlC)
7575
UseApp("databasesql/mysql")
76-
RunInstrument(t, "-debuglog", "go", "build", "test_single_row_query.go")
76+
RunGoBuild(t, "go", "build", "test_single_row_query.go")
7777
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
7878
RunApp(t, "test_single_row_query", env...)
7979
}
@@ -82,7 +82,7 @@ func TestMySql5xTransaction(t *testing.T, env ...string) {
8282
mysqlC, mysqlPort := init5xMySqlContainer()
8383
defer testcontainers.CleanupContainer(t, mysqlC)
8484
UseApp("databasesql/mysql")
85-
RunInstrument(t, "-debuglog", "go", "build", "test_transaction.go")
85+
RunGoBuild(t, "go", "build", "test_transaction.go")
8686
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
8787
RunApp(t, "test_transaction", env...)
8888
}
@@ -91,7 +91,7 @@ func TestMySql5xPreparedStatement(t *testing.T, env ...string) {
9191
mysqlC, mysqlPort := init5xMySqlContainer()
9292
defer testcontainers.CleanupContainer(t, mysqlC)
9393
UseApp("databasesql/mysql")
94-
RunInstrument(t, "-debuglog", "go", "build", "test_prepared_statement.go")
94+
RunGoBuild(t, "go", "build", "test_prepared_statement.go")
9595
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
9696
RunApp(t, "test_prepared_statement", env...)
9797
}
@@ -100,7 +100,7 @@ func TestMySql8xAccessDatabase(t *testing.T, env ...string) {
100100
mysqlC, mysqlPort := init8xMySqlContainer()
101101
defer testcontainers.CleanupContainer(t, mysqlC)
102102
UseApp("databasesql/mysql")
103-
RunInstrument(t, "-debuglog", "go", "build", "test_access_database.go")
103+
RunGoBuild(t, "go", "build", "test_access_database.go")
104104
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
105105
RunApp(t, "test_access_database", env...)
106106
}
@@ -109,7 +109,7 @@ func TestMySql8xFetchingDatabase(t *testing.T, env ...string) {
109109
mysqlC, mysqlPort := init8xMySqlContainer()
110110
defer testcontainers.CleanupContainer(t, mysqlC)
111111
UseApp("databasesql/mysql")
112-
RunInstrument(t, "-debuglog", "go", "build", "test_fetching_database.go")
112+
RunGoBuild(t, "go", "build", "test_fetching_database.go")
113113
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
114114
RunApp(t, "test_fetching_database", env...)
115115
}
@@ -118,7 +118,7 @@ func TestMySql8xModifyData(t *testing.T, env ...string) {
118118
mysqlC, mysqlPort := init8xMySqlContainer()
119119
defer testcontainers.CleanupContainer(t, mysqlC)
120120
UseApp("databasesql/mysql")
121-
RunInstrument(t, "-debuglog", "go", "build", "test_modify_data.go")
121+
RunGoBuild(t, "go", "build", "test_modify_data.go")
122122
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
123123
RunApp(t, "test_modify_data", env...)
124124
}
@@ -127,7 +127,7 @@ func TestSingleRowQuery(t *testing.T, env ...string) {
127127
mysqlC, mysqlPort := init8xMySqlContainer()
128128
defer testcontainers.CleanupContainer(t, mysqlC)
129129
UseApp("databasesql/mysql")
130-
RunInstrument(t, "-debuglog", "go", "build", "test_single_row_query.go")
130+
RunGoBuild(t, "go", "build", "test_single_row_query.go")
131131
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
132132
RunApp(t, "test_single_row_query", env...)
133133
}
@@ -136,7 +136,7 @@ func TestTransaction(t *testing.T, env ...string) {
136136
mysqlC, mysqlPort := init8xMySqlContainer()
137137
defer testcontainers.CleanupContainer(t, mysqlC)
138138
UseApp("databasesql/mysql")
139-
RunInstrument(t, "-debuglog", "go", "build", "test_transaction.go")
139+
RunGoBuild(t, "go", "build", "test_transaction.go")
140140
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
141141
RunApp(t, "test_transaction", env...)
142142
}
@@ -145,7 +145,7 @@ func TestPreparedStatement(t *testing.T, env ...string) {
145145
mysqlC, mysqlPort := init8xMySqlContainer()
146146
defer testcontainers.CleanupContainer(t, mysqlC)
147147
UseApp("databasesql/mysql")
148-
RunInstrument(t, "-debuglog", "go", "build", "test_prepared_statement.go")
148+
RunGoBuild(t, "go", "build", "test_prepared_statement.go")
149149
env = append(env, "MYSQL_PORT="+mysqlPort.Port())
150150
RunApp(t, "test_prepared_statement", env...)
151151
}

Diff for: test/echo_tests.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ func init() {
3232

3333
func TestBasicEcho(t *testing.T, env ...string) {
3434
UseApp("echo/v4.0.0")
35-
RunInstrument(t, "-debuglog", "go", "build", "test_echo_basic.go")
35+
RunGoBuild(t, "go", "build", "test_echo_basic.go")
3636
RunApp(t, "test_echo_basic", env...)
3737
}
3838

3939
func TestEchoPattern(t *testing.T, env ...string) {
4040
UseApp("echo/v4.0.0")
41-
RunInstrument(t, "-debuglog", "go", "build", "test_echo_pattern.go")
41+
RunGoBuild(t, "go", "build", "test_echo_pattern.go")
4242
RunApp(t, "test_echo_pattern", env...)
4343
}
4444

4545
func TestEchoMiddleware(t *testing.T, env ...string) {
4646
UseApp("echo/v4.10.0")
47-
RunInstrument(t, "-debuglog", "go", "build", "test_echo_middleware.go")
47+
RunGoBuild(t, "go", "build", "test_echo_middleware.go")
4848
RunApp(t, "test_echo_middleware", env...)
4949
}

Diff for: test/elasticsearch_tests.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ package test
1717
import (
1818
"context"
1919
"fmt"
20-
"github.com/docker/go-connections/nat"
21-
"github.com/testcontainers/testcontainers-go"
2220
"testing"
2321
"time"
22+
23+
"github.com/docker/go-connections/nat"
24+
"github.com/testcontainers/testcontainers-go"
2425
)
2526

2627
const es_v8_dependency_name = "github.com/elastic/go-elasticsearch/v8"
@@ -43,7 +44,7 @@ func TestESCrud(t *testing.T, env ...string) {
4344
esC, esPort := initElasticSearchContainer()
4445
defer testcontainers.CleanupContainer(t, esC)
4546
UseApp("elasticsearch/v8.4.0")
46-
RunInstrument(t, "-debuglog", "go", "build", "test_es_crud.go")
47+
RunGoBuild(t, "go", "build", "test_es_crud.go")
4748
env = append(env, "OTEL_ES_PORT="+esPort.Port())
4849
RunApp(t, "test_es_crud", env...)
4950
}
@@ -52,7 +53,7 @@ func TestESTypedClient(t *testing.T, env ...string) {
5253
esC, esPort := initElasticSearchContainer()
5354
defer testcontainers.CleanupContainer(t, esC)
5455
UseApp("elasticsearch/v8.5.0")
55-
RunInstrument(t, "-debuglog", "go", "build", "test_es_typedclient.go")
56+
RunGoBuild(t, "go", "build", "test_es_typedclient.go")
5657
env = append(env, "OTEL_ES_PORT="+esPort.Port())
5758
RunApp(t, "test_es_typedclient", env...)
5859
}

Diff for: test/errors_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const ErrorsAppName = "errorstest"
2323

2424
func TestRunErrors(t *testing.T) {
2525
UseApp(ErrorsAppName)
26-
RunInstrument(t, UseTestRules("test_error.json"), "-debuglog", "go", "build")
26+
RunSet(t, UseTestRules("test_error.json"))
27+
RunGoBuild(t, "go", "build")
2728
stdout, stderr := RunApp(t, ErrorsAppName)
2829
ExpectContains(t, stdout, "wow")
2930
ExpectContains(t, stdout, "old:wow")

0 commit comments

Comments
 (0)