-52
cmd/collectiondir/metrics.go
-52
cmd/collectiondir/metrics.go
···
1
1
package main
2
2
3
3
import (
4
-
"errors"
5
-
"github.com/labstack/echo/v4"
6
4
"github.com/prometheus/client_golang/prometheus"
7
5
"github.com/prometheus/client_golang/prometheus/promauto"
8
-
"net/http"
9
-
"strconv"
10
-
"time"
11
6
)
12
7
13
8
var firehoseReceivedCounter = promauto.NewCounter(prometheus.CounterOpts{
···
52
47
Help: "how long it takes to calculate total stats",
53
48
Buckets: prometheus.ExponentialBuckets(0.01, 2, 13),
54
49
})
55
-
56
-
var reqDur = promauto.NewHistogramVec(prometheus.HistogramOpts{
57
-
Name: "http_request_duration_seconds",
58
-
Help: "A histogram of latencies for requests.",
59
-
Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
60
-
}, []string{"code", "method", "path"})
61
-
62
-
var reqCnt = promauto.NewCounterVec(prometheus.CounterOpts{
63
-
Name: "http_requests_total",
64
-
Help: "A counter for requests to the wrapped handler.",
65
-
}, []string{"code", "method", "path"})
66
-
67
-
// MetricsMiddleware defines handler function for metrics middleware
68
-
// TODO: reunify with bgs/metrics.go ?
69
-
func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
70
-
return func(c echo.Context) error {
71
-
path := c.Path()
72
-
if path == "/metrics" || path == "/_health" {
73
-
return next(c)
74
-
}
75
-
76
-
start := time.Now()
77
-
78
-
err := next(c)
79
-
80
-
status := c.Response().Status
81
-
if err != nil {
82
-
var httpError *echo.HTTPError
83
-
if errors.As(err, &httpError) {
84
-
status = httpError.Code
85
-
}
86
-
if status == 0 || status == http.StatusOK {
87
-
status = http.StatusInternalServerError
88
-
}
89
-
}
90
-
91
-
elapsed := float64(time.Since(start)) / float64(time.Second)
92
-
93
-
statusStr := strconv.Itoa(status)
94
-
method := c.Request().Method
95
-
96
-
reqDur.WithLabelValues(statusStr, method, path).Observe(elapsed)
97
-
reqCnt.WithLabelValues(statusStr, method, path).Inc()
98
-
99
-
return err
100
-
}
101
-
}
+2
-1
cmd/collectiondir/serve.go
+2
-1
cmd/collectiondir/serve.go
···
25
25
26
26
comatproto "github.com/bluesky-social/indigo/api/atproto"
27
27
"github.com/bluesky-social/indigo/events"
28
+
"github.com/bluesky-social/indigo/util/svcutil"
28
29
"github.com/bluesky-social/indigo/xrpc"
29
30
30
31
"github.com/labstack/echo/v4"
···
405
406
e := echo.New()
406
407
e.HideBanner = true
407
408
408
-
e.Use(MetricsMiddleware)
409
+
e.Use(svcutil.MetricsMiddleware)
409
410
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
410
411
AllowOrigins: []string{"*"},
411
412
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization},