Skip to content

Commit

Permalink
Add -version flag to print version info (#13)
Browse files Browse the repository at this point in the history
* Add -version flag to print version

* add go version
  • Loading branch information
keisku authored Jun 11, 2024
1 parent 1faa2d1 commit 87e2337
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gmon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dockerfile_buildenv=$(mktemp)
cat > "$dockerfile_buildenv" <<EOF
FROM debian:bookworm-20240513@sha256:fac2c0fd33e88dfd3bc88a872cfb78dcb167e74af6162d31724df69e482f886c
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
wget \
llvm-14 \
clang-14 \
Expand All @@ -54,9 +55,12 @@ docker run --platform linux/$arch -i \
-e BPF_CLANG="clang" \
-e BPF_CFLAGS="-O2 -g -Wall -Werror" \
--rm $image_buildenv bash -c '\
git config --global --add safe.directory /usr/src && \
bpftool btf dump file /sys/kernel/btf/vmlinux format c > ./ebpf/c/vmlinux.h && \
go generate -x ./... && \
GOFLAGS="-buildvcs=false" CGO_ENABLED=0 go build -o /usr/src/bin/gmon'
GOFLAGS="-buildvcs=auto" CGO_ENABLED=0 go build \
-ldflags "-s -w -X main.Version=0.0.0-dev" \
-o /usr/src/bin/gmon'
if [ "$1" = "build" ]; then
exit 0
fi
Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/trace"
"strings"

Expand Down Expand Up @@ -45,6 +46,10 @@ var (
traceOutPath = flag.String("trace", "", "Path to Go runtime/trace output")
pprofPort = flag.Int("pprof", 0, "Port to be used for pprof server. If 0, pprof server is not started")
metricsPort = flag.Int("metrics", 5500, "Port to be used for metrics server, /metrics endpoint")
printVersion = flag.Bool("version", false, "Print version information")

// Set by -ldflags at build time
Version = "unknown"
)

type promLogger struct{}
Expand All @@ -55,6 +60,25 @@ func (promLogger) Println(v ...interface{}) {

func main() {
flag.Parse()
if *printVersion {
var gover, arch, goos, commitHash = "unknown", "unknown", "unknown", "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
gover = info.GoVersion
for _, s := range info.Settings {
switch s.Key {
case "GOARCH":
arch = s.Value
case "GOOS":
goos = s.Value
case "vcs.revision":
commitHash = s.Value
}
}
}
fmt.Printf("gmon %s (%s/%s) %s, commit=%s\n", Version, goos, arch, gover, commitHash)
return
}

opts := &slog.HandlerOptions{Level: levelMap[*level]}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, opts)))
errlog := log.New(os.Stderr, "", log.LstdFlags)
Expand Down

0 comments on commit 87e2337

Please sign in to comment.