add gitCommit, gitDate and uptime to build_info metric

This commit is contained in:
barryz 2021-07-23 15:11:53 +08:00
parent d12f5ffd74
commit 8b2a0558ea
5 changed files with 36 additions and 4 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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())
}

View file

@ -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)

View file

@ -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]