diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/httprange/http_reader.go | 11 | ||||
-rw-r--r-- | internal/httptransport/transport.go | 40 | ||||
-rw-r--r-- | internal/httptransport/transport_test.go | 2 | ||||
-rw-r--r-- | internal/logging/logging.go | 2 |
4 files changed, 47 insertions, 8 deletions
diff --git a/internal/httprange/http_reader.go b/internal/httprange/http_reader.go index 72ca3cf9..9b6a8bbd 100644 --- a/internal/httprange/http_reader.go +++ b/internal/httprange/http_reader.go @@ -8,6 +8,7 @@ import ( "time" "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" + "gitlab.com/gitlab-org/gitlab-pages/metrics" ) var ( @@ -44,10 +45,12 @@ type Reader struct { // instead https://gitlab.com/gitlab-org/gitlab-pages/-/issues/457 var httpClient = &http.Client{ // The longest time the request can be executed - Timeout: 30 * time.Minute, - Transport: httptransport.InternalTransport, - // TODO: add metrics https://gitlab.com/gitlab-org/gitlab-pages/-/issues/448 - // Transport: httptransport.NewTransportWithMetrics(metrics.ZIPHttpReaderReqDuration, metrics.ZIPHttpReaderReqTotal), + Timeout: 30 * time.Minute, + Transport: httptransport.NewTransportWithMetrics( + metrics.ZipFileServingReqDuration, + metrics.ZipFileServingReqTotal, + httptransport.WithMeteredRoundTripperName("httprange_client"), + ), } // ensureResponse is set before reading from it. diff --git a/internal/httptransport/transport.go b/internal/httptransport/transport.go index e6094ed7..4f177914 100644 --- a/internal/httptransport/transport.go +++ b/internal/httptransport/transport.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "strconv" + "strings" "sync" "time" @@ -25,10 +26,14 @@ var ( type meteredRoundTripper struct { next http.RoundTripper + name string durations *prometheus.GaugeVec counter *prometheus.CounterVec } +// Option setting for metered round tripper +type Option func(*meteredRoundTripper) + func newInternalTransport() *http.Transport { return &http.Transport{ DialTLS: func(network, addr string) (net.Conn, error) { @@ -44,12 +49,25 @@ func newInternalTransport() *http.Transport { // NewTransportWithMetrics will create a custom http.RoundTripper that can be used with an http.Client. // The RoundTripper will report metrics based on the collectors passed. -func NewTransportWithMetrics(gaugeVec *prometheus.GaugeVec, counterVec *prometheus.CounterVec) http.RoundTripper { - return &meteredRoundTripper{ +func NewTransportWithMetrics(gaugeVec *prometheus.GaugeVec, counterVec *prometheus.CounterVec, options ...Option) http.RoundTripper { + mtr := &meteredRoundTripper{ next: InternalTransport, durations: gaugeVec, counter: counterVec, } + + for _, option := range options { + option(mtr) + } + + return mtr +} + +// WithMeteredRoundTripperName adds a name to the meteredRoundTripper instance +func WithMeteredRoundTripperName(name string) func(*meteredRoundTripper) { + return func(tripper *meteredRoundTripper) { + tripper.name = name + } } // This is here because macOS does not support the SSL_CERT_FILE and @@ -87,9 +105,27 @@ func (mrt *meteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, erro return nil, err } + mrt.logResponse(r, resp) + statusCode := strconv.Itoa(resp.StatusCode) mrt.durations.WithLabelValues(statusCode).Set(time.Since(start).Seconds()) mrt.counter.WithLabelValues(statusCode).Inc() return resp, nil } + +func (mrt *meteredRoundTripper) logResponse(req *http.Request, resp *http.Response) { + if log.GetLevel() == log.TraceLevel { + l := log.WithFields(log.Fields{ + "client_name": mrt.name, + "req_url": req.URL.String(), + "res_status_code": resp.StatusCode, + }) + + for header, value := range resp.Header { + l = l.WithField(strings.ToLower(header), strings.Join(value, ";")) + } + + l.Traceln("response") + } +} diff --git a/internal/httptransport/transport_test.go b/internal/httptransport/transport_test.go index 330d1022..60fbbb47 100644 --- a/internal/httptransport/transport_test.go +++ b/internal/httptransport/transport_test.go @@ -58,7 +58,7 @@ func Test_withRoundTripper(t *testing.T) { err: tt.err, } - mtr := &meteredRoundTripper{next, gaugeVec, counterVec} + mtr := &meteredRoundTripper{next: next, durations: gaugeVec, counter: counterVec} r := httptest.NewRequest("GET", "/", nil) res, err := mtr.RoundTrip(r) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 4ba86985..3432cdf7 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -18,7 +18,7 @@ func ConfigureLogging(format string, verbose bool) error { } if verbose { - levelOption = log.WithLogLevel("debug") + levelOption = log.WithLogLevel("trace") } else { levelOption = log.WithLogLevel("info") } |