fix(prometheus): report subsonic error code (#4282)
* fix(prometheus): report subsonic error code * address feedback
This commit is contained in:
@@ -226,21 +226,35 @@ func playerIDCookieName(userName string) string {
|
||||
return cookieName
|
||||
}
|
||||
|
||||
const subsonicErrorPointer = "subsonicErrorPointer"
|
||||
|
||||
func recordStats(metrics metrics.Metrics) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
|
||||
status := int32(-1)
|
||||
contextWithStatus := context.WithValue(r.Context(), subsonicErrorPointer, &status)
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
|
||||
// We want to get the client name (even if not present for certain endpoints)
|
||||
p := req.Params(r)
|
||||
client, _ := p.String("c")
|
||||
|
||||
metrics.RecordRequest(r.Context(), strings.Replace(r.URL.Path, ".view", "", 1), r.Method, client, ww.Status(), time.Since(start).Milliseconds())
|
||||
// If there is no Subsonic status (e.g., HTTP 501 not implemented), fallback to HTTP
|
||||
if status == -1 {
|
||||
status = int32(ww.Status())
|
||||
}
|
||||
|
||||
shortPath := strings.Replace(r.URL.Path, ".view", "", 1)
|
||||
|
||||
metrics.RecordRequest(r.Context(), shortPath, r.Method, client, status, elapsed)
|
||||
}()
|
||||
|
||||
next.ServeHTTP(ww, r)
|
||||
next.ServeHTTP(ww, r.WithContext(contextWithStatus))
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user