Skip to content

Commit e65f090

Browse files
authored
Add instrgen implementation (#3108)
* Add instrgen implementation * check whether projectPath is directory * use switch instead of if statement * remove some attributes * adding usage to readme * update CHANGELOG.md --------- Co-authored-by: Przemek Delewski <[email protected]>
1 parent 81f1a7e commit e65f090

File tree

40 files changed

+2921
-1
lines changed

40 files changed

+2921
-1
lines changed

.github/dependabot.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ updates:
199199
schedule:
200200
interval: weekly
201201
day: sunday
202+
- package-ecosystem: gomod
203+
directory: /instrgen/driver
204+
labels:
205+
- dependencies
206+
- go
207+
- Skip Changelog
208+
schedule:
209+
interval: weekly
210+
day: sunday
211+
- package-ecosystem: gomod
212+
directory: /instrgen/driver/testdata/interface
213+
labels:
214+
- dependencies
215+
- go
216+
- Skip Changelog
217+
schedule:
218+
interval: weekly
219+
day: sunday
202220
- package-ecosystem: gomod
203221
directory: /instrumentation/github.com/Shopify/sarama/otelsarama
204222
labels:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1010

1111
### Added
1212

13-
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068)
13+
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
1414

1515
## [1.16.0-rc.2/0.41.0-rc.2/0.10.0-rc.2] - 2023-03-23
1616

instrgen/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ This package provides a code generation utility that instruments existing source
66

77
:construction: This package is currently work in progress.
88

9+
## How to use it
10+
11+
In order to instrument your project you have to add following call in your entry point function, usually main
12+
(you can look at testdata directory for reference) and invoke instrgen tool.
13+
14+
```
15+
func main() {
16+
rtlib.AutotelEntryPoint()
17+
```
18+
19+
Instrgen requires three parameters: command, path to project and package(s) pattern we
20+
would like to instrument.
21+
22+
```
23+
./instrgen --inject [path to your go project] [package(s) pattern]
24+
```
25+
26+
Below concrete example with one of test instrumentation that is part of the project.
27+
28+
```
29+
./instrgen --inject ./testdata/basic ./...
30+
```
31+
32+
```./...``` works like wildcard in this case and it will instrument all packages in this path, but it can be invoked with
33+
specific package as well.
34+
935
### Compatibility
1036

1137
The `instrgen` utility is based on the Go standard library and is platform agnostic.

instrgen/driver/go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module go.opentelemetry.io/contrib/instrgen/instrgen
2+
3+
go 1.19
4+
5+
replace go.opentelemetry.io/contrib/instrgen => ../
6+
7+
require (
8+
github.com/stretchr/testify v1.8.1
9+
go.opentelemetry.io/contrib/instrgen v0.0.0-00010101000000-000000000000
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
golang.org/x/mod v0.7.0 // indirect
16+
golang.org/x/sys v0.3.0 // indirect
17+
golang.org/x/tools v0.4.0 // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
19+
)

instrgen/driver/go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
14+
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
15+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
16+
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
17+
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18+
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
19+
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
21+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
22+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

instrgen/driver/instrgen_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !windows
16+
17+
package main
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"os"
23+
"path/filepath"
24+
"testing"
25+
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
29+
alib "go.opentelemetry.io/contrib/instrgen/lib"
30+
)
31+
32+
var testcases = map[string]string{
33+
"./testdata/basic": "./testdata/expected/basic",
34+
"./testdata/selector": "./testdata/expected/selector",
35+
"./testdata/interface": "./testdata/expected/interface",
36+
}
37+
38+
var failures []string
39+
40+
func inject(t *testing.T, root string, packagePattern string) {
41+
err := executeCommand("--inject-dump-ir", root, packagePattern)
42+
require.NoError(t, err)
43+
}
44+
45+
func Test(t *testing.T) {
46+
for k, v := range testcases {
47+
inject(t, k, "./...")
48+
files := alib.SearchFiles(k, ".go_pass_tracing")
49+
expectedFiles := alib.SearchFiles(v, ".go")
50+
numOfFiles := len(expectedFiles)
51+
fmt.Println("Go Files:", len(files))
52+
fmt.Println("Expected Go Files:", len(expectedFiles))
53+
numOfComparisons := 0
54+
for _, file := range files {
55+
fmt.Println(filepath.Base(file))
56+
for _, expectedFile := range expectedFiles {
57+
fmt.Println(filepath.Base(expectedFile))
58+
if filepath.Base(file) == filepath.Base(expectedFile+"_pass_tracing") {
59+
f1, err1 := os.ReadFile(file)
60+
require.NoError(t, err1)
61+
f2, err2 := os.ReadFile(expectedFile)
62+
require.NoError(t, err2)
63+
if !assert.True(t, bytes.Equal(f1, f2)) {
64+
fmt.Println(k)
65+
failures = append(failures, k)
66+
}
67+
numOfComparisons = numOfComparisons + 1
68+
}
69+
}
70+
}
71+
if numOfFiles != numOfComparisons {
72+
fmt.Println("numberOfComparisons:", numOfComparisons)
73+
panic("not all files were compared")
74+
}
75+
_, err := Prune(k, "./...", false)
76+
if err != nil {
77+
fmt.Println("Prune failed")
78+
}
79+
}
80+
for _, f := range failures {
81+
fmt.Println("FAILURE : ", f)
82+
}
83+
}
84+
85+
func TestCommands(t *testing.T) {
86+
err := executeCommand("--dumpcfg", "./testdata/dummy", "./...")
87+
require.NoError(t, err)
88+
err = executeCommand("--rootfunctions", "./testdata/dummy", "./...")
89+
require.NoError(t, err)
90+
err = executeCommand("--prune", "./testdata/dummy", "./...")
91+
require.NoError(t, err)
92+
err = executeCommand("--inject", "./testdata/dummy", "./...")
93+
require.NoError(t, err)
94+
err = usage()
95+
require.NoError(t, err)
96+
}
97+
98+
func TestCallGraph(t *testing.T) {
99+
cg := makeCallGraph("./testdata/dummy", "./...")
100+
dumpCallGraph(cg)
101+
assert.Equal(t, len(cg), 0, "callgraph should contain 0 elems")
102+
rf := makeRootFunctions("./testdata/dummy", "./...")
103+
dumpRootFunctions(rf)
104+
assert.Equal(t, len(rf), 0, "rootfunctions set should be empty")
105+
}
106+
107+
func TestArgs(t *testing.T) {
108+
err := checkArgs(nil)
109+
require.Error(t, err)
110+
args := []string{"driver", "--inject", "", "./..."}
111+
err = checkArgs(args)
112+
require.NoError(t, err)
113+
}
114+
115+
func TestUnknown(t *testing.T) {
116+
err := executeCommand("unknown", "a", "b")
117+
require.Error(t, err)
118+
}

0 commit comments

Comments
 (0)