mirror of
https://github.com/minio/minio.git
synced 2025-11-24 19:46:16 -05:00
Add TTFB to all APIs and enable for responses without body (#20479)
Add TTFB for all requests in metrics-v3 in addition to the existing GetObject. Also for the requests that do not return a body in the response, calculate TTFB as the HTTP status code and the headers are sent.
This commit is contained in:
committed by
Harshavardhana
parent
f6f0807c86
commit
2b0156b1fc
@@ -41,8 +41,10 @@ type ResponseRecorder struct {
|
||||
// Log body of all responses
|
||||
LogAllBody bool
|
||||
|
||||
TimeToFirstByte time.Duration
|
||||
StartTime time.Time
|
||||
ttfbHeader time.Duration
|
||||
ttfbBody time.Duration
|
||||
|
||||
StartTime time.Time
|
||||
// number of bytes written
|
||||
bytesWritten int
|
||||
// number of bytes of response headers written
|
||||
@@ -63,6 +65,15 @@ func (lrw *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return hj.Hijack()
|
||||
}
|
||||
|
||||
// TTFB of the request - this function needs to be called
|
||||
// when the request is finished to provide accurate data
|
||||
func (lrw *ResponseRecorder) TTFB() time.Duration {
|
||||
if lrw.ttfbBody != 0 {
|
||||
return lrw.ttfbBody
|
||||
}
|
||||
return lrw.ttfbHeader
|
||||
}
|
||||
|
||||
// NewResponseRecorder - returns a wrapped response writer to trap
|
||||
// http status codes for auditing purposes.
|
||||
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
|
||||
@@ -97,8 +108,8 @@ func (lrw *ResponseRecorder) Write(p []byte) (int, error) {
|
||||
}
|
||||
n, err := lrw.ResponseWriter.Write(p)
|
||||
lrw.bytesWritten += n
|
||||
if lrw.TimeToFirstByte == 0 {
|
||||
lrw.TimeToFirstByte = time.Now().UTC().Sub(lrw.StartTime)
|
||||
if lrw.ttfbBody == 0 {
|
||||
lrw.ttfbBody = time.Now().UTC().Sub(lrw.StartTime)
|
||||
}
|
||||
|
||||
if (lrw.LogErrBody && lrw.StatusCode >= http.StatusBadRequest) || lrw.LogAllBody {
|
||||
@@ -159,6 +170,7 @@ func (lrw *ResponseRecorder) Body() []byte {
|
||||
// WriteHeader - writes http status code
|
||||
func (lrw *ResponseRecorder) WriteHeader(code int) {
|
||||
if !lrw.headersLogged {
|
||||
lrw.ttfbHeader = time.Now().UTC().Sub(lrw.StartTime)
|
||||
lrw.StatusCode = code
|
||||
lrw.writeHeaders(&lrw.headers, code, lrw.ResponseWriter.Header())
|
||||
lrw.headersLogged = true
|
||||
|
||||
Reference in New Issue
Block a user