mirror of
https://github.com/ferdzo/fs.git
synced 2026-04-05 08:46:24 +00:00
Initial metrics endpoint added in Prometheus style
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fs/metrics"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
@@ -86,6 +88,8 @@ func HTTPMiddleware(logger *slog.Logger, cfg Config) func(http.Handler) http.Han
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
metrics.Default.IncHTTPInFlight()
|
||||
defer metrics.Default.DecHTTPInFlight()
|
||||
requestID := middleware.GetReqID(r.Context())
|
||||
if requestID != "" {
|
||||
ww.Header().Set("x-amz-request-id", requestID)
|
||||
@@ -93,15 +97,18 @@ func HTTPMiddleware(logger *slog.Logger, cfg Config) func(http.Handler) http.Han
|
||||
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
if !cfg.Audit && !cfg.DebugMode {
|
||||
return
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
status := ww.Status()
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
route := metricRouteLabel(r)
|
||||
metrics.Default.ObserveHTTPRequest(r.Method, route, status, elapsed, ww.BytesWritten())
|
||||
|
||||
if !cfg.Audit && !cfg.DebugMode {
|
||||
return
|
||||
}
|
||||
|
||||
attrs := []any{
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
@@ -131,6 +138,35 @@ func HTTPMiddleware(logger *slog.Logger, cfg Config) func(http.Handler) http.Han
|
||||
}
|
||||
}
|
||||
|
||||
func metricRouteLabel(r *http.Request) string {
|
||||
if r == nil || r.URL == nil {
|
||||
return "/unknown"
|
||||
}
|
||||
|
||||
if routeCtx := chi.RouteContext(r.Context()); routeCtx != nil {
|
||||
if pattern := strings.TrimSpace(routeCtx.RoutePattern()); pattern != "" {
|
||||
return pattern
|
||||
}
|
||||
}
|
||||
|
||||
path := strings.TrimSpace(r.URL.Path)
|
||||
if path == "" || path == "/" {
|
||||
return "/"
|
||||
}
|
||||
if path == "/healthz" || path == "/metrics" {
|
||||
return path
|
||||
}
|
||||
|
||||
trimmed := strings.Trim(path, "/")
|
||||
if trimmed == "" {
|
||||
return "/"
|
||||
}
|
||||
if !strings.Contains(trimmed, "/") {
|
||||
return "/{bucket}"
|
||||
}
|
||||
return "/{bucket}/*"
|
||||
}
|
||||
|
||||
func envBool(key string, defaultValue bool) bool {
|
||||
raw := os.Getenv(key)
|
||||
if raw == "" {
|
||||
|
||||
Reference in New Issue
Block a user