Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaime Martinez <jmartinez@gitlab.com>2020-09-10 07:40:01 +0300
committerJaime Martinez <jmartinez@gitlab.com>2020-09-15 07:49:29 +0300
commit17f10d8dbff9d228e111b99063bc71fc10695000 (patch)
treea5a648beb13c9198367e42b3e435fb1d02316c24 /metrics
parent264c22c58941320fdb99150c88b68a3e278b8874 (diff)
Add metrics tests for vectors
Diffstat (limited to 'metrics')
-rw-r--r--metrics/metrics_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go
new file mode 100644
index 00000000..c65aab3c
--- /dev/null
+++ b/metrics/metrics_test.go
@@ -0,0 +1,60 @@
+package metrics
+
+import (
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+ "github.com/prometheus/client_golang/prometheus/testutil"
+ "github.com/stretchr/testify/require"
+)
+
+func TestMetricsVectorsCanBeScraped(t *testing.T) {
+ reg := prometheus.NewRegistry()
+
+ // vectors will only be available in /metrics after a label has been set/incremented so we can't test these in
+ // TestPrometheusMetricsCanBeScraped as part of the acceptance tests
+ reg.MustRegister(
+ DomainsSourceAPIReqTotal,
+ DomainsSourceAPICallDuration,
+ ZipFileServingReqTotal,
+ ZipFileServingReqDuration,
+ )
+
+ handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
+ testServer := httptest.NewServer(handler)
+ defer testServer.Close()
+
+ DomainsSourceAPICallDuration.WithLabelValues("200").Set(float64(20 * time.Millisecond))
+ DomainsSourceAPIReqTotal.WithLabelValues("200").Inc()
+
+ c, err := DomainsSourceAPIReqTotal.GetMetricWithLabelValues("200")
+ require.NoError(t, err)
+ require.Equal(t, float64(1), testutil.ToFloat64(c))
+
+ ZipFileServingReqDuration.WithLabelValues("200").Set(float64(20 * time.Millisecond))
+ ZipFileServingReqTotal.WithLabelValues("200").Inc()
+
+ c, err = ZipFileServingReqTotal.GetMetricWithLabelValues("200")
+ require.NoError(t, err)
+ require.Equal(t, float64(1), testutil.ToFloat64(c))
+
+ metricFamilies, err := reg.Gather()
+ require.NoError(t, err)
+
+ require.Len(t, metricFamilies, 4)
+
+ res, err := http.Get(testServer.URL + "/metrics")
+ require.NoError(t, err)
+ defer res.Body.Close()
+ body, _ := ioutil.ReadAll(res.Body)
+
+ require.Contains(t, string(body), `gitlab_pages_domains_source_api_requests_total{status_code="200"}`)
+ require.Contains(t, string(body), `gitlab_pages_domains_source_api_call_duration{status_code="200"}`)
+ require.Contains(t, string(body), `gitlab_pages_httprange_zip_reader_requests_total{status_code="200"}`)
+ require.Contains(t, string(body), `gitlab_pages_httprange_zip_reader_requests_duration{status_code="200"}`)
+}