mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-18 08:38:46 +00:00
a4cfbe0d56
This is an update of: https://github.com/ledgerwatch/erigon/pull/7846 which uses a local fork of victoria metrics to include the changes that https://github.com/anshalshukla added to the original for we where using. It also includes code to address the duplicate metrics issue identified here: https://github.com/ledgerwatch/erigon/issues/8053 It has one more associated fix which is to correctly add a metadata label to counters, these where previously labelled as gauges. e.g. ``` # TYPE p2p_peers counter p2p_peers 0 ``` rather than ``` # TYPE p2p_peers gauge p2p_peers 0 ``` --------- Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com> Co-authored-by: Anshal Shukla <shukla.anshal85@gmail.com>
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
// Copyright 2019 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package prometheus exposes go-metrics into a Prometheus format.
|
|
package prometheus
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
|
|
metrics2 "github.com/VictoriaMetrics/metrics"
|
|
"github.com/ledgerwatch/erigon/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.
|
|
func Handler(reg metrics.Registry) http.Handler {
|
|
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", "*")
|
|
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')
|
|
|
|
for _, name := range names {
|
|
i := reg.Get(name)
|
|
|
|
switch m := i.(type) {
|
|
case *metrics2.Counter:
|
|
if m.IsGauge() {
|
|
c.writeGauge(name, m.Get())
|
|
} else {
|
|
c.writeCounter(name, m.Get())
|
|
}
|
|
case *metrics2.Gauge:
|
|
c.addGauge(name, m)
|
|
case *metrics2.FloatCounter:
|
|
c.addFloatCounter(name, m)
|
|
case *metrics2.Histogram:
|
|
c.addHistogram(name, m)
|
|
case *metrics2.Summary:
|
|
c.addTimer(name, m)
|
|
default:
|
|
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
|
|
}
|
|
}
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
|
|
w.Write(c.buff.Bytes())
|
|
})
|
|
}
|