소스 검색

feat: slow threshold customizable in rest (#1189)

* feat: slow threshold customizable in rest

* feat: slow threshold customizable in rest
Kevin Wan 3 년 전
부모
커밋
ebc90720ea
2개의 변경된 파일19개의 추가작업 그리고 5개의 파일을 삭제
  1. 12 4
      rest/handler/loghandler.go
  2. 7 1
      rest/handler/loghandler_test.go

+ 12 - 4
rest/handler/loghandler.go

@@ -16,6 +16,7 @@ import (
 
 	"github.com/tal-tech/go-zero/core/iox"
 	"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/utils"
 	"github.com/tal-tech/go-zero/rest/httpx"
@@ -23,10 +24,12 @@ import (
 )
 
 const (
-	limitBodyBytes = 1024
-	slowThreshold  = time.Millisecond * 500
+	limitBodyBytes       = 1024
+	defaultSlowThreshold = time.Millisecond * 500
 )
 
+var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
+
 type loggedResponseWriter struct {
 	w    http.ResponseWriter
 	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 {
 	reqContent, err := httputil.DumpRequest(r, true)
 	if err != nil {
@@ -155,7 +163,7 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern
 	logger := logx.WithContext(r.Context())
 	buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s - %s - %s",
 		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)",
 			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())
 	buf.WriteString(fmt.Sprintf("[HTTP] %s - %d - %s - %s\n=> %s\n",
 		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",
 			r.Method, response.writer.code, r.RemoteAddr, timex.ReprOfDuration(duration), dumpRequest(r))
 	}

+ 7 - 1
rest/handler/loghandler_test.go

@@ -53,7 +53,7 @@ func TestLogHandlerSlow(t *testing.T) {
 	for _, logHandler := range handlers {
 		req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
 		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()
@@ -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) {
 	b.ReportAllocs()