feat: slow threshold customizable in rest (#1189)

* feat: slow threshold customizable in rest

* feat: slow threshold customizable in rest
master
Kevin Wan 3 years ago committed by GitHub
parent 785d100be9
commit ebc90720ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,6 +16,7 @@ import (
"github.com/tal-tech/go-zero/core/iox" "github.com/tal-tech/go-zero/core/iox"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
"github.com/tal-tech/go-zero/core/utils" "github.com/tal-tech/go-zero/core/utils"
"github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/httpx"
@ -24,9 +25,11 @@ import (
const ( const (
limitBodyBytes = 1024 limitBodyBytes = 1024
slowThreshold = time.Millisecond * 500 defaultSlowThreshold = time.Millisecond * 500
) )
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
type loggedResponseWriter struct { type loggedResponseWriter struct {
w http.ResponseWriter w http.ResponseWriter
r *http.Request r *http.Request
@ -140,6 +143,11 @@ func DetailedLogHandler(next http.Handler) http.Handler {
}) })
} }
// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}
func dumpRequest(r *http.Request) string { func dumpRequest(r *http.Request) string {
reqContent, err := httputil.DumpRequest(r, true) reqContent, err := httputil.DumpRequest(r, true)
if err != nil { if err != nil {
@ -155,7 +163,7 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern
logger := logx.WithContext(r.Context()) logger := logx.WithContext(r.Context())
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s", buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s",
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)))
if duration > slowThreshold { if duration > slowThreshold.Load() {
logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)", logger.Slowf("[HTTP] %s - %d - %s - %s - %s - slowcall(%s)",
r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) r.Method, code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))
} }
@ -191,7 +199,7 @@ func logDetails(r *http.Request, response *detailLoggedResponseWriter, timer *ut
logger := logx.WithContext(r.Context()) logger := logx.WithContext(r.Context())
buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n", buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))) r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)))
if duration > slowThreshold { if duration > defaultSlowThreshold {
logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n", logger.Slowf("[HTTP] %s - %d - %s - slowcall(%s)\n=> %s\n",
r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r)) r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
} }

@ -53,7 +53,7 @@ func TestLogHandlerSlow(t *testing.T) {
for _, logHandler := range handlers { for _, logHandler := range handlers {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(slowThreshold + time.Millisecond*50) time.Sleep(defaultSlowThreshold + time.Millisecond*50)
})) }))
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
@ -100,6 +100,12 @@ func TestDetailedLogHandler_Hijack(t *testing.T) {
}) })
} }
func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
SetSlowThreshold(time.Second)
assert.Equal(t, time.Second, slowThreshold.Load())
}
func BenchmarkLogHandler(b *testing.B) { func BenchmarkLogHandler(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()

Loading…
Cancel
Save