Commit e70e762b authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

Merge pull request #1660 from mslipper/feature/proxy-method-metrics

Add proxy method metrics
parents e7159d5d 8cc824e5
---
'@eth-optimism/proxyd': minor
---
Updates proxyd to include additional error metrics.
This diff is collapsed.
package proxyd package proxyd
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/cors" "github.com/rs/cors"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time"
) )
var ( var (
...@@ -22,17 +21,11 @@ var ( ...@@ -22,17 +21,11 @@ var (
Help: "Count of total HTTP requests.", Help: "Count of total HTTP requests.",
}) })
httpRequestDurationHisto = promauto.NewHistogram(prometheus.HistogramOpts{ httpRequestDurationSummary = promauto.NewSummary(prometheus.SummaryOpts{
Namespace: "proxyd", Namespace: "proxyd",
Name: "http_request_duration_histogram_seconds", Name: "http_request_duration_seconds",
Help: "Histogram of HTTP request durations.", Help: "Summary of HTTP request durations, in seconds.",
Buckets: []float64{ Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001},
0,
0.1,
0.25,
0.75,
1,
},
}) })
rpcRequestsCtr = promauto.NewCounterVec(prometheus.CounterOpts{ rpcRequestsCtr = promauto.NewCounterVec(prometheus.CounterOpts{
...@@ -96,8 +89,8 @@ func (s *Server) ListenAndServe(host string, port int) error { ...@@ -96,8 +89,8 @@ func (s *Server) ListenAndServe(host string, port int) error {
hdlr.HandleFunc("/healthz", s.HandleHealthz).Methods("GET") hdlr.HandleFunc("/healthz", s.HandleHealthz).Methods("GET")
hdlr.HandleFunc("/", s.HandleRPC).Methods("POST") hdlr.HandleFunc("/", s.HandleRPC).Methods("POST")
c := cors.New(cors.Options{ c := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, AllowedOrigins: []string{"*"},
}) })
addr := fmt.Sprintf("%s:%d", host, port) addr := fmt.Sprintf("%s:%d", host, port)
server := &http.Server{ server := &http.Server{
Handler: instrumentedHdlr(c.Handler(hdlr)), Handler: instrumentedHdlr(c.Handler(hdlr)),
...@@ -142,7 +135,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) { ...@@ -142,7 +135,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
return return
} }
backendRes, err := group.Forward(body) backendRes, err := group.Forward(req)
if err != nil { if err != nil {
log.Error("error forwarding RPC request", "group", group.Name, "method", req.Method, "err", err) log.Error("error forwarding RPC request", "group", group.Name, "method", req.Method, "err", err)
rpcErrorsCtr.WithLabelValues("-32603").Inc() rpcErrorsCtr.WithLabelValues("-32603").Inc()
...@@ -181,9 +174,8 @@ func writeRPCError(w http.ResponseWriter, id *int, code int, msg string) { ...@@ -181,9 +174,8 @@ func writeRPCError(w http.ResponseWriter, id *int, code int, msg string) {
func instrumentedHdlr(h http.Handler) http.HandlerFunc { func instrumentedHdlr(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
httpRequestsCtr.Inc() httpRequestsCtr.Inc()
start := time.Now() timer := prometheus.NewTimer(httpRequestDurationSummary)
defer timer.ObserveDuration()
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
dur := time.Since(start)
httpRequestDurationHisto.Observe(float64(dur) / float64(time.Second))
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment