Debug HTTPLoggingMiddleware latency.
This commit is contained in:

committed by
Sebastian Serth

parent
317590d3ea
commit
ae86b1c261
@ -2,13 +2,16 @@ package logging
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/coreos/go-systemd/v22/daemon"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/openHPI/poseidon/pkg/dto"
|
"github.com/openHPI/poseidon/pkg/dto"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -82,6 +85,12 @@ func HTTPLoggingMiddleware(next http.Handler) http.Handler {
|
|||||||
start := time.Now().UTC()
|
start := time.Now().UTC()
|
||||||
path := RemoveNewlineSymbol(r.URL.Path)
|
path := RemoveNewlineSymbol(r.URL.Path)
|
||||||
|
|
||||||
|
if path == "/api/v1/health" {
|
||||||
|
ctx, cancel := context.WithCancel(r.Context())
|
||||||
|
defer cancel()
|
||||||
|
go debugGoroutines(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
lrw := NewLoggingResponseWriter(w)
|
lrw := NewLoggingResponseWriter(w)
|
||||||
next.ServeHTTP(lrw, r)
|
next.ServeHTTP(lrw, r)
|
||||||
|
|
||||||
@ -107,3 +116,24 @@ func RemoveNewlineSymbol(data string) string {
|
|||||||
data = strings.ReplaceAll(data, "\n", "")
|
data = strings.ReplaceAll(data, "\n", "")
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// debugGoroutines temporarily debugs a behavior where we observe long latencies in the Health route.
|
||||||
|
func debugGoroutines(ctx context.Context) {
|
||||||
|
interval, err := daemon.SdWatchdogEnabled(false)
|
||||||
|
if err != nil || interval == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Trace("Starting timeout for debugging the Goroutines")
|
||||||
|
|
||||||
|
const notificationIntervalFactor = 3
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(interval / notificationIntervalFactor):
|
||||||
|
log.Warn("Health route latency is too high")
|
||||||
|
err := pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Warn("Failed to log the goroutines")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user