diff --git a/cmd/collectiondir/metrics.go b/cmd/collectiondir/metrics.go index 3d1af066..993e39af 100644 --- a/cmd/collectiondir/metrics.go +++ b/cmd/collectiondir/metrics.go @@ -1,13 +1,8 @@ package main import ( - "errors" - "github.com/labstack/echo/v4" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "net/http" - "strconv" - "time" ) var firehoseReceivedCounter = promauto.NewCounter(prometheus.CounterOpts{ @@ -52,50 +47,3 @@ var statsCalculations = promauto.NewHistogram(prometheus.HistogramOpts{ Help: "how long it takes to calculate total stats", Buckets: prometheus.ExponentialBuckets(0.01, 2, 13), }) - -var reqDur = promauto.NewHistogramVec(prometheus.HistogramOpts{ - Name: "http_request_duration_seconds", - Help: "A histogram of latencies for requests.", - Buckets: prometheus.ExponentialBuckets(0.001, 2, 15), -}, []string{"code", "method", "path"}) - -var reqCnt = promauto.NewCounterVec(prometheus.CounterOpts{ - Name: "http_requests_total", - Help: "A counter for requests to the wrapped handler.", -}, []string{"code", "method", "path"}) - -// MetricsMiddleware defines handler function for metrics middleware -// TODO: reunify with bgs/metrics.go ? -func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - path := c.Path() - if path == "/metrics" || path == "/_health" { - return next(c) - } - - start := time.Now() - - err := next(c) - - status := c.Response().Status - if err != nil { - var httpError *echo.HTTPError - if errors.As(err, &httpError) { - status = httpError.Code - } - if status == 0 || status == http.StatusOK { - status = http.StatusInternalServerError - } - } - - elapsed := float64(time.Since(start)) / float64(time.Second) - - statusStr := strconv.Itoa(status) - method := c.Request().Method - - reqDur.WithLabelValues(statusStr, method, path).Observe(elapsed) - reqCnt.WithLabelValues(statusStr, method, path).Inc() - - return err - } -} diff --git a/cmd/collectiondir/serve.go b/cmd/collectiondir/serve.go index b8e2c506..fba58441 100644 --- a/cmd/collectiondir/serve.go +++ b/cmd/collectiondir/serve.go @@ -25,6 +25,7 @@ import ( comatproto "github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/events" + "github.com/bluesky-social/indigo/util/svcutil" "github.com/bluesky-social/indigo/xrpc" "github.com/labstack/echo/v4" @@ -405,7 +406,7 @@ func (cs *collectionServer) StartApiServer(ctx context.Context, addr string) err e := echo.New() e.HideBanner = true - e.Use(MetricsMiddleware) + e.Use(svcutil.MetricsMiddleware) e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"*"}, AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization},