Commit 000ebbf2 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Re-use CountingResponseWriter for Git HTTP metrics

parent 625b53a1
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
...@@ -42,39 +44,14 @@ func init() { ...@@ -42,39 +44,14 @@ func init() {
} }
type GitHttpResponseWriter struct { type GitHttpResponseWriter struct {
rw http.ResponseWriter helper.CountingResponseWriter
status int
written int64
} }
func NewGitHttpResponseWriter(rw http.ResponseWriter) *GitHttpResponseWriter { func NewGitHttpResponseWriter(rw http.ResponseWriter) *GitHttpResponseWriter {
gitHTTPSessionsActive.Inc() gitHTTPSessionsActive.Inc()
return &GitHttpResponseWriter{ return &GitHttpResponseWriter{
rw: rw, CountingResponseWriter: helper.NewCountingResponseWriter(rw),
}
}
func (w *GitHttpResponseWriter) Header() http.Header {
return w.rw.Header()
}
func (w *GitHttpResponseWriter) Write(data []byte) (n int, err error) {
if w.status == 0 {
w.WriteHeader(http.StatusOK)
} }
n, err = w.rw.Write(data)
w.written += int64(n)
return n, err
}
func (w *GitHttpResponseWriter) WriteHeader(status int) {
if w.status != 0 {
return
}
w.status = status
w.rw.WriteHeader(status)
} }
func (w *GitHttpResponseWriter) Log(r *http.Request, writtenIn int64) { func (w *GitHttpResponseWriter) Log(r *http.Request, writtenIn int64) {
...@@ -82,11 +59,11 @@ func (w *GitHttpResponseWriter) Log(r *http.Request, writtenIn int64) { ...@@ -82,11 +59,11 @@ func (w *GitHttpResponseWriter) Log(r *http.Request, writtenIn int64) {
agent := getRequestAgent(r) agent := getRequestAgent(r)
gitHTTPSessionsActive.Dec() gitHTTPSessionsActive.Dec()
gitHTTPRequests.WithLabelValues(r.Method, strconv.Itoa(w.status), service, agent).Inc() gitHTTPRequests.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent).Inc()
gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.status), service, agent, directionIn). gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent, directionIn).
Add(float64(writtenIn)) Add(float64(writtenIn))
gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.status), service, agent, directionOut). gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent, directionOut).
Add(float64(w.written)) Add(float64(w.Count()))
} }
func getRequestAgent(r *http.Request) string { func getRequestAgent(r *http.Request) string {
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
type CountingResponseWriter interface { type CountingResponseWriter interface {
http.ResponseWriter http.ResponseWriter
Count() int64 Count() int64
Status() int
} }
type countingResponseWriter struct { type countingResponseWriter struct {
...@@ -42,6 +43,14 @@ func (c *countingResponseWriter) WriteHeader(status int) { ...@@ -42,6 +43,14 @@ func (c *countingResponseWriter) WriteHeader(status int) {
c.rw.WriteHeader(status) c.rw.WriteHeader(status)
} }
// Count returns the number of bytes written to the ResponseWriter. This
// function is not thread-safe.
func (c *countingResponseWriter) Count() int64 { func (c *countingResponseWriter) Count() int64 {
return c.count return c.count
} }
// Status returns the first HTTP status value that was written to the
// ResponseWriter. This function is not thread-safe.
func (c *countingResponseWriter) Status() int {
return c.status
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment