fix: regression in counting total requests (#17024)

This commit is contained in:
Harshavardhana
2023-04-12 14:37:19 -07:00
committed by GitHub
parent b19620b324
commit a5835cecbf
8 changed files with 65 additions and 189 deletions

View File

@@ -56,8 +56,8 @@ func (r *RequestRecorder) Read(p []byte) (n int, err error) {
return n, err
}
// BodySize returns the body size if the currently read object
func (r *RequestRecorder) BodySize() int {
// Size returns the body size if the currently read object
func (r *RequestRecorder) Size() int {
return r.bytesRead
}

View File

@@ -1,66 +0,0 @@
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package stats
import (
"io"
"net/http"
)
// IncomingTrafficMeter counts the incoming bytes from the underlying request.Body.
type IncomingTrafficMeter struct {
countBytes int64
io.ReadCloser
}
// Read calls the underlying Read and counts the transferred bytes.
func (r *IncomingTrafficMeter) Read(p []byte) (n int, err error) {
n, err = r.ReadCloser.Read(p)
r.countBytes += int64(n)
return n, err
}
// BytesRead returns the number of transferred bytes
func (r *IncomingTrafficMeter) BytesRead() int64 {
return r.countBytes
}
// OutgoingTrafficMeter counts the outgoing bytes through the responseWriter.
type OutgoingTrafficMeter struct {
countBytes int64
// wrapper for underlying http.ResponseWriter.
http.ResponseWriter
}
// Write calls the underlying write and counts the output bytes
func (w *OutgoingTrafficMeter) Write(p []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(p)
w.countBytes += int64(n)
return n, err
}
// Flush calls the underlying Flush.
func (w *OutgoingTrafficMeter) Flush() {
w.ResponseWriter.(http.Flusher).Flush()
}
// BytesWritten returns the number of transferred bytes
func (w *OutgoingTrafficMeter) BytesWritten() int64 {
return w.countBytes
}

View File

@@ -24,11 +24,10 @@ import (
"strconv"
"time"
"github.com/minio/minio/internal/http/stats"
internalAudit "github.com/minio/minio/internal/logger/message/audit"
"github.com/minio/minio/internal/mcontext"
"github.com/minio/pkg/logger/message/audit"
"github.com/klauspost/compress/gzhttp"
"github.com/minio/madmin-go/v2"
xhttp "github.com/minio/minio/internal/http"
)
@@ -96,34 +95,13 @@ func AuditLog(ctx context.Context, w http.ResponseWriter, r *http.Request, reqCl
headerBytes int64
)
var st *xhttp.ResponseRecorder
for {
switch v := w.(type) {
case *gzhttp.GzipResponseWriter:
// the writer may be obscured by gzip response writer
w = v.ResponseWriter
case *gzhttp.NoGzipResponseWriter:
// the writer may be obscured by no-gzip response writer
w = v.ResponseWriter
case *stats.OutgoingTrafficMeter:
// the writer may be obscured by http stats response writer
w = v.ResponseWriter
case *xhttp.ResponseRecorder:
st = v
goto exit
default:
goto exit
}
}
exit:
if st != nil {
statusCode = st.StatusCode
timeToResponse = time.Now().UTC().Sub(st.StartTime)
timeToFirstByte = st.TimeToFirstByte
outputBytes = int64(st.Size())
headerBytes = int64(st.HeaderSize())
tc, ok := r.Context().Value(mcontext.ContextTraceKey).(*mcontext.TraceCtxt)
if ok {
statusCode = tc.ResponseRecorder.StatusCode
outputBytes = int64(tc.ResponseRecorder.Size())
headerBytes = int64(tc.ResponseRecorder.HeaderSize())
timeToResponse = time.Now().UTC().Sub(tc.ResponseRecorder.StartTime)
timeToFirstByte = tc.ResponseRecorder.TimeToFirstByte
}
entry.AccessKey = reqInfo.Cred.AccessKey
@@ -147,7 +125,7 @@ func AuditLog(ctx context.Context, w http.ResponseWriter, r *http.Request, reqCl
entry.API.TimeToResponse = strconv.FormatInt(timeToResponse.Nanoseconds(), 10) + "ns"
entry.API.TimeToResponseInNS = strconv.FormatInt(timeToResponse.Nanoseconds(), 10)
entry.Tags = reqInfo.GetTagsMap()
// ttfb will be recorded only for GET requests, Ignore such cases where ttfb will be empty.
// ignore cases for ttfb when its zero.
if timeToFirstByte != 0 {
entry.API.TimeToFirstByte = strconv.FormatInt(timeToFirstByte.Nanoseconds(), 10) + "ns"
entry.API.TimeToFirstByteInNS = strconv.FormatInt(timeToFirstByte.Nanoseconds(), 10)