mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-13 23:43:47 +00:00
add gitCommit, gitDate and uptime to build_info metric
This commit is contained in:
parent
d12f5ffd74
commit
8b2a0558ea
5 changed files with 36 additions and 4 deletions
|
|
@ -64,6 +64,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PackagePrefix = "github.com/ethereum/go-ethereum"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Files that end up in the geth*.zip archive.
|
// Files that end up in the geth*.zip archive.
|
||||||
gethArchiveFiles = []string{
|
gethArchiveFiles = []string{
|
||||||
|
|
@ -259,6 +263,8 @@ func buildFlags(env build.Environment) (flags []string) {
|
||||||
if env.Commit != "" {
|
if env.Commit != "" {
|
||||||
ld = append(ld, "-X", "main.gitCommit="+env.Commit)
|
ld = append(ld, "-X", "main.gitCommit="+env.Commit)
|
||||||
ld = append(ld, "-X", "main.gitDate="+env.Date)
|
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,
|
// 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.
|
// and there is no downside to this, so we just keep doing it.
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
|
|
@ -44,7 +45,7 @@ func Exp(r metrics.Registry) {
|
||||||
// http.HandleFunc("/debug/vars", e.expHandler)
|
// http.HandleFunc("/debug/vars", e.expHandler)
|
||||||
// haven't found an elegant way, so just use a different endpoint
|
// haven't found an elegant way, so just use a different endpoint
|
||||||
http.Handle("/debug/metrics", h)
|
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.
|
// ExpHandler will return an expvar powered metrics handler.
|
||||||
|
|
@ -58,7 +59,7 @@ func ExpHandler(r metrics.Registry) http.Handler {
|
||||||
func Setup(address string) {
|
func Setup(address string) {
|
||||||
m := http.NewServeMux()
|
m := http.NewServeMux()
|
||||||
m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry))
|
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))
|
log.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", address))
|
||||||
go func() {
|
go func() {
|
||||||
if err := http.ListenAndServe(address, m); err != nil {
|
if err := http.ListenAndServe(address, m); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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) {
|
func (c *collector) addCounter(name string, m metrics.Counter) {
|
||||||
c.writeGaugeCounter(name, m.Count())
|
c.writeGaugeCounter(name, m.Count())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handler returns an HTTP handler which dump metrics in Prometheus format.
|
// 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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Gather and pre-sort the metrics to avoid random listings
|
// Gather and pre-sort the metrics to avoid random listings
|
||||||
var names []string
|
var names []string
|
||||||
|
|
@ -39,6 +41,7 @@ func Handler(reg metrics.Registry) http.Handler {
|
||||||
// Aggregate all the metris into a Prometheus collector
|
// Aggregate all the metris into a Prometheus collector
|
||||||
c := newCollector()
|
c := newCollector()
|
||||||
|
|
||||||
|
c.addBuildInfo(start)
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
i := reg.Get(name)
|
i := reg.Get(name)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,12 @@ const (
|
||||||
VersionMeta = "unstable-debank" // Version metadata to append to the version string
|
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.
|
// Version holds the textual version string.
|
||||||
var Version = func() string {
|
var Version = func() string {
|
||||||
return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
|
return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
|
||||||
|
|
@ -55,7 +61,14 @@ func ArchiveVersion(gitCommit string) string {
|
||||||
return vsn
|
return vsn
|
||||||
}
|
}
|
||||||
|
|
||||||
func VersionWithCommit(gitCommit, gitDate string) string {
|
func VersionWithCommit(commit, date string) string {
|
||||||
|
if commit == "" {
|
||||||
|
commit = gitCommit
|
||||||
|
}
|
||||||
|
if date == "" {
|
||||||
|
date = gitDate
|
||||||
|
}
|
||||||
|
|
||||||
vsn := VersionWithMeta
|
vsn := VersionWithMeta
|
||||||
if len(gitCommit) >= 8 {
|
if len(gitCommit) >= 8 {
|
||||||
vsn += "-" + gitCommit[:8]
|
vsn += "-" + gitCommit[:8]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue