Commit 5f36b358 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add metrics to internal/api package

parent 20ee508c
...@@ -8,8 +8,11 @@ import ( ...@@ -8,8 +8,11 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/badgateway" "gitlab.com/gitlab-org/gitlab-workhorse/internal/badgateway"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/secret" "gitlab.com/gitlab-org/gitlab-workhorse/internal/secret"
...@@ -26,6 +29,27 @@ type API struct { ...@@ -26,6 +29,27 @@ type API struct {
Version string Version string
} }
var (
requestsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_internal_api_requests",
Help: "How many internal API requests have been completed by gitlab-workhorse, partitioned by status code and HTTP method.",
},
[]string{"code", "method"},
)
bytesTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitlab_workhorse_internal_api_failure_response_bytes",
Help: "How many bytes have been returned by upstream GitLab in API failure/rejection response bodies.",
},
)
)
func init() {
prometheus.MustRegister(requestsCounter)
prometheus.MustRegister(bytesTotal)
}
func NewAPI(myURL *url.URL, version string, roundTripper *badgateway.RoundTripper) *API { func NewAPI(myURL *url.URL, version string, roundTripper *badgateway.RoundTripper) *API {
return &API{ return &API{
Client: &http.Client{Transport: roundTripper}, Client: &http.Client{Transport: roundTripper},
...@@ -161,6 +185,7 @@ func (api *API) PreAuthorize(suffix string, r *http.Request) (httpResponse *http ...@@ -161,6 +185,7 @@ func (api *API) PreAuthorize(suffix string, r *http.Request) (httpResponse *http
httpResponse = nil httpResponse = nil
} }
}() }()
requestsCounter.WithLabelValues(strconv.Itoa(httpResponse.StatusCode), authReq.Method).Inc()
if httpResponse.StatusCode != http.StatusOK { if httpResponse.StatusCode != http.StatusOK {
return httpResponse, nil, nil return httpResponse, nil, nil
...@@ -205,6 +230,7 @@ func (api *API) PreAuthorizeHandler(next HandleFunc, suffix string) http.Handler ...@@ -205,6 +230,7 @@ func (api *API) PreAuthorizeHandler(next HandleFunc, suffix string) http.Handler
helper.Fail500(w, r, err) helper.Fail500(w, r, err)
} }
httpResponse.Body.Close() // Free up the Unicorn worker httpResponse.Body.Close() // Free up the Unicorn worker
bytesTotal.Add(float64(responseBody.Len()))
for k, v := range httpResponse.Header { for k, v := range httpResponse.Header {
// Accomodate broken clients that do case-sensitive header lookup // Accomodate broken clients that do case-sensitive header lookup
...@@ -215,7 +241,9 @@ func (api *API) PreAuthorizeHandler(next HandleFunc, suffix string) http.Handler ...@@ -215,7 +241,9 @@ func (api *API) PreAuthorizeHandler(next HandleFunc, suffix string) http.Handler
} }
} }
w.WriteHeader(httpResponse.StatusCode) w.WriteHeader(httpResponse.StatusCode)
io.Copy(w, responseBody) if _, err := io.Copy(w, responseBody); err != nil {
helper.LogError(r, err)
}
return return
} }
......
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