erigon-pulse/metrics/exp/exp.go
Mark Holt 051cad0fdd
Devnet diagnostics (#7762)
Added support tunnel to the devnet cmd. In order to get this to run I
made the following changes:

* Create a public function 
* Added non root logging

I have also added commentary to the readme to explain the additional
command line arguments needed to integrate with diagnostics. In summary,
if you set the --diagnostics.url the devenet will wait for diagnostic
requests rather than exiting

---------

Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
2023-06-20 10:11:55 +01:00

42 lines
1.4 KiB
Go

// Hook go-metrics into expvar
// on any /debug/metrics request, load all vars from the registry into expvar, and execute regular expvar handler
package exp
import (
"fmt"
"net/http"
metrics2 "github.com/VictoriaMetrics/metrics"
"github.com/ledgerwatch/log/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
// 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.HandleFunc("/debug/metrics/prometheus", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
metrics2.WritePrometheus(w, true)
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)
}
})
//m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry))
//http.Handle("/debug/metrics/prometheus2", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
logger.Info("Starting metrics server", "addr",
fmt.Sprintf("http://%s/debug/metrics/prometheus", address),
)
go func() {
if err := http.ListenAndServe(address, nil); err != nil { // nolint:gosec
logger.Error("Failure in running metrics server", "err", err)
}
}()
}