feat: add prometheus metrics

This commit is contained in:
Sam 2023-04-17 23:44:21 +02:00
parent b4c331daa0
commit 5c8c6eed63
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
7 changed files with 110 additions and 9 deletions

View file

@ -107,12 +107,17 @@ func New() (*Server, error) {
rateLimiter.Scope("*", "/auth/invites", 10)
rateLimiter.Scope("POST", "/auth/discord/*", 10)
// rate limit handling
// - 120 req/minute (2/s)
// - keyed by Authorization header if valid token is provided, otherwise by IP
// - returns rate limit reset info in error
s.Router.Use(rateLimiter.Handler())
// increment the total requests counter whenever a request is made
s.Router.Use(func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
s.DB.TotalRequests.Inc()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
})
// return an API error for not found + method not allowed
s.Router.NotFound(func(w http.ResponseWriter, r *http.Request) {
render.Status(r, errCodeStatuses[ErrNotFound])