Files

36 lines
1.3 KiB
Go

package public
import (
"net/http"
"time"
sresponse "git.sendico.io/tech/sxchnge/product/auth/http/v1/response"
"github.com/go-chi/chi/v5/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var flowRequests = promauto.NewCounterVec(prometheus.CounterOpts{Name: "sxchnge_auth_flow_requests_total", Help: "Authentication flow outcomes; success means this HTTP step completed, not email delivery."}, []string{"operation", "outcome"})
var flowDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{Name: "sxchnge_auth_flow_duration_seconds", Help: "Authentication HTTP step latency."}, []string{"operation"})
func measured(operation string, handler sresponse.HandlerFunc) sresponse.HandlerFunc {
return func(r *http.Request) http.HandlerFunc {
start := time.Now()
response := handler(r)
return func(w http.ResponseWriter, r *http.Request) {
wrapped := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
response(wrapped, r)
outcome := "accepted"
if wrapped.Status() >= 500 {
outcome = "unavailable"
} else if wrapped.Status() == 429 {
outcome = "limited"
} else if wrapped.Status() >= 400 {
outcome = "rejected"
}
flowRequests.WithLabelValues(operation, outcome).Inc()
flowDuration.WithLabelValues(operation).Observe(time.Since(start).Seconds())
}
}
}