feat(metrics): wire instrumentation through queue, handlers, middleware
This commit is contained in:
@@ -7,10 +7,12 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shcizo/package-updater/internal/logging"
|
||||
"github.com/shcizo/package-updater/internal/metrics"
|
||||
)
|
||||
|
||||
// Auth returns middleware that requires a matching bearer token.
|
||||
@@ -66,8 +68,9 @@ func RequestID(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// RequestLogger emits a structured access-log line per request.
|
||||
func RequestLogger(base *slog.Logger) func(http.Handler) http.Handler {
|
||||
// RequestLogger emits a structured access-log line per request. m may be nil,
|
||||
// in which case metrics recording is silently skipped.
|
||||
func RequestLogger(base *slog.Logger, m *metrics.Metrics) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
@@ -81,6 +84,9 @@ func RequestLogger(base *slog.Logger) func(http.Handler) http.Handler {
|
||||
"duration_ms", time.Since(start).Milliseconds(),
|
||||
"client_ip", clientIP(r),
|
||||
)
|
||||
if m != nil {
|
||||
m.HTTPRequests.WithLabelValues(r.URL.Path, strconv.Itoa(sw.status)).Inc()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user