Skip to content

Commit

Permalink
app: log git commit hash (#368)
Browse files Browse the repository at this point in the history
Logs git commit hash and time on startup. Also set commit hash metric and add to version command.

category: feature
ticket: #367
  • Loading branch information
corverroos authored Apr 6, 2022
1 parent 9a2c216 commit b029922
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
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
}

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

0 comments on commit b029922

Please sign in to comment.