Package list golang-github-go-kit-kit / bb87de2
metrics: improve test coverage Peter Bourgon 6 years ago
3 changed file(s) with 37 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
5252 Subsystem: "multi_counter",
5353 Name: "beta",
5454 Help: "Beta counter.",
55 }, []string{}),
56 ).Add(123)
55 }, []string{"a"}),
56 ).With(metrics.Field{Key: "a", Value: "b"}).Add(123)
5757
5858 if want, have := "123", stdexpvar.Get("alpha").String(); want != have {
5959 t.Errorf("expvar: want %q, have %q", want, have)
6262 if want, have := strings.Join([]string{
6363 `# HELP test_multi_counter_beta Beta counter.`,
6464 `# TYPE test_multi_counter_beta counter`,
65 `test_multi_counter_beta 123`,
65 `test_multi_counter_beta{a="b"} 123`,
6666 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
6767 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
6868 }
7676 Subsystem: "multi_gauge",
7777 Name: "kappa",
7878 Help: "Kappa gauge.",
79 }, []string{}),
80 )
81
82 g.Set(34)
79 }, []string{"a"}),
80 )
81
82 f := metrics.Field{Key: "a", Value: "aaa"}
83 g.With(f).Set(34)
8384
8485 if want, have := "34", stdexpvar.Get("delta").String(); want != have {
8586 t.Errorf("expvar: want %q, have %q", want, have)
8788 if want, have := strings.Join([]string{
8889 `# HELP test_multi_gauge_kappa Kappa gauge.`,
8990 `# TYPE test_multi_gauge_kappa gauge`,
90 `test_multi_gauge_kappa 34`,
91 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
92 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
93 }
94
95 g.Add(-40)
91 `test_multi_gauge_kappa{a="aaa"} 34`,
92 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
93 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
94 }
95
96 g.With(f).Add(-40)
9697
9798 if want, have := "-6", stdexpvar.Get("delta").String(); want != have {
9899 t.Errorf("expvar: want %q, have %q", want, have)
100101 if want, have := strings.Join([]string{
101102 `# HELP test_multi_gauge_kappa Kappa gauge.`,
102103 `# TYPE test_multi_gauge_kappa gauge`,
103 `test_multi_gauge_kappa -6`,
104 `test_multi_gauge_kappa{a="aaa"} -6`,
104105 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
105106 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
106107 }
121122 const seed, mean, stdev int64 = 123, 50, 10
122123 populateNormalHistogram(t, h, seed, mean, stdev)
123124 assertExpvarNormalHistogram(t, "omicron", mean, stdev, quantiles)
124 assertPrometheusNormalHistogram(t, "test_multi_histogram_nu", mean, stdev)
125 assertPrometheusNormalHistogram(t, `test_multi_histogram_nu`, mean, stdev)
125126 }
126127
127128 func populateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) {
219220 }
220221
221222 func getPrometheusQuantile(t *testing.T, scrape, name, quantileStr string) int {
222 matches := regexp.MustCompile(name+`{quantile="`+quantileStr+`"} ([0-9]+)`).FindAllStringSubmatch(scrape, -1)
223 re := name + `{quantile="` + quantileStr + `"} ([0-9]+)`
224 matches := regexp.MustCompile(re).FindAllStringSubmatch(scrape, -1)
223225 if len(matches) < 1 {
224 t.Fatalf("%q: quantile %q not found in scrape", name, quantileStr)
226 t.Fatalf("%q: quantile %q not found in scrape (%s)", name, quantileStr, re)
225227 }
226228 if len(matches[0]) < 2 {
227 t.Fatalf("%q: quantile %q not found in scrape", name, quantileStr)
229 t.Fatalf("%q: quantile %q not found in scrape (%s)", name, quantileStr, re)
228230 }
229231 i, err := strconv.Atoi(matches[0][1])
230232 if err != nil {
77 )
88
99 func TestScaledHistogram(t *testing.T) {
10 quantiles := []int{50, 90, 99}
11 scale := int64(10)
12 metricName := "test_scaled_histogram"
10 var (
11 quantiles = []int{50, 90, 99}
12 scale = int64(10)
13 metricName = "test_scaled_histogram"
14 )
1315
1416 var h metrics.Histogram
1517 h = expvar.NewHistogram(metricName, 0, 1000, 3, quantiles...)
1618 h = metrics.NewScaledHistogram(h, scale)
19 h = h.With(metrics.Field{Key: "a", Value: "b"})
1720
1821 const seed, mean, stdev = 333, 500, 100 // input values
1922 populateNormalHistogram(t, h, seed, mean, stdev) // will be scaled down
99 )
1010
1111 func TestTimeHistogram(t *testing.T) {
12 const metricName string = "test_time_histogram"
13 quantiles := []int{50, 90, 99}
14 h0 := expvar.NewHistogram(metricName, 0, 200, 3, quantiles...)
15 h := metrics.NewTimeHistogram(time.Millisecond, h0)
12 var (
13 metricName = "test_time_histogram"
14 minValue = int64(0)
15 maxValue = int64(200)
16 sigfigs = 3
17 quantiles = []int{50, 90, 99}
18 h = expvar.NewHistogram(metricName, minValue, maxValue, sigfigs, quantiles...)
19 th = metrics.NewTimeHistogram(time.Millisecond, h).With(metrics.Field{Key: "a", Value: "b"})
20 )
21
1622 const seed, mean, stdev int64 = 321, 100, 20
1723
1824 for i := 0; i < 4321; i++ {
1925 sample := time.Duration(rand.NormFloat64()*float64(stdev)+float64(mean)) * time.Millisecond
20 h.Observe(sample)
26 th.Observe(sample)
2127 }
2228
2329 assertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles)