audit: Merge ResponseWriter with RecordAPIStats (#9496)

ResponseWriter & RecordAPIStats has similar role, merge them.

This commit will also fix wrong auditing for STS and Web and others
since they are using ResponseWriter instead of the RecordAPIStats.
This commit is contained in:
Anis Elleuch
2020-04-30 19:27:19 +01:00
committed by GitHub
parent c7470e6e6e
commit 27632ca6ec
5 changed files with 17 additions and 60 deletions

View File

@@ -19,7 +19,6 @@ package stats
import (
"io"
"net/http"
"time"
)
// IncomingTrafficMeter counts the incoming bytes from the underlying request.Body.
@@ -63,45 +62,3 @@ func (w *OutgoingTrafficMeter) Flush() {
func (w OutgoingTrafficMeter) BytesCount() int {
return w.countBytes
}
// RecordAPIStats is a response writer which stores
// information of the underlying http response.
type RecordAPIStats struct {
http.ResponseWriter
TTFB time.Duration // TimeToFirstByte.
StartTime time.Time
RespStatusCode int
firstByteRead bool
}
// NewRecordAPIStats creates a new response writer with
// start time set to the function call time.
func NewRecordAPIStats(w http.ResponseWriter) *RecordAPIStats {
return &RecordAPIStats{
ResponseWriter: w,
StartTime: time.Now().UTC(),
}
}
// WriteHeader calls the underlying WriteHeader
// and records the response status code.
func (r *RecordAPIStats) WriteHeader(i int) {
r.RespStatusCode = i
r.ResponseWriter.WriteHeader(i)
}
// Write calls the underlying Write and updates TTFB and other info
func (r *RecordAPIStats) Write(p []byte) (n int, err error) {
if !r.firstByteRead {
r.TTFB = time.Now().UTC().Sub(r.StartTime)
r.firstByteRead = true
}
n, err = r.ResponseWriter.Write(p)
return
}
// Flush calls the underlying Flush.
func (r *RecordAPIStats) Flush() {
r.ResponseWriter.(http.Flusher).Flush()
}