mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-21 19:20:39 +00:00
metrics: remove VictoriaMetrics/metrics lib (#8766)
# Background Erigon currently uses a combination of Victoria Metrics and Prometheus client for providing metrics. We want to rationalize this and use only the Prometheus client library, but we want to maintain the simplified Victoria Metrics methods for constructing metrics. This task is currently partly complete and needs to be finished to a stage where we can remove the Victoria Metrics module from the Erigon code base. # Summary of changes - Remove `UsePrometheusClient` boolean flag - Remove `VictoriaMetrics` client lib and related code (simplifies registry and prometheus http handler initialisation since now we have only 1 registry and can use default `promhttp.Handler`)
This commit is contained in:
parent
fdc75df6b5
commit
28fff1b35e
@ -12,7 +12,6 @@ require (
|
||||
|
||||
require (
|
||||
github.com/RoaringBitmap/roaring v1.2.3
|
||||
github.com/VictoriaMetrics/metrics v1.23.1
|
||||
github.com/anacrolix/dht/v2 v2.20.0
|
||||
github.com/anacrolix/go-libutp v1.3.1
|
||||
github.com/anacrolix/log v0.14.3-0.20230823030427-4b296d71a6b4
|
||||
@ -37,7 +36,6 @@ require (
|
||||
github.com/pelletier/go-toml/v2 v2.1.0
|
||||
github.com/prometheus/client_golang v1.17.0
|
||||
github.com/prometheus/client_model v0.5.0
|
||||
github.com/prometheus/common v0.44.0
|
||||
github.com/quasilyte/go-ruleguard/dsl v0.3.22
|
||||
github.com/spaolacci/murmur3 v1.1.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
@ -129,12 +127,11 @@ require (
|
||||
github.com/pion/webrtc/v3 v3.1.42 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/common v0.44.0 // indirect
|
||||
github.com/prometheus/procfs v0.11.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/valyala/fastrand v1.1.0 // indirect
|
||||
github.com/valyala/histogram v1.2.0 // indirect
|
||||
go.etcd.io/bbolt v1.3.6 // indirect
|
||||
go.opentelemetry.io/otel v1.8.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.8.0 // indirect
|
||||
@ -151,5 +148,3 @@ require (
|
||||
rsc.io/tmplfunc v0.0.3 // indirect
|
||||
zombiezen.com/go/sqlite v0.13.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/VictoriaMetrics/metrics => github.com/ledgerwatch/victoria-metrics v0.0.7
|
||||
|
@ -299,8 +299,6 @@ github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZ
|
||||
github.com/ledgerwatch/log/v3 v3.9.0/go.mod h1:EiAY6upmI/6LkNhOVxb4eVsmsP11HZCnZ3PlJMjYiqE=
|
||||
github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=
|
||||
github.com/ledgerwatch/secp256k1 v1.0.0/go.mod h1:SPmqJFciiF/Q0mPt2jVs2dTr/1TZBTIA+kPMmKgBAak=
|
||||
github.com/ledgerwatch/victoria-metrics v0.0.7 h1:jW7v1oQ64HcR3rhPfC9vLQhyRHAkKE9tFNOer6gDHvg=
|
||||
github.com/ledgerwatch/victoria-metrics v0.0.7/go.mod h1:QVs9/9u6IewQcgSwsmzW+fQqqpid5XvN//X6gt9h504=
|
||||
github.com/matryer/moq v0.3.3 h1:pScMH9VyrdT4S93yiLpVyU8rCDqGQr24uOyBxmktG5Q=
|
||||
github.com/matryer/moq v0.3.3/go.mod h1:RJ75ZZZD71hejp39j4crZLsEDszGk6iH4v4YsWFKH4s=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
@ -462,10 +460,6 @@ github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EU
|
||||
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
||||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
||||
github.com/willf/bitset v1.1.9/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
|
||||
github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
|
@ -1,123 +0,0 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/VictoriaMetrics/metrics"
|
||||
)
|
||||
|
||||
var (
|
||||
typeGaugeTpl = "\n# TYPE %s gauge\n"
|
||||
typeCounterTpl = "\n# TYPE %s counter\n"
|
||||
typeSummaryTpl = "\n# TYPE %s summary\n"
|
||||
keyValueTpl = "%s %v\n"
|
||||
keyCounterTpl = "%s %v\n"
|
||||
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n"
|
||||
keyQuantileTagValueWithLabelsTpl = "%s,quantile=\"%s\"} %v\n"
|
||||
)
|
||||
|
||||
// collector is a collection of byte buffers that aggregate Prometheus reports
|
||||
// for different metric types.
|
||||
type collector struct {
|
||||
buff *bytes.Buffer
|
||||
}
|
||||
|
||||
// newCollector creates a new Prometheus metric aggregator.
|
||||
func newCollector() *collector {
|
||||
return &collector{
|
||||
buff: &bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *collector) writeFloatCounter(name string, m *metrics.FloatCounter, withType bool) {
|
||||
c.writeCounter(name, m.Get(), withType)
|
||||
}
|
||||
|
||||
func (c *collector) writeHistogram(name string, m *metrics.Histogram, withType bool) {
|
||||
if withType {
|
||||
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, stripLabels(name)))
|
||||
}
|
||||
|
||||
c.writeSummarySum(name, fmt.Sprintf("%f", m.GetSum()))
|
||||
c.writeSummaryCounter(name, len(m.GetDecimalBuckets()))
|
||||
}
|
||||
|
||||
func (c *collector) writeTimer(name string, m *metrics.Summary, withType bool) {
|
||||
pv := m.GetQuantiles()
|
||||
ps := m.GetQuantileValues()
|
||||
|
||||
var sum float64 = 0
|
||||
if withType {
|
||||
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, stripLabels(name)))
|
||||
}
|
||||
|
||||
for i := range pv {
|
||||
c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i])
|
||||
sum += ps[i]
|
||||
}
|
||||
|
||||
c.writeSummaryTime(name, fmt.Sprintf("%f", m.GetTime().Seconds()))
|
||||
c.writeSummarySum(name, fmt.Sprintf("%f", sum))
|
||||
c.writeSummaryCounter(name, len(ps))
|
||||
}
|
||||
|
||||
func (c *collector) writeGauge(name string, value interface{}, withType bool) {
|
||||
if withType {
|
||||
c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, stripLabels(name)))
|
||||
}
|
||||
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
|
||||
}
|
||||
|
||||
func (c *collector) writeCounter(name string, value interface{}, withType bool) {
|
||||
if withType {
|
||||
c.buff.WriteString(fmt.Sprintf(typeCounterTpl, stripLabels(name)))
|
||||
}
|
||||
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
|
||||
}
|
||||
|
||||
func stripLabels(name string) string {
|
||||
if labelsIndex := strings.IndexByte(name, '{'); labelsIndex >= 0 {
|
||||
return name[0:labelsIndex]
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func splitLabels(name string) (string, string) {
|
||||
if labelsIndex := strings.IndexByte(name, '{'); labelsIndex >= 0 {
|
||||
return name[0:labelsIndex], name[labelsIndex:]
|
||||
}
|
||||
|
||||
return name, ""
|
||||
}
|
||||
|
||||
func (c *collector) writeSummaryCounter(name string, value interface{}) {
|
||||
name, labels := splitLabels(name)
|
||||
name = name + "_count"
|
||||
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name+labels, value))
|
||||
}
|
||||
|
||||
func (c *collector) writeSummaryPercentile(name, p string, value interface{}) {
|
||||
name, labels := splitLabels(name)
|
||||
|
||||
if len(labels) > 0 {
|
||||
c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueWithLabelsTpl, name+strings.TrimSuffix(labels, "}"), p, value))
|
||||
} else {
|
||||
c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *collector) writeSummarySum(name string, value string) {
|
||||
name, labels := splitLabels(name)
|
||||
name = name + "_sum"
|
||||
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name+labels, value))
|
||||
}
|
||||
|
||||
func (c *collector) writeSummaryTime(name string, value string) {
|
||||
name, labels := splitLabels(name)
|
||||
name = name + "_time"
|
||||
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name+labels, value))
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
metrics2 "github.com/VictoriaMetrics/metrics"
|
||||
"github.com/ledgerwatch/log/v3"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
)
|
||||
|
||||
// Handler returns an HTTP handler which dump metrics in Prometheus format.
|
||||
// Output format can be cheched here: https://o11y.tools/metricslint/
|
||||
func Handler(reg Registry) http.Handler {
|
||||
prometheus.DefaultRegisterer.MustRegister(defaultSet)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Gather and pre-sort the metrics to avoid random listings
|
||||
var names []string
|
||||
reg.Each(func(name string, i interface{}) {
|
||||
names = append(names, name)
|
||||
})
|
||||
sort.Strings(names)
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
metrics2.WritePrometheus(w, false)
|
||||
|
||||
contentType := expfmt.Negotiate(r.Header)
|
||||
enc := expfmt.NewEncoder(w, contentType)
|
||||
mf, err := prometheus.DefaultGatherer.Gather()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, m := range mf {
|
||||
enc.Encode(m)
|
||||
}
|
||||
|
||||
// Aggregate all the metris into a Prometheus collector
|
||||
c := newCollector()
|
||||
c.buff.WriteRune('\n')
|
||||
|
||||
var typeName string
|
||||
var prevTypeName string
|
||||
|
||||
for _, name := range names {
|
||||
i := reg.Get(name)
|
||||
|
||||
typeName = stripLabels(name)
|
||||
|
||||
switch m := i.(type) {
|
||||
case *metrics2.Counter:
|
||||
if m.IsGauge() {
|
||||
c.writeGauge(name, m.Get(), typeName != prevTypeName)
|
||||
} else {
|
||||
c.writeCounter(name, m.Get(), typeName != prevTypeName)
|
||||
}
|
||||
case *metrics2.Gauge:
|
||||
c.writeGauge(name, m, typeName != prevTypeName)
|
||||
case *metrics2.FloatCounter:
|
||||
c.writeFloatCounter(name, m, typeName != prevTypeName)
|
||||
case *metrics2.Histogram:
|
||||
c.writeHistogram(name, m, typeName != prevTypeName)
|
||||
case *metrics2.Summary:
|
||||
c.writeTimer(name, m, typeName != prevTypeName)
|
||||
default:
|
||||
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
|
||||
}
|
||||
|
||||
prevTypeName = typeName
|
||||
}
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.Header().Add("Content-Length", strconv.Itoa(c.buff.Len()))
|
||||
w.Write(c.buff.Bytes())
|
||||
})
|
||||
}
|
@ -4,13 +4,10 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
vm "github.com/VictoriaMetrics/metrics"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
)
|
||||
|
||||
const UsePrometheusClient = true
|
||||
|
||||
type Histogram interface {
|
||||
// UpdateDuration updates request duration based on the given startTime.
|
||||
UpdateDuration(time.Time)
|
||||
@ -63,17 +60,13 @@ func (c intCounter) Get() uint64 {
|
||||
// - foo{bar="baz",aaa="b"}
|
||||
//
|
||||
// The returned counter is safe to use from concurrent goroutines.
|
||||
func NewCounter(s string) Counter {
|
||||
if UsePrometheusClient {
|
||||
counter, err := defaultSet.NewGauge(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new counter: %w", err))
|
||||
}
|
||||
|
||||
return intCounter{counter}
|
||||
} else {
|
||||
return vm.GetDefaultSet().NewCounter(s)
|
||||
func NewCounter(name string) Counter {
|
||||
counter, err := defaultSet.NewGauge(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new counter: %w", err))
|
||||
}
|
||||
|
||||
return intCounter{counter}
|
||||
}
|
||||
|
||||
// GetOrCreateCounter returns registered counter with the given name
|
||||
@ -90,26 +83,13 @@ func NewCounter(s string) Counter {
|
||||
// The returned counter is safe to use from concurrent goroutines.
|
||||
//
|
||||
// Performance tip: prefer NewCounter instead of GetOrCreateCounter.
|
||||
func GetOrCreateCounter(s string, isGauge ...bool) Counter {
|
||||
if UsePrometheusClient {
|
||||
counter, err := defaultSet.GetOrCreateGauge(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new counter: %w", err))
|
||||
}
|
||||
|
||||
return intCounter{counter}
|
||||
} else {
|
||||
if counter := DefaultRegistry.Get(s); counter != nil {
|
||||
if counter, ok := counter.(Counter); ok {
|
||||
return counter
|
||||
}
|
||||
}
|
||||
|
||||
counter := vm.GetOrCreateCounter(s, isGauge...)
|
||||
DefaultRegistry.Register(s, counter)
|
||||
vm.GetDefaultSet().UnregisterMetric(s)
|
||||
return counter
|
||||
func GetOrCreateCounter(name string, isGauge ...bool) Counter {
|
||||
counter, err := defaultSet.GetOrCreateGauge(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new counter: %w", err))
|
||||
}
|
||||
|
||||
return intCounter{counter}
|
||||
}
|
||||
|
||||
// NewGaugeFunc registers and returns gauge with the given name, which calls f
|
||||
@ -125,8 +105,8 @@ func GetOrCreateCounter(s string, isGauge ...bool) Counter {
|
||||
// f must be safe for concurrent calls.
|
||||
//
|
||||
// The returned gauge is safe to use from concurrent goroutines.
|
||||
func NewGaugeFunc(s string, f func() float64) prometheus.GaugeFunc {
|
||||
gf, err := defaultSet.NewGaugeFunc(s, f)
|
||||
func NewGaugeFunc(name string, f func() float64) prometheus.GaugeFunc {
|
||||
gf, err := defaultSet.NewGaugeFunc(name, f)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new gauge func: %w", err))
|
||||
}
|
||||
@ -148,8 +128,8 @@ func NewGaugeFunc(s string, f func() float64) prometheus.GaugeFunc {
|
||||
// The returned gauge is safe to use from concurrent goroutines.
|
||||
//
|
||||
// Performance tip: prefer NewGauge instead of GetOrCreateGauge.
|
||||
func GetOrCreateGaugeFunc(s string, f func() float64) prometheus.GaugeFunc {
|
||||
gf, err := defaultSet.GetOrCreateGaugeFunc(s, f)
|
||||
func GetOrCreateGaugeFunc(name string, f func() float64) prometheus.GaugeFunc {
|
||||
gf, err := defaultSet.GetOrCreateGaugeFunc(name, f)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new gauge func: %w", err))
|
||||
}
|
||||
@ -179,20 +159,13 @@ func (sm summary) Update(v float64) {
|
||||
// - foo{bar="baz",aaa="b"}
|
||||
//
|
||||
// The returned summary is safe to use from concurrent goroutines.
|
||||
func NewSummary(s string) Summary {
|
||||
if UsePrometheusClient {
|
||||
s, err := defaultSet.NewSummary(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new summary: %w", err))
|
||||
}
|
||||
|
||||
return summary{s}
|
||||
} else {
|
||||
summary := vm.NewSummary(s)
|
||||
DefaultRegistry.Register(s, summary)
|
||||
vm.GetDefaultSet().UnregisterMetric(s)
|
||||
return summary
|
||||
func NewSummary(name string) Summary {
|
||||
s, err := defaultSet.NewSummary(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new summary: %w", err))
|
||||
}
|
||||
|
||||
return summary{s}
|
||||
}
|
||||
|
||||
// GetOrCreateSummary returns registered summary with the given name
|
||||
@ -209,20 +182,13 @@ func NewSummary(s string) Summary {
|
||||
// The returned summary is safe to use from concurrent goroutines.
|
||||
//
|
||||
// Performance tip: prefer NewSummary instead of GetOrCreateSummary.
|
||||
func GetOrCreateSummary(s string) Summary {
|
||||
if UsePrometheusClient {
|
||||
s, err := defaultSet.GetOrCreateSummary(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new summary: %w", err))
|
||||
}
|
||||
|
||||
return summary{s}
|
||||
} else {
|
||||
summary := vm.GetOrCreateSummary(s)
|
||||
DefaultRegistry.Register(s, summary)
|
||||
vm.GetDefaultSet().UnregisterMetric(s)
|
||||
return summary
|
||||
func GetOrCreateSummary(name string) Summary {
|
||||
s, err := defaultSet.GetOrCreateSummary(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new summary: %w", err))
|
||||
}
|
||||
|
||||
return summary{s}
|
||||
}
|
||||
|
||||
type histogram struct {
|
||||
@ -247,17 +213,13 @@ func (h histogram) Update(v float64) {
|
||||
// - foo{bar="baz",aaa="b"}
|
||||
//
|
||||
// The returned histogram is safe to use from concurrent goroutines.
|
||||
func NewHistogram(s string) Histogram {
|
||||
if UsePrometheusClient {
|
||||
h, err := defaultSet.NewHistogram(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new histogram: %w", err))
|
||||
}
|
||||
|
||||
return histogram{h}
|
||||
} else {
|
||||
return vm.NewHistogram(s)
|
||||
func NewHistogram(name string) Histogram {
|
||||
h, err := defaultSet.NewHistogram(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not create new histogram: %w", err))
|
||||
}
|
||||
|
||||
return histogram{h}
|
||||
}
|
||||
|
||||
// GetOrCreateHistogram returns registered histogram with the given name
|
||||
@ -274,18 +236,11 @@ func NewHistogram(s string) Histogram {
|
||||
// The returned histogram is safe to use from concurrent goroutines.
|
||||
//
|
||||
// Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.
|
||||
func GetOrCreateHistogram(s string) Histogram {
|
||||
if UsePrometheusClient {
|
||||
h, err := defaultSet.GetOrCreateHistogram(s)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new histogram: %w", err))
|
||||
}
|
||||
|
||||
return histogram{h}
|
||||
} else {
|
||||
summary := vm.GetOrCreateHistogram(s)
|
||||
DefaultRegistry.Register(s, summary)
|
||||
vm.GetDefaultSet().UnregisterMetric(s)
|
||||
return summary
|
||||
func GetOrCreateHistogram(name string) Histogram {
|
||||
h, err := defaultSet.GetOrCreateHistogram(name)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get or create new histogram: %w", err))
|
||||
}
|
||||
|
||||
return histogram{h}
|
||||
}
|
||||
|
@ -1,163 +0,0 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
metrics2 "github.com/VictoriaMetrics/metrics"
|
||||
"github.com/ledgerwatch/log/v3"
|
||||
)
|
||||
|
||||
// DuplicateMetric is the error returned by Registry.Register when a metric
|
||||
// already exists. If you mean to Register that metric you must first
|
||||
// Unregister the existing metric.
|
||||
type DuplicateMetric string
|
||||
|
||||
func (err DuplicateMetric) Error() string {
|
||||
return fmt.Sprintf("duplicate metric: %s", string(err))
|
||||
}
|
||||
|
||||
// A Registry holds references to a set of metrics by name and can iterate
|
||||
// over them, calling callback functions provided by the user.
|
||||
//
|
||||
// This is an interface so as to encourage other structs to implement
|
||||
// the Registry API as appropriate.
|
||||
type Registry interface {
|
||||
|
||||
// Call the given function for each registered metric.
|
||||
Each(func(string, interface{}))
|
||||
|
||||
// Get the metric by the given name or nil if none is registered.
|
||||
Get(string) interface{}
|
||||
|
||||
// Gets an existing metric or registers the given one.
|
||||
// The interface can be the metric to register if not found in registry,
|
||||
// or a function returning the metric for lazy instantiation.
|
||||
GetOrRegister(string, interface{}) interface{}
|
||||
|
||||
// Register the given metric under the given name.
|
||||
Register(string, interface{}) error
|
||||
|
||||
// Unregister the metric with the given name.
|
||||
Unregister(string)
|
||||
|
||||
// Unregister all metrics. (Mostly for testing.)
|
||||
UnregisterAll()
|
||||
}
|
||||
|
||||
// The standard implementation of a Registry is a mutex-protected map
|
||||
// of names to metrics.
|
||||
type StandardRegistry struct {
|
||||
metrics map[string]interface{}
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// Create a new registry.
|
||||
func NewRegistry() Registry {
|
||||
return &StandardRegistry{metrics: make(map[string]interface{})}
|
||||
}
|
||||
|
||||
// Call the given function for each registered metric.
|
||||
func (r *StandardRegistry) Each(f func(string, interface{})) {
|
||||
for name, i := range r.registered() {
|
||||
f(name, i)
|
||||
}
|
||||
}
|
||||
|
||||
// Get the metric by the given name or nil if none is registered.
|
||||
func (r *StandardRegistry) Get(name string) interface{} {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
return r.metrics[name]
|
||||
}
|
||||
|
||||
// Gets an existing metric or creates and registers a new one. Threadsafe
|
||||
// alternative to calling Get and Register on failure.
|
||||
// The interface can be the metric to register if not found in registry,
|
||||
// or a function returning the metric for lazy instantiation.
|
||||
func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
if metric, ok := r.metrics[name]; ok {
|
||||
return metric
|
||||
}
|
||||
if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
|
||||
i = v.Call(nil)[0].Interface()
|
||||
}
|
||||
r.register(name, i)
|
||||
return i
|
||||
}
|
||||
|
||||
// Register the given metric under the given name. Returns a DuplicateMetric
|
||||
// if a metric by the given name is already registered.
|
||||
func (r *StandardRegistry) Register(name string, i interface{}) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
return r.register(name, i)
|
||||
}
|
||||
|
||||
// Unregister the metric with the given name.
|
||||
func (r *StandardRegistry) Unregister(name string) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
r.stop(name)
|
||||
delete(r.metrics, name)
|
||||
}
|
||||
|
||||
// Unregister all metrics. (Mostly for testing.)
|
||||
func (r *StandardRegistry) UnregisterAll() {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
for name := range r.metrics {
|
||||
r.stop(name)
|
||||
delete(r.metrics, name)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *StandardRegistry) register(name string, i interface{}) error {
|
||||
if _, ok := r.metrics[name]; ok {
|
||||
return DuplicateMetric(name)
|
||||
}
|
||||
switch i.(type) {
|
||||
case *metrics2.Counter, *metrics2.Gauge, *metrics2.FloatCounter, *metrics2.Histogram, *metrics2.Summary:
|
||||
r.metrics[name] = i
|
||||
default:
|
||||
log.Info("Type not registered(metrics won't show): ", reflect.TypeOf(i))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *StandardRegistry) registered() map[string]interface{} {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
metrics := make(map[string]interface{}, len(r.metrics))
|
||||
for name, i := range r.metrics {
|
||||
metrics[name] = i
|
||||
}
|
||||
return metrics
|
||||
}
|
||||
|
||||
func (r *StandardRegistry) stop(name string) {
|
||||
if i, ok := r.metrics[name]; ok {
|
||||
if s, ok := i.(Stoppable); ok {
|
||||
s.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stoppable defines the metrics which has to be stopped.
|
||||
type Stoppable interface {
|
||||
Stop()
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultRegistry = NewRegistry()
|
||||
EphemeralRegistry = NewRegistry()
|
||||
AccountingRegistry = NewRegistry() // registry used in swarm
|
||||
)
|
||||
|
||||
// Get the metric by the given name or nil if none is registered.
|
||||
func Get(name string) interface{} {
|
||||
return DefaultRegistry.Get(name)
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
// Hook go-metrics into expvar
|
||||
// on any /debug/metrics request, load all vars from the registry into expvar, and execute regular expvar handler
|
||||
package metrics
|
||||
|
||||
import (
|
||||
@ -7,6 +5,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ledgerwatch/log/v3"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var EnabledExpensive = false
|
||||
@ -14,9 +14,10 @@ var EnabledExpensive = false
|
||||
// Setup starts a dedicated metrics server at the given address.
|
||||
// This function enables metrics reporting separate from pprof.
|
||||
func Setup(address string, logger log.Logger) *http.ServeMux {
|
||||
prometheusMux := http.NewServeMux()
|
||||
prometheus.DefaultRegisterer.MustRegister(defaultSet)
|
||||
|
||||
prometheusMux.Handle("/debug/metrics/prometheus", Handler(DefaultRegistry))
|
||||
prometheusMux := http.NewServeMux()
|
||||
prometheusMux.Handle("/debug/metrics/prometheus", promhttp.Handler())
|
||||
|
||||
promServer := &http.Server{
|
||||
Addr: address,
|
||||
@ -30,6 +31,5 @@ func Setup(address string, logger log.Logger) *http.ServeMux {
|
||||
}()
|
||||
|
||||
logger.Info("Enabling metrics export to prometheus", "path", fmt.Sprintf("http://%s/debug/metrics/prometheus", address))
|
||||
|
||||
return prometheusMux
|
||||
}
|
5
go.mod
5
go.mod
@ -103,7 +103,6 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/VictoriaMetrics/metrics v1.23.1 // indirect
|
||||
github.com/agnivade/levenshtein v1.1.1 // indirect
|
||||
github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 // indirect
|
||||
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
|
||||
@ -258,8 +257,6 @@ require (
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/supranational/blst v0.3.11 // indirect
|
||||
github.com/valyala/fastrand v1.1.0 // indirect
|
||||
github.com/valyala/histogram v1.2.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
go.etcd.io/bbolt v1.3.6 // indirect
|
||||
go.opentelemetry.io/otel v1.8.0 // indirect
|
||||
@ -287,5 +284,3 @@ require (
|
||||
)
|
||||
|
||||
replace github.com/tendermint/tendermint => github.com/bnb-chain/tendermint v0.31.12
|
||||
|
||||
replace github.com/VictoriaMetrics/metrics => github.com/ledgerwatch/victoria-metrics v0.0.7
|
||||
|
6
go.sum
6
go.sum
@ -545,8 +545,6 @@ github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZ
|
||||
github.com/ledgerwatch/log/v3 v3.9.0/go.mod h1:EiAY6upmI/6LkNhOVxb4eVsmsP11HZCnZ3PlJMjYiqE=
|
||||
github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=
|
||||
github.com/ledgerwatch/secp256k1 v1.0.0/go.mod h1:SPmqJFciiF/Q0mPt2jVs2dTr/1TZBTIA+kPMmKgBAak=
|
||||
github.com/ledgerwatch/victoria-metrics v0.0.7 h1:jW7v1oQ64HcR3rhPfC9vLQhyRHAkKE9tFNOer6gDHvg=
|
||||
github.com/ledgerwatch/victoria-metrics v0.0.7/go.mod h1:QVs9/9u6IewQcgSwsmzW+fQqqpid5XvN//X6gt9h504=
|
||||
github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=
|
||||
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
|
||||
github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c=
|
||||
@ -887,10 +885,6 @@ github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
|
||||
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
|
||||
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
||||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
||||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
||||
github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq8cRU=
|
||||
github.com/vektah/gqlparser/v2 v2.5.6/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
|
||||
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
|
||||
|
@ -6,8 +6,4 @@ require github.com/ledgerwatch/erigon $COMMIT_SHA
|
||||
|
||||
replace github.com/ledgerwatch/erigon-lib => github.com/ledgerwatch/erigon/erigon-lib $COMMIT_SHA
|
||||
|
||||
// TODO - remove this temp fix once VictoriaMetrics/metrics is removed from the repo in
|
||||
// https://github.com/ledgerwatch/erigon/pull/8766
|
||||
replace github.com/VictoriaMetrics/metrics => github.com/ledgerwatch/victoria-metrics v0.0.7
|
||||
|
||||
require github.com/ethereum/go-ethereum v1.13.3
|
||||
|
Loading…
Reference in New Issue
Block a user