diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-02-08 03:39:50 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-02-08 03:39:53 +0300 |
commit | de481bad5878862ae6f693190c3aa3d85242b2c9 (patch) | |
tree | 5295952727779ca2597dd897be85379e3d6f9fc8 | |
parent | 9504871174848d6c3f47c6ba1d89899d1cd6c7f1 (diff) |
Export MeteredRoundTripper
Exports the struct so that it can be asserted
and adds a method that is called by the zip VFS.
-rw-r--r-- | internal/httptransport/metered_round_tripper.go | 17 | ||||
-rw-r--r-- | internal/httptransport/trace.go | 4 | ||||
-rw-r--r-- | internal/httptransport/transport.go | 2 | ||||
-rw-r--r-- | internal/httptransport/transport_test.go | 4 | ||||
-rw-r--r-- | internal/vfs/zip/vfs.go | 29 |
5 files changed, 29 insertions, 27 deletions
diff --git a/internal/httptransport/metered_round_tripper.go b/internal/httptransport/metered_round_tripper.go index fc652086..e8facbaa 100644 --- a/internal/httptransport/metered_round_tripper.go +++ b/internal/httptransport/metered_round_tripper.go @@ -12,7 +12,9 @@ import ( log "github.com/sirupsen/logrus" ) -type meteredRoundTripper struct { +// MeteredRoundTripper is a custom http.Transport that implements the http.RoundTripper interface. +// It holds prometheus metrics to report connection usage and durations. +type MeteredRoundTripper struct { next http.RoundTripper name string tracer *prometheus.HistogramVec @@ -29,7 +31,7 @@ func NewMeteredRoundTripper(transport *http.Transport, name string, tracerVec, d transport = DefaultTransport } - return &meteredRoundTripper{ + return &MeteredRoundTripper{ next: transport, name: name, tracer: tracerVec, @@ -39,9 +41,9 @@ func NewMeteredRoundTripper(transport *http.Transport, name string, tracerVec, d } } -// RoundTripper wraps the original http.Transport into a meteredRoundTripper which +// RoundTrip wraps the original http.Transport into a MeteredRoundTripper which // reports metrics on request duration, tracing and request count -func (mrt *meteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { +func (mrt *MeteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { start := time.Now() ctx := httptrace.WithClientTrace(r.Context(), mrt.newTracer(start)) @@ -67,7 +69,7 @@ func (mrt *meteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, erro return resp, nil } -func (mrt *meteredRoundTripper) logResponse(req *http.Request, resp *http.Response) { +func (mrt *MeteredRoundTripper) logResponse(req *http.Request, resp *http.Response) { if log.GetLevel() == log.TraceLevel { l := log.WithFields(log.Fields{ "client_name": mrt.name, @@ -82,3 +84,8 @@ func (mrt *meteredRoundTripper) logResponse(req *http.Request, resp *http.Respon l.Traceln("response") } } + +// RegisterProtocol calls the RegisterProtocol on the MeteredRoundTripper's next Transport +func (mrt *MeteredRoundTripper) RegisterProtocol(scheme string, rt http.RoundTripper) { + mrt.next.(*http.Transport).RegisterProtocol(scheme, rt) +} diff --git a/internal/httptransport/trace.go b/internal/httptransport/trace.go index 9ece5fc4..f99b282d 100644 --- a/internal/httptransport/trace.go +++ b/internal/httptransport/trace.go @@ -8,7 +8,7 @@ import ( "gitlab.com/gitlab-org/labkit/log" ) -func (mrt *meteredRoundTripper) newTracer(start time.Time) *httptrace. +func (mrt *MeteredRoundTripper) newTracer(start time.Time) *httptrace. ClientTrace { trace := &httptrace.ClientTrace{ GetConn: func(host string) { @@ -71,7 +71,7 @@ func (mrt *meteredRoundTripper) newTracer(start time.Time) *httptrace. return trace } -func (mrt *meteredRoundTripper) httpTraceObserve(label string, start time.Time) { +func (mrt *MeteredRoundTripper) httpTraceObserve(label string, start time.Time) { mrt.tracer.WithLabelValues(label). Observe(time.Since(start).Seconds()) } diff --git a/internal/httptransport/transport.go b/internal/httptransport/transport.go index fcadc5fe..a462f7ee 100644 --- a/internal/httptransport/transport.go +++ b/internal/httptransport/transport.go @@ -12,7 +12,7 @@ import ( ) const ( - // DefaultTTFBTimeout is the timeout used in the meteredRoundTripper + // DefaultTTFBTimeout is the timeout used in the MeteredRoundTripper // when calling http.Transport.RoundTrip. The request will be cancelled // if the response takes longer than this. DefaultTTFBTimeout = 15 * time.Second diff --git a/internal/httptransport/transport_test.go b/internal/httptransport/transport_test.go index feaf63b6..25c27f17 100644 --- a/internal/httptransport/transport_test.go +++ b/internal/httptransport/transport_test.go @@ -55,7 +55,7 @@ func Test_withRoundTripper(t *testing.T) { timeout: time.Nanosecond, } - mtr := &meteredRoundTripper{next: next, durations: histVec, counter: counterVec, ttfbTimeout: DefaultTTFBTimeout} + mtr := &MeteredRoundTripper{next: next, durations: histVec, counter: counterVec, ttfbTimeout: DefaultTTFBTimeout} r := httptest.NewRequest("GET", "/", nil) res, err := mtr.RoundTrip(r) @@ -86,7 +86,7 @@ func TestRoundTripTTFBTimeout(t *testing.T) { err: nil, } - mtr := &meteredRoundTripper{next: next, durations: histVec, counter: counterVec, ttfbTimeout: time.Nanosecond} + mtr := &MeteredRoundTripper{next: next, durations: histVec, counter: counterVec, ttfbTimeout: time.Nanosecond} req, err := http.NewRequest("GET", "https://gitlab.com", nil) require.NoError(t, err) diff --git a/internal/vfs/zip/vfs.go b/internal/vfs/zip/vfs.go index b7055b01..29847ac9 100644 --- a/internal/vfs/zip/vfs.go +++ b/internal/vfs/zip/vfs.go @@ -8,13 +8,12 @@ import ( "sync" "time" - "gitlab.com/gitlab-org/gitlab-pages/internal/httpfs" - "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" - "github.com/patrickmn/go-cache" "gitlab.com/gitlab-org/gitlab-pages/internal/config" + "gitlab.com/gitlab-org/gitlab-pages/internal/httpfs" "gitlab.com/gitlab-org/gitlab-pages/internal/httprange" + "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" "gitlab.com/gitlab-org/gitlab-pages/internal/vfs" "gitlab.com/gitlab-org/gitlab-pages/metrics" ) @@ -63,6 +62,14 @@ func New(cfg *config.ZipServing) vfs.VFS { // TODO: make this timeout configurable // https://gitlab.com/gitlab-org/gitlab-pages/-/issues/457 Timeout: 30 * time.Minute, + Transport: httptransport.NewMeteredRoundTripper( + httptransport.NewTransport(), + "zip_vfs", + metrics.HTTPRangeTraceDuration, + metrics.HTTPRangeRequestDuration, + metrics.HTTPRangeRequestsTotal, + httptransport.DefaultTTFBTimeout, + ), }, } @@ -96,25 +103,13 @@ func (fs *zipVFS) Reconfigure(cfg *config.Config) error { } func (fs *zipVFS) reconfigureTransport(cfg *config.Config) error { - transport := httptransport.NewTransport() - fsTransport, err := httpfs.NewFileSystemPath(cfg.Zip.AllowedPaths) if err != nil { return err } - transport.RegisterProtocol("file", http.NewFileTransport(fsTransport)) - - mrt := httptransport.NewMeteredRoundTripper( - transport, - "httprange_client", - metrics.HTTPRangeTraceDuration, - metrics.HTTPRangeRequestDuration, - metrics.HTTPRangeRequestsTotal, - httptransport.DefaultTTFBTimeout, - ) - - fs.httpClient.Transport = mrt + fs.httpClient.Transport.(*httptransport.MeteredRoundTripper). + RegisterProtocol("file", http.NewFileTransport(fsTransport)) return nil } |