refactor: enhancement of logging, config and metrics

This commit is contained in:
Jonas Kaninda
2024-11-11 08:50:34 +01:00
parent e25bc218b5
commit 11c72e5e17
12 changed files with 97 additions and 65 deletions

View File

@@ -35,7 +35,7 @@ var totalRequests = prometheus.NewCounterVec(
Name: "http_requests_total",
Help: "Number of get requests.",
},
[]string{"path"},
[]string{"name", "path"},
)
var responseStatus = prometheus.NewCounterVec(
@@ -49,16 +49,19 @@ var responseStatus = prometheus.NewCounterVec(
var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_response_time_seconds",
Help: "Duration of HTTP requests.",
}, []string{"path"})
}, []string{"name", "path"})
func prometheusMiddleware(next http.Handler) http.Handler {
func (pr PrometheusRoute) prometheusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path))
path := pr.path
if len(path) == 0 {
route := mux.CurrentRoute(r)
path, _ = route.GetPathTemplate()
}
timer := prometheus.NewTimer(httpDuration.WithLabelValues(pr.name, path))
responseStatus.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
totalRequests.WithLabelValues(path).Inc()
totalRequests.WithLabelValues(pr.name, path).Inc()
timer.ObserveDuration()
next.ServeHTTP(w, r)