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

Extract go version and compiler from instrumneted process #811

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
### Fixed

- Change HTTP client span name to `{http.request.method}` ([#775](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/775))
- Extract `process.runtime.version` and `process.runtime.name` from instrumented process ([#811](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/811))
RonFed marked this conversation as resolved.
Show resolved Hide resolved


## [v0.12.0-alpha] - 2024-04-10
Expand Down
28 changes: 22 additions & 6 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package auto

import (
"context"
"debug/buildinfo"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -109,7 +110,12 @@ func NewInstrumentation(ctx context.Context, opts ...InstrumentationOption) (*In
return nil, err
}

ctrl, err := opentelemetry.NewController(logger, c.tracerProvider(), Version())
err = pa.SetBuildInfo(pid)
if err != nil {
return nil, err
}

ctrl, err := opentelemetry.NewController(logger, c.tracerProvider(pa.BuildInfo), Version())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -220,18 +226,28 @@ func (c instConfig) validate() error {
return c.target.Validate()
}

func (c instConfig) tracerProvider() *trace.TracerProvider {
func (c instConfig) tracerProvider(bi *buildinfo.BuildInfo) *trace.TracerProvider {
return trace.NewTracerProvider(
trace.WithSampler(c.sampler),
trace.WithResource(c.res()),
trace.WithResource(c.res(bi)),
trace.WithBatcher(c.traceExp),
trace.WithIDGenerator(opentelemetry.NewEBPFSourceIDGenerator()),
)
}

func (c instConfig) res() *resource.Resource {
runVer := strings.TrimPrefix(runtime.Version(), "go")
runName := runtime.Compiler
func (c instConfig) res(bi *buildinfo.BuildInfo) *resource.Resource {
runVer := strings.ReplaceAll(bi.GoVersion, "go", "")

var compiler string

for _, setting := range bi.Settings {
if setting.Key == "-compiler" {
compiler = setting.Value
break
}
}

runName := compiler
if runName == "gc" {
runName = "go"
}
Expand Down
26 changes: 19 additions & 7 deletions internal/pkg/process/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,13 @@ func (a *Analyzer) Analyze(pid int, relevantFuncs map[string]interface{}) (*Targ
return nil, err
}

buildInfo, err := buildinfo.Read(f)
if err != nil {
return nil, err
}
goVersion, err := version.NewVersion(strings.ReplaceAll(buildInfo.GoVersion, "go", ""))
goVersion, err := version.NewVersion(strings.ReplaceAll(a.BuildInfo.GoVersion, "go", ""))
if err != nil {
return nil, err
}
result.GoVersion = goVersion
result.Libraries = make(map[string]*version.Version, len(buildInfo.Deps)+1)
for _, dep := range buildInfo.Deps {
result.Libraries = make(map[string]*version.Version, len(a.BuildInfo.Deps)+1)
for _, dep := range a.BuildInfo.Deps {
depVersion, err := version.NewVersion(dep.Version)
if err != nil {
a.logger.Error(err, "error parsing module version")
Expand All @@ -118,6 +114,22 @@ func (a *Analyzer) Analyze(pid int, relevantFuncs map[string]interface{}) (*Targ
return result, nil
}

func (a *Analyzer) SetBuildInfo(pid int) error {
f, err := os.Open(fmt.Sprintf("/proc/%d/exe", pid))
if err != nil {
return err
}

defer f.Close()
bi, err := buildinfo.Read(f)
if err != nil {
return err
}

a.BuildInfo = bi
return nil
}

func (a *Analyzer) findFunctions(elfF *elf.File, relevantFuncs map[string]interface{}) ([]*binary.Func, error) {
result, err := binary.FindFunctionsUnStripped(elfF, relevantFuncs)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/process/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package process

import (
"context"
"debug/buildinfo"
"errors"
"io"
"os"
Expand All @@ -39,7 +40,8 @@ var (

// Analyzer is used to find actively running processes.
type Analyzer struct {
logger logr.Logger
logger logr.Logger
BuildInfo *buildinfo.BuildInfo
}

// NewAnalyzer returns a new [ProcessAnalyzer].
Expand Down
Loading