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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
package prometheus
import (
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"github.com/go-kit/kit/metrics/teststat"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func TestCounter(t *testing.T) {
t.Skip("Skipping flaky test")
s := httptest.NewServer(promhttp.HandlerFor(stdprometheus.DefaultGatherer, promhttp.HandlerOpts{}))
defer s.Close()
scrape := func() string {
resp, _ := http.Get(s.URL)
buf, _ := ioutil.ReadAll(resp.Body)
return string(buf)
}
namespace, subsystem, name := "ns", "ss", "foo"
re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{alpha="alpha-value",beta="beta-value"} ([0-9\.]+)`)
counter := NewCounterFrom(stdprometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: "This is the help string.",
}, []string{"alpha", "beta"}).With("beta", "beta-value", "alpha", "alpha-value") // order shouldn't matter
value := func() float64 {
matches := re.FindStringSubmatch(scrape())
f, _ := strconv.ParseFloat(matches[1], 64)
return f
}
if err := teststat.TestCounter(counter, value); err != nil {
t.Fatal(err)
}
}
func TestGauge(t *testing.T) {
s := httptest.NewServer(promhttp.HandlerFor(stdprometheus.DefaultGatherer, promhttp.HandlerOpts{}))
defer s.Close()
scrape := func() string {
resp, _ := http.Get(s.URL)
buf, _ := ioutil.ReadAll(resp.Body)
return string(buf)
}
namespace, subsystem, name := "aaa", "bbb", "ccc"
re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{foo="bar"} ([0-9\.]+)`)
gauge := NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: "This is a different help string.",
}, []string{"foo"}).With("foo", "bar")
value := func() []float64 {
matches := re.FindStringSubmatch(scrape())
f, _ := strconv.ParseFloat(matches[1], 64)
return []float64{f}
}
if err := teststat.TestGauge(gauge, value); err != nil {
t.Fatal(err)
}
}
func TestSummary(t *testing.T) {
t.Skip("Skipping flaky test")
s := httptest.NewServer(promhttp.HandlerFor(stdprometheus.DefaultGatherer, promhttp.HandlerOpts{}))
defer s.Close()
scrape := func() string {
resp, _ := http.Get(s.URL)
buf, _ := ioutil.ReadAll(resp.Body)
return string(buf)
}
namespace, subsystem, name := "test", "prometheus", "summary"
re50 := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{a="a",b="b",quantile="0.5"} ([0-9\.]+)`)
re90 := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{a="a",b="b",quantile="0.9"} ([0-9\.]+)`)
re99 := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{a="a",b="b",quantile="0.99"} ([0-9\.]+)`)
summary := NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: "This is the help string for the summary.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, []string{"a", "b"}).With("b", "b").With("a", "a")
quantiles := func() (float64, float64, float64, float64) {
buf := scrape()
match50 := re50.FindStringSubmatch(buf)
p50, _ := strconv.ParseFloat(match50[1], 64)
match90 := re90.FindStringSubmatch(buf)
p90, _ := strconv.ParseFloat(match90[1], 64)
match99 := re99.FindStringSubmatch(buf)
p99, _ := strconv.ParseFloat(match99[1], 64)
p95 := p90 + ((p99 - p90) / 2) // Prometheus, y u no p95??? :< #yolo
return p50, p90, p95, p99
}
if err := teststat.TestHistogram(summary, quantiles, 0.01); err != nil {
t.Fatal(err)
}
}
func TestHistogram(t *testing.T) {
// Prometheus reports histograms as a count of observations that fell into
// each predefined bucket, with the bucket value representing a global upper
// limit. That is, the count monotonically increases over the buckets. This
// requires a different strategy to test.
s := httptest.NewServer(promhttp.HandlerFor(stdprometheus.DefaultGatherer, promhttp.HandlerOpts{}))
defer s.Close()
scrape := func() string {
resp, _ := http.Get(s.URL)
buf, _ := ioutil.ReadAll(resp.Body)
return string(buf)
}
namespace, subsystem, name := "test", "prometheus", "histogram"
re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `_bucket{x="1",le="([0-9]+|\+Inf)"} ([0-9\.]+)`)
numStdev := 3
bucketMin := (teststat.Mean - (numStdev * teststat.Stdev))
bucketMax := (teststat.Mean + (numStdev * teststat.Stdev))
if bucketMin < 0 {
bucketMin = 0
}
bucketCount := 10
bucketDelta := (bucketMax - bucketMin) / bucketCount
buckets := []float64{}
for i := bucketMin; i <= bucketMax; i += bucketDelta {
buckets = append(buckets, float64(i))
}
histogram := NewHistogramFrom(stdprometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: "This is the help string for the histogram.",
Buckets: buckets,
}, []string{"x"}).With("x", "1")
// Can't TestHistogram, because Prometheus Histograms don't dynamically
// compute quantiles. Instead, they fill up buckets. So, let's populate the
// histogram kind of manually.
teststat.PopulateNormalHistogram(histogram, rand.Int())
// Then, we use ExpectedObservationsLessThan to validate.
for _, line := range strings.Split(scrape(), "\n") {
match := re.FindStringSubmatch(line)
if match == nil {
continue
}
bucket, _ := strconv.ParseInt(match[1], 10, 64)
have, _ := strconv.ParseFloat(match[2], 64)
want := teststat.ExpectedObservationsLessThan(bucket)
if match[1] == "+Inf" {
want = int64(teststat.Count) // special case
}
// Unfortunately, we observe experimentally that Prometheus is quite
// imprecise at the extremes. I'm setting a very high tolerance for now.
// It would be great to dig in and figure out whether that's a problem
// with my Expected calculation, or in Prometheus.
tolerance := 0.25
if delta := math.Abs(float64(want) - float64(have)); (delta / float64(want)) > tolerance {
t.Logf("Bucket %d: want %d, have %d (%.1f%%)", bucket, want, int(have), (100.0 * delta / float64(want)))
}
}
}
func TestInconsistentLabelCardinality(t *testing.T) {
defer func() {
x := recover()
if x == nil {
t.Fatal("expected panic, got none")
}
err, ok := x.(error)
if !ok {
t.Fatalf("expected error, got %s", reflect.TypeOf(x))
}
if want, have := "inconsistent label cardinality", err.Error(); !strings.HasPrefix(have, want) {
t.Fatalf("want %q, have %q", want, have)
}
}()
NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "test",
Subsystem: "inconsistent_label_cardinality",
Name: "foobar",
Help: "This is the help string for the metric.",
}, []string{"a", "b"}).With(
"a", "1", "b", "2", "c", "KABOOM!",
).Add(123)
}
|