diff --git a/app/app.go b/app/app.go index ff342bb2d..823cd0823 100644 --- a/app/app.go +++ b/app/app.go @@ -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) diff --git a/app/metrics.go b/app/metrics.go index 057132850..440d887d2 100644 --- a/app/metrics.go +++ b/app/metrics.go @@ -16,6 +16,8 @@ package app import ( + "runtime/debug" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -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", @@ -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 } diff --git a/cmd/version.go b/cmd/version.go index 862b85993..f054570ad 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" + "github.com/obolnetwork/charon/app" "github.com/obolnetwork/charon/app/version" ) @@ -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