Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ demo/demo

otel.runtime.go
otel-pkg.gz
otel-contrib.gz
pkg_temp
coverage.txt
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ SHELL := /bin/bash
BINARY_NAME := otel
TOOL_DIR := tool/cmd
INST_PKG_GZIP = otel-pkg.gz
INST_CONTRIB_GZIP = otel-contrib.gz
INST_CONTRIB_TMP = contrib_temp
INST_PKG_TMP = pkg_temp
API_SYNC_SOURCE = pkg/inst/context.go
API_SYNC_TARGET = tool/internal/instrument/api.tmpl
Expand Down Expand Up @@ -60,6 +62,7 @@ install: ## Install otel to $$GOPATH/bin
package: ## Package the instrumentation code into binary
@echo "Packaging instrumentation code into binary..."
@set -euo pipefail
# pkg
rm -rf $(INST_PKG_TMP)
if [ ! -d pkg ]; then \
echo "Error: pkg directory does not exist"; \
Expand All @@ -73,6 +76,20 @@ package: ## Package the instrumentation code into binary
rm -rf $(INST_PKG_TMP)
@echo "Package created successfully at tool/data/$(INST_PKG_GZIP)"

# contrib
rm -rf $(INST_CONTRIB_TMP)
if [ ! -d contrib ]; then \
echo "Error: contrib directory does not exist"; \
exit 1; \
fi
cp -r contrib $(INST_CONTRIB_TMP)
(cd $(INST_CONTRIB_TMP) && go mod tidy)
tar -czf $(INST_CONTRIB_GZIP) --exclude='*.log' $(INST_CONTRIB_TMP)
mkdir -p tool/data/
mv $(INST_CONTRIB_GZIP) tool/data/
rm -rf $(INST_CONTRIB_TMP)
@echo "Package created successfully at tool/data/$(INST_CONTRIB_GZIP)"

build-demo: ## Build all demos
build-demo: build-demo-grpc build-demo-http

Expand Down Expand Up @@ -191,6 +208,11 @@ check-embed: ## Verify that embedded files exist (required for tests)
echo "Run 'make package' to generate it"; \
exit 1; \
fi
@if [ ! -f tool/data/$(INST_CONTRIB_GZIP) ]; then \
echo "Error: tool/data/$(INST_CONTRIB_GZIP) does not exist"; \
echo "Run 'make package' to generate it"; \
exit 1; \
fi
@echo "All embedded files present"

##@ Testing
Expand Down
5 changes: 5 additions & 0 deletions contrib/grpc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/grpc

go 1.23.0

require github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3
2 changes: 2 additions & 0 deletions contrib/grpc/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3 h1:aKlE1r2trNK/cWtB6n39ALYvBdTrCb+jzeHRSaQSjhM=
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3/go.mod h1:bh6h/DRFNGk+tD5T8obFCFwdOVP16tzIiQWCYk1AWc0=
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/in

go 1.23.0

replace github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg => ../..

require (
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251124021638-b2f0cf1e96a8
go.opentelemetry.io/otel v1.38.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251014023539-a5ba44c6591d h1:mRDAFbr7cP+OZZQ8M2zbSwt8rfi9HU4HfZKoFu4dPOE=
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251014023539-a5ba44c6591d/go.mod h1:bh6h/DRFNGk+tD5T8obFCFwdOVP16tzIiQWCYk1AWc0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/in

go 1.23.0

replace github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg => ../..
require github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3
2 changes: 2 additions & 0 deletions contrib/nethttp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3 h1:aKlE1r2trNK/cWtB6n39ALYvBdTrCb+jzeHRSaQSjhM=
github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg v0.0.0-20251114091432-696aafd122b3/go.mod h1:bh6h/DRFNGk+tD5T8obFCFwdOVP16tzIiQWCYk1AWc0=
File renamed without changes.
2 changes: 2 additions & 0 deletions demo/http/client/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/open-telemetry/opentelemetry-go-compile-instrumentation/demo/http/client

go 1.23.0

replace github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib => ../../../contrib/
2 changes: 2 additions & 0 deletions demo/http/server/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/open-telemetry/opentelemetry-go-compile-instrumentation/demo/http/server

go 1.23.0

replace github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib => ../../../contrib/
2 changes: 1 addition & 1 deletion docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ hook_helloworld:
func: Example
before: MyHookBefore
after: MyHookAfter
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/helloworld"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/helloworld"
```

This rule will inject `MyHookBefore` at the start of the `Example` function in the `main` package, and `MyHookAfter` at the end. The hook functions are located in the specified `path`.
Expand Down
5 changes: 0 additions & 5 deletions pkg/instrumentation/grpc/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion tool/data/grpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ server_hook:
func: Serve
recv: "*Server"
before: BeforeServe
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/grpc"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/grpc"
8 changes: 4 additions & 4 deletions tool/data/helloworld.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ hook_helloworld:
target: main
func: Example
before: MyHookBefore
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/helloworld"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/helloworld"

add_new_field:
target: main
Expand All @@ -23,7 +23,7 @@ hook_recv:
recv: "*MyStruct"
before: MyHook1Before
after: MyHook1After
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/helloworld"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/helloworld"

version_range:
target: golang.org/x/time/rate
Expand All @@ -50,10 +50,10 @@ only_after:
target: main
func: Example
after: MyHookAfter
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/helloworld"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/helloworld"

underscore_param:
target: main
func: Underscore
before: BeforeUnderscore
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/helloworld"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/helloworld"
2 changes: 1 addition & 1 deletion tool/data/nethttp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ server_hook:
func: ServeHTTP
recv: serverHandler
before: BeforeServeHTTP
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/nethttp"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/nethttp"
2 changes: 1 addition & 1 deletion tool/data/runtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_gls_field:
gls_linker:
target: "runtime"
file: "runtime_gls.go"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pkg/instrumentation/runtime"
path: "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/contrib/runtime"

goroutine_propagate:
target: "runtime"
Expand Down
4 changes: 3 additions & 1 deletion tool/ex/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ func Fatal(err error) {
e := &stackfulError{}
if errors.As(err, &e) {
em := ""
var emSb149 strings.Builder
for i, m := range e.message {
em += fmt.Sprintf("[%d] %s\n", i, m)
emSb149.WriteString(fmt.Sprintf("[%d] %s\n", i, m))
}
em += emSb149.String()
_, _ = fmt.Fprintf(os.Stderr, "Error:\n%s\nStack:\n%s\n",
em, strings.Join(e.frame, "\n"))
os.Exit(1)
Expand Down
32 changes: 26 additions & 6 deletions tool/internal/setup/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ const (
)

func normalizePath(name string) string {
const pkg, pkgTemp = unzippedPkgDir, "pkg_temp"
const pkg, pkgTemp = "pkg", "pkg_temp"
const contrib, contribTemp = "contrib", "contrib_temp"

cleanName := filepath.ToSlash(filepath.Clean(name))
if strings.HasPrefix(cleanName, pkgTemp+"/") {
cleanName = strings.Replace(cleanName, pkgTemp+"/", pkg+"/", 1)
} else if cleanName == pkgTemp {
cleanName = pkg
switch {
case cleanName == pkgTemp:
return pkg
case cleanName == contribTemp:
return contrib
case strings.HasPrefix(cleanName, pkgTemp+"/"):
return strings.Replace(cleanName, pkgTemp+"/", pkg+"/", 1)

case strings.HasPrefix(cleanName, contribTemp+"/"):
return strings.Replace(cleanName, contribTemp+"/", contrib+"/", 1)

default:
return cleanName
}
return cleanName
}

func extract(tarReader *tar.Reader, header *tar.Header, targetPath string) error {
Expand Down Expand Up @@ -129,11 +139,21 @@ func extractGZip(data []byte, targetDir string) error {

func (*SetupPhase) extract() error {
const embeddedInstPkgGzip = "otel-pkg.gz"
const embeddedInstContribGzib = "otel-contrib.gz"
// Read the instrumentation code from the embedded binary file
bs, err := data.ReadEmbedFile(embeddedInstPkgGzip)
if err != nil {
return err
}
err = extractGZip(bs, util.GetBuildTempDir())
if err != nil {
return err
}

bs, err = data.ReadEmbedFile(embeddedInstContribGzib)
if err != nil {
return err
}

// Extract the instrumentation code to the build temp directory
// for future instrumentation phase
Expand Down