-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metrics middleware to collect request duration metrics (#4486)
- Loading branch information
Showing
2 changed files
with
64 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/go-chi/chi" | ||
"github.com/go-chi/chi/middleware" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/stellar/go/exp/lighthorizon/actions" | ||
"github.com/stellar/go/exp/lighthorizon/services" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func newWrapResponseWriter(w http.ResponseWriter, r *http.Request) middleware.WrapResponseWriter { | ||
mw, ok := w.(middleware.WrapResponseWriter) | ||
if !ok { | ||
mw = middleware.NewWrapResponseWriter(w, r.ProtoMajor) | ||
} | ||
|
||
return mw | ||
} | ||
|
||
func prometheusMiddleware(requestDurationMetric *prometheus.SummaryVec) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
mw := newWrapResponseWriter(w, r) | ||
|
||
then := time.Now() | ||
next.ServeHTTP(mw, r) | ||
duration := time.Since(then) | ||
|
||
requestDurationMetric.With(prometheus.Labels{ | ||
"status": strconv.FormatInt(int64(mw.Status()), 10), | ||
"method": r.Method, | ||
}).Observe(float64(duration.Seconds())) | ||
}) | ||
} | ||
} | ||
|
||
func lightHorizonHTTPHandler(registry *prometheus.Registry, lightHorizon services.LightHorizon) http.Handler { | ||
requestDurationMetric := prometheus.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Namespace: "horizon_lite", Subsystem: "http", Name: "requests_duration_seconds", | ||
Help: "HTTP requests durations, sliding window = 10m", | ||
}, | ||
[]string{"status", "method"}, | ||
) | ||
registry.MustRegister(requestDurationMetric) | ||
|
||
router := chi.NewMux() | ||
router.Use(prometheusMiddleware(requestDurationMetric)) | ||
|
||
router.Route("/accounts/{account_id}", func(r chi.Router) { | ||
r.MethodFunc(http.MethodGet, "/transactions", actions.NewTXByAccountHandler(lightHorizon)) | ||
r.MethodFunc(http.MethodGet, "/operations", actions.NewOpsByAccountHandler(lightHorizon)) | ||
}) | ||
|
||
router.MethodFunc(http.MethodGet, "/", actions.ApiDocs()) | ||
router.Method(http.MethodGet, "/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) | ||
|
||
return router | ||
} |
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