From e4d3eeec62fba1e7f30d6bd3bae95f4c4a13ed14 Mon Sep 17 00:00:00 2001 From: tamirms <tamirms@gmail.com> Date: Thu, 29 Feb 2024 20:47:15 +0000 Subject: [PATCH 1/2] Add requests in flight metric --- services/horizon/internal/httpx/middleware.go | 14 +++++++++++--- services/horizon/internal/httpx/server.go | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/services/horizon/internal/httpx/middleware.go b/services/horizon/internal/httpx/middleware.go index cdcd7f4e3c..60969f9194 100644 --- a/services/horizon/internal/httpx/middleware.go +++ b/services/horizon/internal/httpx/middleware.go @@ -78,11 +78,20 @@ func loggerMiddleware(serverMetrics *ServerMetrics) func(next http.Handler) http // is reset before sending the first event no Content-Type header is sent in a response. acceptHeader := r.Header.Get("Accept") streaming := strings.Contains(acceptHeader, render.MimeEventStream) + route := supportHttp.GetChiRoutePattern(r) + + inFlightLabels := prometheus.Labels{ + "route": route, + "streaming": strconv.FormatBool(streaming), + "method": r.Method, + } + serverMetrics.RequestsInFlightGauge.With(inFlightLabels).Inc() + defer serverMetrics.RequestsInFlightGauge.With(inFlightLabels).Dec() then := time.Now() next.ServeHTTP(mw, r.WithContext(ctx)) duration := time.Since(then) - logEndOfRequest(ctx, r, serverMetrics.RequestDurationSummary, duration, mw, streaming) + logEndOfRequest(ctx, r, route, serverMetrics.RequestDurationSummary, duration, mw, streaming) }) } } @@ -129,8 +138,7 @@ func getClientData(r *http.Request, headerName string) string { return value } -func logEndOfRequest(ctx context.Context, r *http.Request, requestDurationSummary *prometheus.SummaryVec, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) { - route := supportHttp.GetChiRoutePattern(r) +func logEndOfRequest(ctx context.Context, r *http.Request, route string, requestDurationSummary *prometheus.SummaryVec, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) { referer := r.Referer() if referer == "" { diff --git a/services/horizon/internal/httpx/server.go b/services/horizon/internal/httpx/server.go index 65058c0ea5..b4cbd6d087 100644 --- a/services/horizon/internal/httpx/server.go +++ b/services/horizon/internal/httpx/server.go @@ -23,6 +23,7 @@ import ( type ServerMetrics struct { RequestDurationSummary *prometheus.SummaryVec ReplicaLagErrorsCounter prometheus.Counter + RequestsInFlightGauge *prometheus.GaugeVec } type TLSConfig struct { @@ -71,6 +72,13 @@ func NewServer(serverConfig ServerConfig, routerConfig RouterConfig, ledgerState }, []string{"status", "route", "streaming", "method"}, ), + RequestsInFlightGauge: prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "horizon", Subsystem: "http", Name: "requests_in_flight", + Help: "HTTP requests in flight", + }, + []string{"route", "streaming", "method"}, + ), ReplicaLagErrorsCounter: prometheus.NewCounter( prometheus.CounterOpts{ Namespace: "horizon", Subsystem: "http", Name: "replica_lag_errors_count", @@ -109,6 +117,7 @@ func NewServer(serverConfig ServerConfig, routerConfig RouterConfig, ledgerState func (s *Server) RegisterMetrics(registry *prometheus.Registry) { registry.MustRegister(s.Metrics.RequestDurationSummary) registry.MustRegister(s.Metrics.ReplicaLagErrorsCounter) + registry.MustRegister(s.Metrics.RequestsInFlightGauge) } func (s *Server) Serve() error { From 8f92fc566be030e1eac6cb485cf65a7481dd2c6d Mon Sep 17 00:00:00 2001 From: tamirms <tamirms@gmail.com> Date: Fri, 1 Mar 2024 10:34:53 +0000 Subject: [PATCH 2/2] add requests received metric --- services/horizon/internal/httpx/middleware.go | 7 ++++--- services/horizon/internal/httpx/server.go | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/services/horizon/internal/httpx/middleware.go b/services/horizon/internal/httpx/middleware.go index 60969f9194..eaa49634a5 100644 --- a/services/horizon/internal/httpx/middleware.go +++ b/services/horizon/internal/httpx/middleware.go @@ -80,13 +80,14 @@ func loggerMiddleware(serverMetrics *ServerMetrics) func(next http.Handler) http streaming := strings.Contains(acceptHeader, render.MimeEventStream) route := supportHttp.GetChiRoutePattern(r) - inFlightLabels := prometheus.Labels{ + requestLabels := prometheus.Labels{ "route": route, "streaming": strconv.FormatBool(streaming), "method": r.Method, } - serverMetrics.RequestsInFlightGauge.With(inFlightLabels).Inc() - defer serverMetrics.RequestsInFlightGauge.With(inFlightLabels).Dec() + serverMetrics.RequestsInFlightGauge.With(requestLabels).Inc() + defer serverMetrics.RequestsInFlightGauge.With(requestLabels).Dec() + serverMetrics.RequestsReceivedCounter.With(requestLabels).Inc() then := time.Now() next.ServeHTTP(mw, r.WithContext(ctx)) diff --git a/services/horizon/internal/httpx/server.go b/services/horizon/internal/httpx/server.go index b4cbd6d087..262d51578d 100644 --- a/services/horizon/internal/httpx/server.go +++ b/services/horizon/internal/httpx/server.go @@ -24,6 +24,7 @@ type ServerMetrics struct { RequestDurationSummary *prometheus.SummaryVec ReplicaLagErrorsCounter prometheus.Counter RequestsInFlightGauge *prometheus.GaugeVec + RequestsReceivedCounter *prometheus.CounterVec } type TLSConfig struct { @@ -79,6 +80,12 @@ func NewServer(serverConfig ServerConfig, routerConfig RouterConfig, ledgerState }, []string{"route", "streaming", "method"}, ), + RequestsReceivedCounter: prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "horizon", Subsystem: "http", Name: "requests_received", + }, + []string{"route", "streaming", "method"}, + ), ReplicaLagErrorsCounter: prometheus.NewCounter( prometheus.CounterOpts{ Namespace: "horizon", Subsystem: "http", Name: "replica_lag_errors_count", @@ -118,6 +125,7 @@ func (s *Server) RegisterMetrics(registry *prometheus.Registry) { registry.MustRegister(s.Metrics.RequestDurationSummary) registry.MustRegister(s.Metrics.ReplicaLagErrorsCounter) registry.MustRegister(s.Metrics.RequestsInFlightGauge) + registry.MustRegister(s.Metrics.RequestsReceivedCounter) } func (s *Server) Serve() error {