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

Add Go version detection #485

Merged
merged 4 commits into from
Dec 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
go.opentelemetry.io/otel/sdk/metric v0.41.0
go.opentelemetry.io/otel/trace v1.19.0
golang.org/x/arch v0.3.0
golang.org/x/mod v0.8.0
golang.org/x/sys v0.12.0
google.golang.org/grpc v1.58.3
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -88,7 +89,6 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down Expand Up @@ -269,6 +268,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
5 changes: 4 additions & 1 deletion pkg/internal/discover/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func (ta *TraceAttacher) getTracer(ie *Instrumentable) (*ebpf.ProcessTracer, boo
case svc.InstrumentableGolang:
// gets all the possible supported tracers for a go program, and filters out
// those whose symbols are not present in the ELF functions list
if ta.Cfg.Discovery.SkipGoSpecificTracers {
if ta.Cfg.Discovery.SkipGoSpecificTracers || ie.InstrumentationError != nil {
if ie.InstrumentationError != nil {
ta.log.Warn("Unsupported Go program detected, using generic instrumentation", "error", ie.InstrumentationError)
}
if ta.reusableTracer != nil {
programs = newNonGoTracersGroupUProbes(ta.Cfg, ta.Metrics)
} else {
Expand Down
18 changes: 10 additions & 8 deletions pkg/internal/discover/typer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type ExecTyper struct {
}

type Instrumentable struct {
Type svc.InstrumentableType
Type svc.InstrumentableType
InstrumentationError error

// in some runtimes, like python gunicorn, we need to allow
// tracing both the parent pid and all of its children pid
Expand Down Expand Up @@ -106,7 +107,7 @@ func (t *typer) asInstrumentable(execElf *exec.FileInfo) Instrumentable {
log := t.log.With("pid", execElf.Pid, "comm", execElf.CmdExePath)
log.Debug("getting instrumentable information")
// look for suitable Go application first
offsets, ok := t.inspectOffsets(execElf)
offsets, ok, err := t.inspectOffsets(execElf)
if ok {
// we found go offsets, let's see if this application is not a proxy
if !isGoProxy(offsets) {
Expand Down Expand Up @@ -139,23 +140,24 @@ func (t *typer) asInstrumentable(execElf *exec.FileInfo) Instrumentable {
"child", child, "language", detectedType.String())
// Return the instrumentable without offsets, as it is identified as a generic
// (or non-instrumentable Go proxy) executable
return Instrumentable{Type: detectedType, FileInfo: execElf, ChildPids: child}
return Instrumentable{Type: detectedType, FileInfo: execElf, ChildPids: child, InstrumentationError: err}
}

func (t *typer) inspectOffsets(execElf *exec.FileInfo) (*goexec.Offsets, bool) {
func (t *typer) inspectOffsets(execElf *exec.FileInfo) (*goexec.Offsets, bool, error) {
if !t.cfg.Discovery.SystemWide {
if t.cfg.Discovery.SkipGoSpecificTracers {
t.log.Debug("skipping inspection for Go functions", "pid", execElf.Pid, "comm", execElf.CmdExePath)
} else {
t.log.Debug("inspecting", "pid", execElf.Pid, "comm", execElf.CmdExePath)
if offsets, err := goexec.InspectOffsets(execElf, t.allGoFunctions); err != nil {
offsets, err := goexec.InspectOffsets(execElf, t.allGoFunctions)
if err != nil {
t.log.Debug("couldn't find go specific tracers", "error", err)
} else {
return offsets, true
return nil, false, err
}
return offsets, true, nil
}
}
return nil, false
return nil, false, nil
}

func isGoProxy(offsets *goexec.Offsets) bool {
Expand Down
21 changes: 21 additions & 0 deletions pkg/internal/goexec/gofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ import (
"encoding/binary"
"errors"
"fmt"
"regexp"
"strings"

"golang.org/x/mod/semver"
)

// minGoVersion defines the minimum instrumentable Go version. If the target binary was
// compiled using an older Go version, it will be treated as a non-Go program.
const minGoVersion = "1.17"

// supportedGoVersion checks if the given Go version string is equal or greater than the
// minimum supported version.
func supportedGoVersion(version string) bool {
re := regexp.MustCompile(`\d+\.\d+(?:\.\d+)?`)
match := re.FindStringSubmatch(version)
if match == nil {
return false
}
version = match[0]
// 'semver' package requires version strings to begin with a leading "v".
return semver.Compare("v"+version, "v"+minGoVersion) >= 0
}

// findLibraryVersions looks for all the libraries and versions inside the elf file.
// It returns a map where the key is the library name and the value is the library version
func findLibraryVersions(elfFile *elf.File) (map[string]string, error) {
Expand All @@ -20,6 +40,7 @@ func findLibraryVersions(elfFile *elf.File) (map[string]string, error) {

goVersion = strings.ReplaceAll(goVersion, "go", "")
log().Debug("Go version detected", "version", goVersion)

modsMap := parseModules(modules)
modsMap["go"] = goVersion
return modsMap, nil
Expand Down
50 changes: 50 additions & 0 deletions pkg/internal/goexec/gofile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package goexec

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSupportedGoVersion(t *testing.T) {
tests := []struct {
input string
want bool
}{
// Unsupported versions
{input: "1.15", want: false},
{input: "1.15.1", want: false},
{input: "1.15.15", want: false},
{input: "1.16beta1", want: false},
{input: "1.16rc1", want: false},
{input: "1.16", want: false},
{input: "1.16.1", want: false},
{input: "1.16.15", want: false},

// Supported versions
{input: "1.17", want: true},
{input: "1.17beta1", want: true},
{input: "1.17rc1", want: true},
{input: "1.17rc2", want: true},
{input: "1.17.1", want: true},
{input: "1.17.13", want: true},
{input: "1.18", want: true},
{input: "1.18.9", want: true},

// Uncleaned Go version strings
{input: "go1.16.4", want: false},
{input: "go1.21.4", want: true},
{input: "devel go1.22-098f059 Mon Dec 4 23:03:04 2023 +0000", want: true},

// Invalid versions
{input: "devel", want: false},
{input: "go", want: false},
{input: "098f059", want: false},
{input: "Mon Dec 4 23:03:04 2023 +0000", want: false},
}

for _, tt := range tests {
got := supportedGoVersion(tt.input)
assert.Equal(t, tt.want, got, "input: %v", tt.input)
}
}
7 changes: 7 additions & 0 deletions pkg/internal/goexec/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// instrumentationPoints loads the provided executable and looks for the addresses
// where the start and return probes must be inserted.
//
//nolint:cyclop
func instrumentationPoints(elfF *elf.File, funcNames []string) (map[string]FuncOffsets, error) {
ilog := slog.With("component", "goexec.instructions")
ilog.Debug("searching for instrumentation points", "functions", funcNames)
Expand All @@ -24,6 +26,11 @@ func instrumentationPoints(elfF *elf.File, funcNames []string) (map[string]FuncO
return nil, err
}

goVersion, _, err := getGoDetails(elfF)
if err == nil && !supportedGoVersion(goVersion) {
return nil, fmt.Errorf("unsupported Go version: %v. Minimum supported version is %v", goVersion, minGoVersion)
}

gosyms := elfF.Section(".gosymtab")

var allSyms map[string]exec.Sym
Expand Down
1 change: 0 additions & 1 deletion pkg/internal/goexec/structmembers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func structMemberOffsets(elfFile *elf.File) (FieldOffsets, error) {
dwarfData, err := elfFile.DWARF()
if err == nil {
offs, expected = structMemberOffsetsFromDwarf(dwarfData)

if len(expected) > 0 {
log().Debug("Fields not found in the DWARF file", "fields", expected)
} else {
Expand Down
3 changes: 1 addition & 2 deletions test/integration/docker-compose-1.16.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ services:
BEYLA_METRICS_REPORT_PEER: "true"
BEYLA_INTERNAL_METRICS_PROMETHEUS_PORT: 8999
BEYLA_HOSTNAME: "beyla"
BEYLA_SKIP_GO_SPECIFIC_TRACERS: "true"
ports:
- "8999:8999" # Prometheus scrape port, if enabled via config

Expand Down Expand Up @@ -81,4 +80,4 @@ services:
volumes:
- ./configs/:/etc/prometheus
ports:
- "9090:9090"
- "9090:9090"
21 changes: 0 additions & 21 deletions vendor/github.com/stretchr/objx/.codeclimate.yml

This file was deleted.

11 changes: 0 additions & 11 deletions vendor/github.com/stretchr/objx/.gitignore

This file was deleted.

22 changes: 0 additions & 22 deletions vendor/github.com/stretchr/objx/LICENSE

This file was deleted.

80 changes: 0 additions & 80 deletions vendor/github.com/stretchr/objx/README.md

This file was deleted.

30 changes: 0 additions & 30 deletions vendor/github.com/stretchr/objx/Taskfile.yml

This file was deleted.

Loading