detect CloseNotify capability in accesslog and metrics
This commit is contained in:
parent
1d4f10bead
commit
bdf4c6723f
5 changed files with 59 additions and 4 deletions
|
@ -89,10 +89,10 @@ func (m *metricsMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request)
|
|||
}(labels)
|
||||
|
||||
start := time.Now()
|
||||
recorder := &responseRecorder{rw, http.StatusOK}
|
||||
recorder := newResponseRecorder(rw)
|
||||
m.next.ServeHTTP(recorder, req)
|
||||
|
||||
labels = append(labels, "code", strconv.Itoa(recorder.statusCode))
|
||||
labels = append(labels, "code", strconv.Itoa(recorder.getCode()))
|
||||
m.reqsCounter.With(labels...).Add(1)
|
||||
m.reqDurationHistogram.With(labels...).Observe(time.Since(start).Seconds())
|
||||
}
|
||||
|
|
|
@ -6,6 +6,23 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
type recorder interface {
|
||||
http.ResponseWriter
|
||||
http.Flusher
|
||||
getCode() int
|
||||
}
|
||||
|
||||
func newResponseRecorder(rw http.ResponseWriter) recorder {
|
||||
rec := &responseRecorder{
|
||||
ResponseWriter: rw,
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
if _, ok := rw.(http.CloseNotifier); !ok {
|
||||
return rec
|
||||
}
|
||||
return responseRecorderWithCloseNotify{rec}
|
||||
}
|
||||
|
||||
// responseRecorder captures information from the response and preserves it for
|
||||
// later analysis.
|
||||
type responseRecorder struct {
|
||||
|
@ -13,6 +30,20 @@ type responseRecorder struct {
|
|||
statusCode int
|
||||
}
|
||||
|
||||
type responseRecorderWithCloseNotify struct {
|
||||
*responseRecorder
|
||||
}
|
||||
|
||||
// CloseNotify returns a channel that receives at most a
|
||||
// single value (true) when the client connection has gone away.
|
||||
func (r *responseRecorderWithCloseNotify) CloseNotify() <-chan bool {
|
||||
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
||||
}
|
||||
|
||||
func (r *responseRecorder) getCode() int {
|
||||
return r.statusCode
|
||||
}
|
||||
|
||||
// WriteHeader captures the status code for later retrieval.
|
||||
func (r *responseRecorder) WriteHeader(status int) {
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue