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

metrics_test.go « metrics - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c65aab3ceef331eb9b34d2e0bb4f713a3ddb8bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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"}`)
}