mirror of
https://github.com/minio/minio.git
synced 2025-01-11 15:03:22 -05:00
simplify recordAPIStats wrapper for ResponseWriters (#9034)
This commit is contained in:
parent
4c92bec619
commit
ece0d4ac53
@ -538,9 +538,8 @@ func setHTTPStatsHandler(h http.Handler) http.Handler {
|
|||||||
func (h httpStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h httpStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
isS3Request := !strings.HasPrefix(r.URL.Path, minioReservedBucketPath)
|
isS3Request := !strings.HasPrefix(r.URL.Path, minioReservedBucketPath)
|
||||||
// record s3 connection stats.
|
// record s3 connection stats.
|
||||||
recordRequest := &recordTrafficRequest{ReadCloser: r.Body, isS3Request: isS3Request}
|
r.Body = &recordTrafficRequest{ReadCloser: r.Body, isS3Request: isS3Request}
|
||||||
r.Body = recordRequest
|
recordResponse := &recordTrafficResponse{ResponseWriter: w, isS3Request: isS3Request}
|
||||||
recordResponse := &recordTrafficResponse{w, isS3Request}
|
|
||||||
// Execute the request
|
// Execute the request
|
||||||
h.handler.ServeHTTP(recordResponse, r)
|
h.handler.ServeHTTP(recordResponse, r)
|
||||||
}
|
}
|
||||||
|
@ -362,14 +362,16 @@ func collectAPIStats(api string, f http.HandlerFunc) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
isS3Request := !strings.HasPrefix(r.URL.Path, minioReservedBucketPath)
|
isS3Request := !strings.HasPrefix(r.URL.Path, minioReservedBucketPath)
|
||||||
apiStatsWriter := &recordAPIStats{w, UTCNow(), false, 0, isS3Request}
|
|
||||||
|
|
||||||
// Time start before the call is about to start.
|
// Time start before the call is about to start.
|
||||||
tBefore := UTCNow()
|
tBefore := UTCNow()
|
||||||
|
|
||||||
|
apiStatsWriter := &recordAPIStats{ResponseWriter: w, TTFB: tBefore, isS3Request: isS3Request}
|
||||||
|
|
||||||
if isS3Request {
|
if isS3Request {
|
||||||
globalHTTPStats.currentS3Requests.Inc(api)
|
globalHTTPStats.currentS3Requests.Inc(api)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the request
|
// Execute the request
|
||||||
f.ServeHTTP(apiStatsWriter, r)
|
f.ServeHTTP(apiStatsWriter, r)
|
||||||
|
|
||||||
|
@ -41,23 +41,13 @@ func (r *recordTrafficRequest) Read(p []byte) (n int, err error) {
|
|||||||
// Records the outgoing bytes through the responseWriter.
|
// Records the outgoing bytes through the responseWriter.
|
||||||
type recordTrafficResponse struct {
|
type recordTrafficResponse struct {
|
||||||
// wrapper for underlying http.ResponseWriter.
|
// wrapper for underlying http.ResponseWriter.
|
||||||
writer http.ResponseWriter
|
http.ResponseWriter
|
||||||
isS3Request bool
|
isS3Request bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls the underlying WriteHeader.
|
|
||||||
func (r *recordTrafficResponse) WriteHeader(i int) {
|
|
||||||
r.writer.WriteHeader(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calls the underlying Header.
|
|
||||||
func (r *recordTrafficResponse) Header() http.Header {
|
|
||||||
return r.writer.Header()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Records the output bytes
|
// Records the output bytes
|
||||||
func (r *recordTrafficResponse) Write(p []byte) (n int, err error) {
|
func (r *recordTrafficResponse) Write(p []byte) (n int, err error) {
|
||||||
n, err = r.writer.Write(p)
|
n, err = r.ResponseWriter.Write(p)
|
||||||
globalConnStats.incOutputBytes(n)
|
globalConnStats.incOutputBytes(n)
|
||||||
// Check if it is s3 request
|
// Check if it is s3 request
|
||||||
if r.isS3Request {
|
if r.isS3Request {
|
||||||
@ -68,13 +58,12 @@ func (r *recordTrafficResponse) Write(p []byte) (n int, err error) {
|
|||||||
|
|
||||||
// Calls the underlying Flush.
|
// Calls the underlying Flush.
|
||||||
func (r *recordTrafficResponse) Flush() {
|
func (r *recordTrafficResponse) Flush() {
|
||||||
r.writer.(http.Flusher).Flush()
|
r.ResponseWriter.(http.Flusher).Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Records the outgoing bytes through the responseWriter.
|
// Records the outgoing bytes through the responseWriter.
|
||||||
type recordAPIStats struct {
|
type recordAPIStats struct {
|
||||||
// wrapper for underlying http.ResponseWriter.
|
http.ResponseWriter
|
||||||
writer http.ResponseWriter
|
|
||||||
TTFB time.Time // TimeToFirstByte.
|
TTFB time.Time // TimeToFirstByte.
|
||||||
firstByteRead bool
|
firstByteRead bool
|
||||||
respStatusCode int
|
respStatusCode int
|
||||||
@ -84,12 +73,7 @@ type recordAPIStats struct {
|
|||||||
// Calls the underlying WriteHeader.
|
// Calls the underlying WriteHeader.
|
||||||
func (r *recordAPIStats) WriteHeader(i int) {
|
func (r *recordAPIStats) WriteHeader(i int) {
|
||||||
r.respStatusCode = i
|
r.respStatusCode = i
|
||||||
r.writer.WriteHeader(i)
|
r.ResponseWriter.WriteHeader(i)
|
||||||
}
|
|
||||||
|
|
||||||
// Calls the underlying Header.
|
|
||||||
func (r *recordAPIStats) Header() http.Header {
|
|
||||||
return r.writer.Header()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Records the TTFB on the first byte write.
|
// Records the TTFB on the first byte write.
|
||||||
@ -98,10 +82,10 @@ func (r *recordAPIStats) Write(p []byte) (n int, err error) {
|
|||||||
r.TTFB = UTCNow()
|
r.TTFB = UTCNow()
|
||||||
r.firstByteRead = true
|
r.firstByteRead = true
|
||||||
}
|
}
|
||||||
return r.writer.Write(p)
|
return r.ResponseWriter.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls the underlying Flush.
|
// Calls the underlying Flush.
|
||||||
func (r *recordAPIStats) Flush() {
|
func (r *recordAPIStats) Flush() {
|
||||||
r.writer.(http.Flusher).Flush()
|
r.ResponseWriter.(http.Flusher).Flush()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user