-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error count prom metric (#14670)
* add error count prom metric * address review comments * add comment for response writer * update changelog
- Loading branch information
Showing
5 changed files
with
57 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,18 +34,41 @@ type endpoint struct { | |
methods []string | ||
} | ||
|
||
// responseWriter is the wrapper to http Response writer. | ||
type responseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
// WriteHeader wraps the WriteHeader method of the underlying http.ResponseWriter to capture the status code. | ||
// Refer for WriteHeader doc: https://pkg.go.dev/net/[email protected]#ResponseWriter. | ||
func (w *responseWriter) WriteHeader(statusCode int) { | ||
w.statusCode = statusCode | ||
w.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
|
||
func (e *endpoint) handlerWithMiddleware() http.HandlerFunc { | ||
handler := http.Handler(e.handler) | ||
for _, m := range e.middleware { | ||
handler = m(handler) | ||
} | ||
return promhttp.InstrumentHandlerDuration( | ||
|
||
handler = promhttp.InstrumentHandlerDuration( | ||
httpRequestLatency.MustCurryWith(prometheus.Labels{"endpoint": e.name}), | ||
promhttp.InstrumentHandlerCounter( | ||
httpRequestCount.MustCurryWith(prometheus.Labels{"endpoint": e.name}), | ||
handler, | ||
), | ||
) | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} | ||
handler.ServeHTTP(rw, r) | ||
|
||
if rw.statusCode >= 400 { | ||
httpErrorCount.WithLabelValues(r.URL.Path, http.StatusText(rw.statusCode), r.Method).Inc() | ||
} | ||
} | ||
} | ||
|
||
func (s *Service) endpoints( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package rpc | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var ( | ||
httpRequestLatency = promauto.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "http_request_latency_seconds", | ||
Help: "Latency of HTTP requests in seconds", | ||
Buckets: []float64{0.001, 0.01, 0.025, 0.1, 0.25, 1, 2.5, 10}, | ||
}, | ||
[]string{"endpoint", "code", "method"}, | ||
) | ||
httpRequestCount = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_request_count", | ||
Help: "Number of HTTP requests", | ||
}, | ||
[]string{"endpoint", "code", "method"}, | ||
) | ||
httpErrorCount = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_error_count", | ||
Help: "Total HTTP errors for beacon node requests", | ||
}, | ||
[]string{"endpoint", "code", "method"}, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters