+2
-1
bgs/bgs.go
+2
-1
bgs/bgs.go
···
24
24
"github.com/bluesky-social/indigo/indexer"
25
25
"github.com/bluesky-social/indigo/models"
26
26
"github.com/bluesky-social/indigo/repomgr"
27
+
"github.com/bluesky-social/indigo/util/svcutil"
27
28
"github.com/bluesky-social/indigo/xrpc"
28
29
lru "github.com/hashicorp/golang-lru/v2"
29
30
"golang.org/x/sync/semaphore"
···
237
238
e.File("/dash/*", "public/index.html")
238
239
e.Static("/assets", "public/assets")
239
240
240
-
e.Use(MetricsMiddleware)
241
+
e.Use(svcutil.MetricsMiddleware)
241
242
242
243
e.HTTPErrorHandler = func(err error, ctx echo.Context) {
243
244
switch err := err.(type) {
-93
bgs/metrics.go
-93
bgs/metrics.go
···
1
1
package bgs
2
2
3
3
import (
4
-
"errors"
5
-
"net/http"
6
-
"strconv"
7
-
"time"
8
-
9
-
"github.com/labstack/echo/v4"
10
4
"github.com/prometheus/client_golang/prometheus"
11
5
"github.com/prometheus/client_golang/prometheus/promauto"
12
6
)
···
68
62
Help: "The total number of new users discovered directly from the firehose (not from refs)",
69
63
})
70
64
71
-
var reqSz = promauto.NewHistogramVec(prometheus.HistogramOpts{
72
-
Name: "http_request_size_bytes",
73
-
Help: "A histogram of request sizes for requests.",
74
-
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
75
-
}, []string{"code", "method", "path"})
76
-
77
-
var reqDur = promauto.NewHistogramVec(prometheus.HistogramOpts{
78
-
Name: "http_request_duration_seconds",
79
-
Help: "A histogram of latencies for requests.",
80
-
Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
81
-
}, []string{"code", "method", "path"})
82
-
83
-
var reqCnt = promauto.NewCounterVec(prometheus.CounterOpts{
84
-
Name: "http_requests_total",
85
-
Help: "A counter for requests to the wrapped handler.",
86
-
}, []string{"code", "method", "path"})
87
-
88
-
var resSz = promauto.NewHistogramVec(prometheus.HistogramOpts{
89
-
Name: "http_response_size_bytes",
90
-
Help: "A histogram of response sizes for requests.",
91
-
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
92
-
}, []string{"code", "method", "path"})
93
-
94
65
var userLookupDuration = promauto.NewHistogram(prometheus.HistogramOpts{
95
66
Name: "relay_user_lookup_duration",
96
67
Help: "A histogram of user lookup latencies",
···
102
73
Help: "A histogram of new user discovery latencies",
103
74
Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
104
75
})
105
-
106
-
// MetricsMiddleware defines handler function for metrics middleware
107
-
func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
108
-
return func(c echo.Context) error {
109
-
path := c.Path()
110
-
if path == "/metrics" || path == "/_health" {
111
-
return next(c)
112
-
}
113
-
114
-
start := time.Now()
115
-
requestSize := computeApproximateRequestSize(c.Request())
116
-
117
-
err := next(c)
118
-
119
-
status := c.Response().Status
120
-
if err != nil {
121
-
var httpError *echo.HTTPError
122
-
if errors.As(err, &httpError) {
123
-
status = httpError.Code
124
-
}
125
-
if status == 0 || status == http.StatusOK {
126
-
status = http.StatusInternalServerError
127
-
}
128
-
}
129
-
130
-
elapsed := float64(time.Since(start)) / float64(time.Second)
131
-
132
-
statusStr := strconv.Itoa(status)
133
-
method := c.Request().Method
134
-
135
-
responseSize := float64(c.Response().Size)
136
-
137
-
reqDur.WithLabelValues(statusStr, method, path).Observe(elapsed)
138
-
reqCnt.WithLabelValues(statusStr, method, path).Inc()
139
-
reqSz.WithLabelValues(statusStr, method, path).Observe(float64(requestSize))
140
-
resSz.WithLabelValues(statusStr, method, path).Observe(responseSize)
141
-
142
-
return err
143
-
}
144
-
}
145
-
146
-
func computeApproximateRequestSize(r *http.Request) int {
147
-
s := 0
148
-
if r.URL != nil {
149
-
s = len(r.URL.Path)
150
-
}
151
-
152
-
s += len(r.Method)
153
-
s += len(r.Proto)
154
-
for name, values := range r.Header {
155
-
s += len(name)
156
-
for _, value := range values {
157
-
s += len(value)
158
-
}
159
-
}
160
-
s += len(r.Host)
161
-
162
-
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
163
-
164
-
if r.ContentLength != -1 {
165
-
s += int(r.ContentLength)
166
-
}
167
-
return s
168
-
}
+2
-2
splitter/splitter.go
+2
-2
splitter/splitter.go
···
21
21
22
22
"github.com/bluesky-social/indigo/api/atproto"
23
23
comatproto "github.com/bluesky-social/indigo/api/atproto"
24
-
"github.com/bluesky-social/indigo/bgs"
25
24
"github.com/bluesky-social/indigo/events"
26
25
"github.com/bluesky-social/indigo/events/pebblepersist"
27
26
"github.com/bluesky-social/indigo/events/schedulers/sequential"
28
27
"github.com/bluesky-social/indigo/util"
28
+
"github.com/bluesky-social/indigo/util/svcutil"
29
29
"github.com/bluesky-social/indigo/xrpc"
30
30
"github.com/gorilla/websocket"
31
31
"github.com/labstack/echo/v4"
···
211
211
}
212
212
*/
213
213
214
-
e.Use(bgs.MetricsMiddleware)
214
+
e.Use(svcutil.MetricsMiddleware)
215
215
216
216
e.HTTPErrorHandler = func(err error, ctx echo.Context) {
217
217
switch err := err.(type) {