From 8b2a0558eaa43bc55c177af50790f39cdfb741a9 Mon Sep 17 00:00:00 2001 From: barryz Date: Fri, 23 Jul 2021 15:11:53 +0800 Subject: [PATCH] add gitCommit, gitDate and uptime to build_info metric --- build/ci.go | 6 ++++++ metrics/exp/exp.go | 5 +++-- metrics/prometheus/collector.go | 9 +++++++++ metrics/prometheus/prometheus.go | 5 ++++- params/version.go | 15 ++++++++++++++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/build/ci.go b/build/ci.go index 63dd606321..5a9e6a2d0a 100644 --- a/build/ci.go +++ b/build/ci.go @@ -64,6 +64,10 @@ import ( "github.com/ethereum/go-ethereum/params" ) +const ( + PackagePrefix = "github.com/ethereum/go-ethereum" +) + var ( // Files that end up in the geth*.zip archive. gethArchiveFiles = []string{ @@ -259,6 +263,8 @@ func buildFlags(env build.Environment) (flags []string) { if env.Commit != "" { ld = append(ld, "-X", "main.gitCommit="+env.Commit) ld = append(ld, "-X", "main.gitDate="+env.Date) + ld = append(ld, "-X", fmt.Sprintf("%s/params.gitCommit=%s", PackagePrefix, env.Commit)) + ld = append(ld, "-X", fmt.Sprintf("%s/params.gitDate=%s", PackagePrefix, env.Date)) } // Strip DWARF on darwin. This used to be required for certain things, // and there is no downside to this, so we just keep doing it. diff --git a/metrics/exp/exp.go b/metrics/exp/exp.go index 3ebe8cc68a..d42d52aa09 100644 --- a/metrics/exp/exp.go +++ b/metrics/exp/exp.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "sync" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -44,7 +45,7 @@ func Exp(r metrics.Registry) { // http.HandleFunc("/debug/vars", e.expHandler) // haven't found an elegant way, so just use a different endpoint http.Handle("/debug/metrics", h) - http.Handle("/debug/metrics/prometheus", prometheus.Handler(r)) + http.Handle("/debug/metrics/prometheus", prometheus.Handler(r, time.Now())) } // ExpHandler will return an expvar powered metrics handler. @@ -58,7 +59,7 @@ func ExpHandler(r metrics.Registry) http.Handler { func Setup(address string) { m := http.NewServeMux() m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry)) - m.Handle("/debug/metrics/prometheus", prometheus.Handler(metrics.DefaultRegistry)) + m.Handle("/debug/metrics/prometheus", prometheus.Handler(metrics.DefaultRegistry, time.Now())) log.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", address)) go func() { if err := http.ListenAndServe(address, m); err != nil { diff --git a/metrics/prometheus/collector.go b/metrics/prometheus/collector.go index 3959cbf5e1..ca78081d1d 100644 --- a/metrics/prometheus/collector.go +++ b/metrics/prometheus/collector.go @@ -21,8 +21,10 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/params" ) var ( @@ -46,6 +48,13 @@ func newCollector() *collector { } } +// addBuildInfo produce `geth_build_info {version=$version} $uptime_seconds` prometheus format metric +func (c *collector) addBuildInfo(start time.Time) { + buildInfo := "geth_build_info" + c.buff.WriteString(fmt.Sprintf("#TYPE %s gauge\n", buildInfo)) + c.buff.WriteString(fmt.Sprintf("%s {version=\"%s\"} %v\n", buildInfo, params.VersionWithCommit("", ""), time.Since(start).Seconds())) +} + func (c *collector) addCounter(name string, m metrics.Counter) { c.writeGaugeCounter(name, m.Count()) } diff --git a/metrics/prometheus/prometheus.go b/metrics/prometheus/prometheus.go index 9ad5ec7e99..114e9b8ea1 100644 --- a/metrics/prometheus/prometheus.go +++ b/metrics/prometheus/prometheus.go @@ -21,13 +21,15 @@ import ( "fmt" "net/http" "sort" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" ) // Handler returns an HTTP handler which dump metrics in Prometheus format. -func Handler(reg metrics.Registry) http.Handler { +func Handler(reg metrics.Registry, begin time.Time) http.Handler { + start := begin return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Gather and pre-sort the metrics to avoid random listings var names []string @@ -39,6 +41,7 @@ func Handler(reg metrics.Registry) http.Handler { // Aggregate all the metris into a Prometheus collector c := newCollector() + c.addBuildInfo(start) for _, name := range names { i := reg.Get(name) diff --git a/params/version.go b/params/version.go index 9ac205ee96..f12a9796b9 100644 --- a/params/version.go +++ b/params/version.go @@ -27,6 +27,12 @@ const ( VersionMeta = "unstable-debank" // Version metadata to append to the version string ) +var ( + // Git SHA1 commit hash of the release (set via linker flags) + gitCommit string + gitDate string +) + // Version holds the textual version string. var Version = func() string { return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) @@ -55,7 +61,14 @@ func ArchiveVersion(gitCommit string) string { return vsn } -func VersionWithCommit(gitCommit, gitDate string) string { +func VersionWithCommit(commit, date string) string { + if commit == "" { + commit = gitCommit + } + if date == "" { + date = gitDate + } + vsn := VersionWithMeta if len(gitCommit) >= 8 { vsn += "-" + gitCommit[:8]