diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e67902a..5a0ebfbed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http - Change HTTP client span name to `{http.request.method}` ([#775](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/775)) - Don't set empty URL path in HTTP client probe. ([#810](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/810)) - Don't fail HTTP client probe attribute resolution on empty URL path. ([#810](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/810)) - +- Extract `process.runtime.version` and `process.runtime.name` from instrumented process. ([#811](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/811)) ## [v0.12.0-alpha] - 2024-04-10 diff --git a/instrumentation.go b/instrumentation.go index 0402a1401..32e02534d 100644 --- a/instrumentation.go +++ b/instrumentation.go @@ -16,6 +16,7 @@ package auto import ( "context" + "debug/buildinfo" "errors" "fmt" "log" @@ -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 } @@ -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" } diff --git a/internal/pkg/process/analyze.go b/internal/pkg/process/analyze.go index 5ea92c89c..d20e41bf5 100644 --- a/internal/pkg/process/analyze.go +++ b/internal/pkg/process/analyze.go @@ -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") @@ -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 { diff --git a/internal/pkg/process/discover.go b/internal/pkg/process/discover.go index 9d790cf9a..a55cdf792 100644 --- a/internal/pkg/process/discover.go +++ b/internal/pkg/process/discover.go @@ -16,6 +16,7 @@ package process import ( "context" + "debug/buildinfo" "errors" "io" "os" @@ -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].