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

app: log git commit hash #368

Merged
merged 2 commits into from
Apr 6, 2022
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
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func Run(ctx context.Context, conf Config) (err error) {
return err
}

log.Info(ctx, "Charon starting", z.Str("version", version.Version))
hash, timestamp := GitCommit()
log.Info(ctx, "Charon starting",
z.Str("version", version.Version),
z.Str("git_commit_hash", hash),
z.Str("git_commit_time", timestamp),
)

// Wire processes and their dependencies
life := new(lifecycle.Manager)
Expand Down
31 changes: 31 additions & 0 deletions app/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package app

import (
"runtime/debug"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand All @@ -29,6 +31,12 @@ var (
Help: "Constant gauge with label set to current app version",
}, []string{"version"})

gitGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app",
Name: "git_commit",
Help: "Constant gauge with label set to current git commit hash",
}, []string{"hash"})

startGauge = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "app",
Name: "start_time_secs",
Expand All @@ -39,4 +47,27 @@ var (
func initStartupMetrics() {
versionGauge.WithLabelValues(version.Version).Set(1)
startGauge.SetToCurrentTime()

hash, _ := GitCommit()
gitGauge.WithLabelValues(hash).Set(1)
}

// GitCommit returns the git commit hash and timestamp from build info.
func GitCommit() (hash string, timestamp string) {
hash, timestamp = "unknown", "unknown"

info, ok := debug.ReadBuildInfo()
if !ok {
return hash, timestamp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we returning unknown strings if it is unable to read build info?
Why not return an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it isn't mission critical, it is just "extra information". And it depends on how the binary was built, there is no guarantee that the data is available. For example go run charon/main.go doesn't include this. Neither does go test. So if we return an error, we cannot go run or go test this code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

}

for _, s := range info.Settings {
if s.Key == "vcs.revision" {
hash = s.Value[:7]
} else if s.Key == "vcs.time" {
timestamp = s.Value
}
}

return hash, timestamp
}
4 changes: 3 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/obolnetwork/charon/app"
"github.com/obolnetwork/charon/app/version"
)

Expand Down Expand Up @@ -53,7 +54,8 @@ func bindVersionFlags(flags *pflag.FlagSet, config *versionConfig) {
}

func runVersionCmd(out io.Writer, config versionConfig) {
_, _ = fmt.Fprintln(out, version.Version)
hash, timestamp := app.GitCommit()
_, _ = fmt.Fprintf(out, "%s [git_commit_hash=%s,git_commit_time=%s]\n", version.Version, hash, timestamp)

if !config.Verbose {
return
Expand Down