Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp/lighthorizon: Add metrics middleware to collect request duration metrics #4486

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions exp/lighthorizon/http.go
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
}
15 changes: 1 addition & 14 deletions exp/lighthorizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"flag"
"net/http"

"github.com/go-chi/chi"
"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/archive"
"github.com/stellar/go/exp/lighthorizon/index"
"github.com/stellar/go/exp/lighthorizon/services"
Expand Down Expand Up @@ -71,14 +67,5 @@ if left empty, uses a temporary directory`)
},
}

router := chi.NewMux()
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{}))

log.Fatal(http.ListenAndServe(":8080", router))
log.Fatal(http.ListenAndServe(":8080", lightHorizonHTTPHandler(registry, lightHorizon)))
}