Skip to content

Commit

Permalink
WIP: V2 metrics middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKitsune committed Apr 18, 2024
1 parent 7de7c64 commit d0eaa61
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/api/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ import (
"net/http"
)

func MetricsV2(rec metrics.Recorder, routeTemplate string) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// Don't want metric requests messing with our actual metrics
if r.URL.Path == "/metrics" {
next.ServeHTTP(w, r)
return
}

// wrap original http.ResponseWriter
rwd := responseWriterDecorator{
ResponseWriter: w,
StatusCode: 0,
}

// Record request duration with the path
rec.ReportRequestDuration(routeTemplate, func() {
next.ServeHTTP(&rwd, r)
})

if isServerErrorCode(rwd.StatusCode) {
go rec.CountServerError()
}
})
}
}

func Metrics(rec metrics.Recorder) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit d0eaa61

Please sign in to comment.