Skip to content

Commit

Permalink
chore(lib/buildinfo): log binary architecture (#2059)
Browse files Browse the repository at this point in the history
Log binary os.architecture on startup of all binaries. This aids in
debugging docker issues.

issue: none
  • Loading branch information
corverroos authored Oct 4, 2024
1 parent 5a6a450 commit 81fa18f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package buildinfo

import (
"context"
"debug/elf"
"debug/macho"
"os"
"runtime/debug"
"strings"
"time"
Expand Down Expand Up @@ -32,6 +35,7 @@ func Instrument(ctx context.Context) {
"version", version,
"git_commit", commit,
"git_timestamp", timestamp,
"arch", getArch(),
)

versionGauge.WithLabelValues(version).Set(1)
Expand Down Expand Up @@ -96,3 +100,27 @@ func get() (hash string, timestamp string) { //nolint:nonamedreturns // Disambig

return hash, timestamp
}

// getArch returns the running binary's architecture.
// This is best effort. It may not work on all platforms.
func getArch() string {
path, err := os.Executable()
if err != nil {
return "unknown"
}

if f, err := elf.Open(path); err == nil {
return f.Machine.String()
} else if f, err := macho.Open(path); err == nil {
return f.Cpu.String()
} else if f, err := macho.OpenFat(path); err == nil {
var arches []string
for _, a := range f.Arches {
arches = append(arches, a.Cpu.String())
}

return strings.Join(arches, ",")
}

return "unknown"
}

0 comments on commit 81fa18f

Please sign in to comment.