create a /metrics endpoint for Prometheus

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-10-09 17:04:16 +02:00 committed by Jakub
parent d87caf57e3
commit 4fe317917e
2 changed files with 42 additions and 7 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/okzk/sdnotify"
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/metrics"
nodemetrics "github.com/status-im/status-go/metrics/node"
"github.com/status-im/status-go/node"
"github.com/status-im/status-go/params"
@ -56,7 +57,8 @@ var (
)
// don't change the name of this flag, https://github.com/ethereum/go-ethereum/blob/master/metrics/metrics.go#L41
metrics = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call")
metricsEnabled = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call")
metricsPort = flag.Int("metrics-port", 9305, "Port for the Prometheus /metrics endpoint")
syncAndExit = flag.Int("sync-and-exit", -1, "Timeout in minutes for blockchain sync and exit, zero means no timeout unless sync is finished")
)
@ -148,17 +150,18 @@ func main() {
// handle interrupt signals
interruptCh := haltOnInterruptSignal(backend.StatusNode())
// Check if profiling shall be enabled.
if *pprofEnabled {
profiling.NewProfiler(*pprofPort).Go()
}
// Start collecting metrics. Metrics can be enabled by providing `-metrics` flag
// or setting `gethmetrics.Enabled` to true during compilation time:
// https://github.com/status-im/go-ethereum/pull/76.
if *metrics || gethmetrics.Enabled {
if *metricsEnabled || gethmetrics.Enabled {
go startCollectingNodeMetrics(interruptCh, backend.StatusNode())
go gethmetrics.CollectProcessMetrics(3 * time.Second)
go metrics.NewMetricsServer(*metricsPort, gethmetrics.DefaultRegistry).Listen()
}
// Check if profiling shall be enabled.
if *pprofEnabled {
profiling.NewProfiler(*pprofPort).Go()
}
// Sync blockchain and stop.

32
metrics/metrics.go Normal file
View File

@ -0,0 +1,32 @@
package metrics
import (
"fmt"
"net/http"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics/prometheus"
)
// Server runs and controls a HTTP pprof interface.
type Server struct {
server *http.Server
}
func NewMetricsServer(port int, r metrics.Registry) *Server {
mux := http.NewServeMux()
mux.Handle("/metrics", prometheus.Handler(r))
p := Server{
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
},
}
return &p
}
// Listen starts the HTTP server in the background.
func (p *Server) Listen() {
log.Info("metrics server stopped", "err", p.server.ListenAndServe())
}