diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-06-03 10:01:46 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-06-03 10:13:35 +0300 |
commit | 4eedf5c25e17698d03cd71960040f155f51672c9 (patch) | |
tree | f91722da8237d111437d42e73ca3c1ca1e3d4f05 /internal/serving | |
parent | 193b911922d389ab2a8145d1fda190faecc13704 (diff) |
Add file size metric for disk serving
Diffstat (limited to 'internal/serving')
-rw-r--r-- | internal/serving/disk/reader.go | 7 | ||||
-rw-r--r-- | internal/serving/disk/serving.go | 11 | ||||
-rw-r--r-- | internal/serving/disk/serving_test.go | 43 |
3 files changed, 56 insertions, 5 deletions
diff --git a/internal/serving/disk/reader.go b/internal/serving/disk/reader.go index 0bc72646..38d055cf 100644 --- a/internal/serving/disk/reader.go +++ b/internal/serving/disk/reader.go @@ -10,11 +10,14 @@ import ( "strings" "time" + "github.com/prometheus/client_golang/prometheus" + "gitlab.com/gitlab-org/gitlab-pages/internal/serving" ) // Reader is a disk access driver type Reader struct { + fileSizeMetrics *prometheus.GaugeVec } func (reader *Reader) tryFile(h serving.Handler) error { @@ -141,6 +144,8 @@ func (reader *Reader) serveFile(w http.ResponseWriter, r *http.Request, origPath return err } + reader.fileSizeMetrics.With(prometheus.Labels{"serving_type": "disk", "content_type": contentType}).Set(float64(fi.Size())) + w.Header().Set("Content-Type", contentType) http.ServeContent(w, r, origPath, fi.ModTime(), file) @@ -167,6 +172,8 @@ func (reader *Reader) serveCustomFile(w http.ResponseWriter, r *http.Request, co return err } + reader.fileSizeMetrics.With(prometheus.Labels{"serving_type": "disk", "content_type": contentType}).Set(float64(fi.Size())) + w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10)) w.WriteHeader(code) diff --git a/internal/serving/disk/serving.go b/internal/serving/disk/serving.go index b4c1ba96..84945c8d 100644 --- a/internal/serving/disk/serving.go +++ b/internal/serving/disk/serving.go @@ -3,9 +3,14 @@ package disk import ( "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" "gitlab.com/gitlab-org/gitlab-pages/internal/serving" + "gitlab.com/gitlab-org/gitlab-pages/metrics" ) -var disk *Disk = &Disk{} +var disk = &Disk{ + reader: Reader{ + fileSizeMetrics: metrics.ServingFileSize, + }, +} // Disk describes a disk access serving type Disk struct { @@ -31,9 +36,5 @@ func (s *Disk) ServeNotFoundHTTP(h serving.Handler) { // New returns a serving instance that is capable of reading files // from the disk func New() serving.Serving { - if disk == nil { - disk = &Disk{} - } - return disk } diff --git a/internal/serving/disk/serving_test.go b/internal/serving/disk/serving_test.go new file mode 100644 index 00000000..01a9a775 --- /dev/null +++ b/internal/serving/disk/serving_test.go @@ -0,0 +1,43 @@ +package disk + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-pages/internal/serving" + "gitlab.com/gitlab-org/gitlab-pages/metrics" +) + +func TestDisk_ServeFileHTTP(t *testing.T) { + s := New() + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "http://group.gitlab-example.com/serving/index.html", nil) + handler := serving.Handler{ + Writer: w, + Request: r, + LookupPath: &serving.LookupPath{ + Prefix: "/serving", + Path: "../../../shared/pages/group/serving/public", + }, + SubPath: "/index.html", + } + + require.True(t, s.ServeFileHTTP(handler)) + + resp := w.Result() + defer resp.Body.Close() + + require.Equal(t, http.StatusOK, resp.StatusCode) + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + require.Contains(t, string(body), "HTML Document") + gaugeValue := testutil.ToFloat64(metrics.ServingFileSize.With(prometheus.Labels{"serving_type": "disk", "content_type": resp.Header.Get("Content-Type")})) + require.Greater(t, gaugeValue, float64(0)) +} |