2023-08-31 08:04:27 +00:00
|
|
|
// 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.
|
2023-09-03 01:09:27 +00:00
|
|
|
package metrics
|
2023-08-31 08:04:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
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.
|
2023-09-14 07:28:57 +00:00
|
|
|
// Output format can be cheched here: https://o11y.tools/metricslint/
|
2023-09-03 01:09:27 +00:00
|
|
|
func Handler(reg Registry) http.Handler {
|
2023-08-31 08:04:27 +00:00
|
|
|
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", "*")
|
2023-09-07 16:48:01 +00:00
|
|
|
|
|
|
|
metrics2.WritePrometheus(w, false)
|
|
|
|
|
2023-08-31 08:04:27 +00:00
|
|
|
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')
|
|
|
|
|
2023-09-14 07:28:57 +00:00
|
|
|
var typeName string
|
|
|
|
var prevTypeName string
|
|
|
|
|
2023-08-31 08:04:27 +00:00
|
|
|
for _, name := range names {
|
|
|
|
i := reg.Get(name)
|
|
|
|
|
2023-09-14 07:28:57 +00:00
|
|
|
typeName = stripLabels(name)
|
|
|
|
|
2023-08-31 08:04:27 +00:00
|
|
|
switch m := i.(type) {
|
|
|
|
case *metrics2.Counter:
|
|
|
|
if m.IsGauge() {
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeGauge(name, m.Get(), typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
} else {
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeCounter(name, m.Get(), typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
}
|
|
|
|
case *metrics2.Gauge:
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeGauge(name, m, typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
case *metrics2.FloatCounter:
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeFloatCounter(name, m, typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
case *metrics2.Histogram:
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeHistogram(name, m, typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
case *metrics2.Summary:
|
2023-09-14 07:28:57 +00:00
|
|
|
c.writeTimer(name, m, typeName != prevTypeName)
|
2023-08-31 08:04:27 +00:00
|
|
|
default:
|
|
|
|
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
|
|
|
|
}
|
2023-09-14 07:28:57 +00:00
|
|
|
|
|
|
|
prevTypeName = typeName
|
2023-08-31 08:04:27 +00:00
|
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
|
|
|
|
w.Write(c.buff.Bytes())
|
|
|
|
})
|
|
|
|
}
|