package observability import ( "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) // HTTPMetrics contains metric collectors used by HTTP middleware. type HTTPMetrics struct { RequestsTotal *prometheus.CounterVec RequestDuration *prometheus.HistogramVec InFlight prometheus.Gauge } func NewRegistry() *prometheus.Registry { registry := prometheus.NewRegistry() registry.MustRegister( collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), ) return registry } func NewHTTPMetrics(registerer prometheus.Registerer) *HTTPMetrics { factory := promauto.With(registerer) return &HTTPMetrics{ RequestsTotal: factory.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests by method, route, and status.", }, []string{"method", "route", "status"}, ), RequestDuration: factory.NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "HTTP request duration by method, route, and status.", Buckets: prometheus.DefBuckets, }, []string{"method", "route", "status"}, ), InFlight: factory.NewGauge( prometheus.GaugeOpts{ Name: "http_in_flight_requests", Help: "Current number of in-flight HTTP requests.", }, ), } } func NewPrometheusHandler(registry *prometheus.Registry) http.Handler { return promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) }