| 1 |
// Copyright 2018 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package testutil |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"net/http" |
| 19 |
"net/http/httptest" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/prometheus/common/expfmt" |
| 24 |
|
| 25 |
"github.com/prometheus/client_golang/prometheus" |
| 26 |
) |
| 27 |
|
| 28 |
type untypedCollector struct{} |
| 29 |
|
| 30 |
func (u untypedCollector) Describe(c chan<- *prometheus.Desc) { |
| 31 |
c <- prometheus.NewDesc("name", "help", nil, nil) |
| 32 |
} |
| 33 |
|
| 34 |
func (u untypedCollector) Collect(c chan<- prometheus.Metric) { |
| 35 |
c <- prometheus.MustNewConstMetric( |
| 36 |
prometheus.NewDesc("name", "help", nil, nil), |
| 37 |
prometheus.UntypedValue, |
| 38 |
2001, |
| 39 |
) |
| 40 |
} |
| 41 |
|
| 42 |
func TestToFloat64(t *testing.T) { |
| 43 |
gaugeWithAValueSet := prometheus.NewGauge(prometheus.GaugeOpts{}) |
| 44 |
gaugeWithAValueSet.Set(3.14) |
| 45 |
|
| 46 |
counterVecWithOneElement := prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"foo"}) |
| 47 |
counterVecWithOneElement.WithLabelValues("bar").Inc() |
| 48 |
|
| 49 |
counterVecWithTwoElements := prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"foo"}) |
| 50 |
counterVecWithTwoElements.WithLabelValues("bar").Add(42) |
| 51 |
counterVecWithTwoElements.WithLabelValues("baz").Inc() |
| 52 |
|
| 53 |
histogramVecWithOneElement := prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{"foo"}) |
| 54 |
histogramVecWithOneElement.WithLabelValues("bar").Observe(2.7) |
| 55 |
|
| 56 |
scenarios := map[string]struct { |
| 57 |
collector prometheus.Collector |
| 58 |
panics bool |
| 59 |
want float64 |
| 60 |
}{ |
| 61 |
"simple counter": { |
| 62 |
collector: prometheus.NewCounter(prometheus.CounterOpts{}), |
| 63 |
panics: false, |
| 64 |
want: 0, |
| 65 |
}, |
| 66 |
"simple gauge": { |
| 67 |
collector: prometheus.NewGauge(prometheus.GaugeOpts{}), |
| 68 |
panics: false, |
| 69 |
want: 0, |
| 70 |
}, |
| 71 |
"simple untyped": { |
| 72 |
collector: untypedCollector{}, |
| 73 |
panics: false, |
| 74 |
want: 2001, |
| 75 |
}, |
| 76 |
"simple histogram": { |
| 77 |
collector: prometheus.NewHistogram(prometheus.HistogramOpts{}), |
| 78 |
panics: true, |
| 79 |
}, |
| 80 |
"simple summary": { |
| 81 |
collector: prometheus.NewSummary(prometheus.SummaryOpts{}), |
| 82 |
panics: true, |
| 83 |
}, |
| 84 |
"simple gauge with an actual value set": { |
| 85 |
collector: gaugeWithAValueSet, |
| 86 |
panics: false, |
| 87 |
want: 3.14, |
| 88 |
}, |
| 89 |
"counter vec with zero elements": { |
| 90 |
collector: prometheus.NewCounterVec(prometheus.CounterOpts{}, nil), |
| 91 |
panics: true, |
| 92 |
}, |
| 93 |
"counter vec with one element": { |
| 94 |
collector: counterVecWithOneElement, |
| 95 |
panics: false, |
| 96 |
want: 1, |
| 97 |
}, |
| 98 |
"counter vec with two elements": { |
| 99 |
collector: counterVecWithTwoElements, |
| 100 |
panics: true, |
| 101 |
}, |
| 102 |
"histogram vec with one element": { |
| 103 |
collector: histogramVecWithOneElement, |
| 104 |
panics: true, |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
for n, s := range scenarios { |
| 109 |
t.Run(n, func(t *testing.T) { |
| 110 |
defer func() { |
| 111 |
r := recover() |
| 112 |
if r == nil && s.panics { |
| 113 |
t.Error("expected panic") |
| 114 |
} else if r != nil && !s.panics { |
| 115 |
t.Error("unexpected panic: ", r) |
| 116 |
} |
| 117 |
// Any other combination is the expected outcome. |
| 118 |
}() |
| 119 |
if got := ToFloat64(s.collector); got != s.want { |
| 120 |
t.Errorf("want %f, got %f", s.want, got) |
| 121 |
} |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestCollectAndCompare(t *testing.T) { |
| 127 |
const metadata = ` |
| 128 |
# HELP some_total A value that represents a counter. |
| 129 |
# TYPE some_total counter |
| 130 |
` |
| 131 |
|
| 132 |
c := prometheus.NewCounter(prometheus.CounterOpts{ |
| 133 |
Name: "some_total", |
| 134 |
Help: "A value that represents a counter.", |
| 135 |
ConstLabels: prometheus.Labels{ |
| 136 |
"label1": "value1", |
| 137 |
}, |
| 138 |
}) |
| 139 |
c.Inc() |
| 140 |
|
| 141 |
expected := ` |
| 142 |
|
| 143 |
some_total{ label1 = "value1" } 1 |
| 144 |
` |
| 145 |
|
| 146 |
if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil { |
| 147 |
t.Errorf("unexpected collecting result:\n%s", err) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestCollectAndCompareNoLabel(t *testing.T) { |
| 152 |
const metadata = ` |
| 153 |
# HELP some_total A value that represents a counter. |
| 154 |
# TYPE some_total counter |
| 155 |
` |
| 156 |
|
| 157 |
c := prometheus.NewCounter(prometheus.CounterOpts{ |
| 158 |
Name: "some_total", |
| 159 |
Help: "A value that represents a counter.", |
| 160 |
}) |
| 161 |
c.Inc() |
| 162 |
|
| 163 |
expected := ` |
| 164 |
|
| 165 |
some_total 1 |
| 166 |
` |
| 167 |
|
| 168 |
if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil { |
| 169 |
t.Errorf("unexpected collecting result:\n%s", err) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestCollectAndCompareNoHelp(t *testing.T) { |
| 174 |
const metadata = ` |
| 175 |
# TYPE some_total counter |
| 176 |
` |
| 177 |
|
| 178 |
c := prometheus.NewCounter(prometheus.CounterOpts{ |
| 179 |
Name: "some_total", |
| 180 |
}) |
| 181 |
c.Inc() |
| 182 |
|
| 183 |
expected := ` |
| 184 |
|
| 185 |
some_total 1 |
| 186 |
` |
| 187 |
|
| 188 |
if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil { |
| 189 |
t.Errorf("unexpected collecting result:\n%s", err) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func TestCollectAndCompareHistogram(t *testing.T) { |
| 194 |
inputs := []struct { |
| 195 |
name string |
| 196 |
c prometheus.Collector |
| 197 |
metadata string |
| 198 |
expect string |
| 199 |
observation float64 |
| 200 |
}{ |
| 201 |
{ |
| 202 |
name: "Testing Histogram Collector", |
| 203 |
c: prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 204 |
Name: "some_histogram", |
| 205 |
Help: "An example of a histogram", |
| 206 |
Buckets: []float64{1, 2, 3}, |
| 207 |
}), |
| 208 |
metadata: ` |
| 209 |
# HELP some_histogram An example of a histogram |
| 210 |
# TYPE some_histogram histogram |
| 211 |
`, |
| 212 |
expect: ` |
| 213 |
some_histogram{le="1"} 0 |
| 214 |
some_histogram{le="2"} 0 |
| 215 |
some_histogram{le="3"} 1 |
| 216 |
some_histogram_bucket{le="+Inf"} 1 |
| 217 |
some_histogram_sum 2.5 |
| 218 |
some_histogram_count 1 |
| 219 |
|
| 220 |
`, |
| 221 |
observation: 2.5, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "Testing HistogramVec Collector", |
| 225 |
c: prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 226 |
Name: "some_histogram", |
| 227 |
Help: "An example of a histogram", |
| 228 |
Buckets: []float64{1, 2, 3}, |
| 229 |
}, []string{"test"}), |
| 230 |
|
| 231 |
metadata: ` |
| 232 |
# HELP some_histogram An example of a histogram |
| 233 |
# TYPE some_histogram histogram |
| 234 |
`, |
| 235 |
expect: ` |
| 236 |
some_histogram_bucket{test="test",le="1"} 0 |
| 237 |
some_histogram_bucket{test="test",le="2"} 0 |
| 238 |
some_histogram_bucket{test="test",le="3"} 1 |
| 239 |
some_histogram_bucket{test="test",le="+Inf"} 1 |
| 240 |
some_histogram_sum{test="test"} 2.5 |
| 241 |
some_histogram_count{test="test"} 1 |
| 242 |
|
| 243 |
`, |
| 244 |
observation: 2.5, |
| 245 |
}, |
| 246 |
} |
| 247 |
|
| 248 |
for _, input := range inputs { |
| 249 |
switch collector := input.c.(type) { |
| 250 |
case prometheus.Histogram: |
| 251 |
collector.Observe(input.observation) |
| 252 |
case *prometheus.HistogramVec: |
| 253 |
collector.WithLabelValues("test").Observe(input.observation) |
| 254 |
default: |
| 255 |
t.Fatalf("unsupported collector tested") |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
t.Run(input.name, func(t *testing.T) { |
| 260 |
if err := CollectAndCompare(input.c, strings.NewReader(input.metadata+input.expect)); err != nil { |
| 261 |
t.Errorf("unexpected collecting result:\n%s", err) |
| 262 |
} |
| 263 |
}) |
| 264 |
|
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func TestNoMetricFilter(t *testing.T) { |
| 269 |
const metadata = ` |
| 270 |
# HELP some_total A value that represents a counter. |
| 271 |
# TYPE some_total counter |
| 272 |
` |
| 273 |
|
| 274 |
c := prometheus.NewCounter(prometheus.CounterOpts{ |
| 275 |
Name: "some_total", |
| 276 |
Help: "A value that represents a counter.", |
| 277 |
ConstLabels: prometheus.Labels{ |
| 278 |
"label1": "value1", |
| 279 |
}, |
| 280 |
}) |
| 281 |
c.Inc() |
| 282 |
|
| 283 |
expected := ` |
| 284 |
some_total{label1="value1"} 1 |
| 285 |
` |
| 286 |
|
| 287 |
if err := CollectAndCompare(c, strings.NewReader(metadata+expected)); err != nil { |
| 288 |
t.Errorf("unexpected collecting result:\n%s", err) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func TestMetricNotFound(t *testing.T) { |
| 293 |
const metadata = ` |
| 294 |
# HELP some_other_metric A value that represents a counter. |
| 295 |
# TYPE some_other_metric counter |
| 296 |
` |
| 297 |
|
| 298 |
c := prometheus.NewCounter(prometheus.CounterOpts{ |
| 299 |
Name: "some_total", |
| 300 |
Help: "A value that represents a counter.", |
| 301 |
ConstLabels: prometheus.Labels{ |
| 302 |
"label1": "value1", |
| 303 |
}, |
| 304 |
}) |
| 305 |
|
| 306 |
c.Inc() |
| 307 |
|
| 308 |
expected := ` |
| 309 |
some_other_metric{label1="value1"} 1 |
| 310 |
` |
| 311 |
|
| 312 |
expectedError := `-# HELP some_total A value that represents a counter. |
| 313 |
-# TYPE some_total counter |
| 314 |
-some_total{label1="value1"} 1 |
| 315 |
+# HELP some_other_metric A value that represents a counter. |
| 316 |
+# TYPE some_other_metric counter |
| 317 |
+some_other_metric{label1="value1"} 1 |
| 318 |
` |
| 319 |
|
| 320 |
err := CollectAndCompare(c, strings.NewReader(metadata+expected)) |
| 321 |
if err == nil { |
| 322 |
t.Error("Expected error, got no error.") |
| 323 |
} |
| 324 |
|
| 325 |
if err.Error() != expectedError { |
| 326 |
t.Errorf("Expected\n%#+v\nGot:\n%#+v", expectedError, err.Error()) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func TestScrapeAndCompare(t *testing.T) { |
| 331 |
const expected = ` |
| 332 |
# HELP some_total A value that represents a counter. |
| 333 |
# TYPE some_total counter |
| 334 |
|
| 335 |
some_total{ label1 = "value1" } 1 |
| 336 |
` |
| 337 |
|
| 338 |
expectedReader := strings.NewReader(expected) |
| 339 |
|
| 340 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 341 |
fmt.Fprintln(w, expected) |
| 342 |
})) |
| 343 |
defer ts.Close() |
| 344 |
|
| 345 |
if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total"); err != nil { |
| 346 |
t.Errorf("unexpected scraping result:\n%s", err) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestScrapeAndCompareWithMultipleExpected(t *testing.T) { |
| 351 |
const expected = ` |
| 352 |
# HELP some_total A value that represents a counter. |
| 353 |
# TYPE some_total counter |
| 354 |
|
| 355 |
some_total{ label1 = "value1" } 1 |
| 356 |
|
| 357 |
# HELP some_total2 A value that represents a counter. |
| 358 |
# TYPE some_total2 counter |
| 359 |
|
| 360 |
some_total2{ label2 = "value2" } 1 |
| 361 |
` |
| 362 |
|
| 363 |
expectedReader := strings.NewReader(expected) |
| 364 |
|
| 365 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 366 |
fmt.Fprintln(w, expected) |
| 367 |
})) |
| 368 |
defer ts.Close() |
| 369 |
|
| 370 |
if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total2"); err != nil { |
| 371 |
t.Errorf("unexpected scraping result:\n%s", err) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestScrapeAndCompareFetchingFail(t *testing.T) { |
| 376 |
err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total") |
| 377 |
if err == nil { |
| 378 |
t.Errorf("expected an error but got nil") |
| 379 |
} |
| 380 |
if !strings.HasPrefix(err.Error(), "scraping metrics failed") { |
| 381 |
t.Errorf("unexpected error happened: %s", err) |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
func TestScrapeAndCompareBadStatusCode(t *testing.T) { |
| 386 |
const expected = ` |
| 387 |
# HELP some_total A value that represents a counter. |
| 388 |
# TYPE some_total counter |
| 389 |
|
| 390 |
some_total{ label1 = "value1" } 1 |
| 391 |
` |
| 392 |
|
| 393 |
expectedReader := strings.NewReader(expected) |
| 394 |
|
| 395 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 396 |
w.WriteHeader(http.StatusBadGateway) |
| 397 |
fmt.Fprintln(w, expected) |
| 398 |
})) |
| 399 |
defer ts.Close() |
| 400 |
|
| 401 |
err := ScrapeAndCompare(ts.URL, expectedReader, "some_total") |
| 402 |
if err == nil { |
| 403 |
t.Errorf("expected an error but got nil") |
| 404 |
} |
| 405 |
if !strings.HasPrefix(err.Error(), "the scraping target returned a status code other than 200") { |
| 406 |
t.Errorf("unexpected error happened: %s", err) |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
func TestCollectAndCount(t *testing.T) { |
| 411 |
c := prometheus.NewCounterVec( |
| 412 |
prometheus.CounterOpts{ |
| 413 |
Name: "some_total", |
| 414 |
Help: "A value that represents a counter.", |
| 415 |
}, |
| 416 |
[]string{"foo"}, |
| 417 |
) |
| 418 |
if got, want := CollectAndCount(c), 0; got != want { |
| 419 |
t.Errorf("unexpected metric count, got %d, want %d", got, want) |
| 420 |
} |
| 421 |
c.WithLabelValues("bar") |
| 422 |
if got, want := CollectAndCount(c), 1; got != want { |
| 423 |
t.Errorf("unexpected metric count, got %d, want %d", got, want) |
| 424 |
} |
| 425 |
c.WithLabelValues("baz") |
| 426 |
if got, want := CollectAndCount(c), 2; got != want { |
| 427 |
t.Errorf("unexpected metric count, got %d, want %d", got, want) |
| 428 |
} |
| 429 |
if got, want := CollectAndCount(c, "some_total"), 2; got != want { |
| 430 |
t.Errorf("unexpected metric count, got %d, want %d", got, want) |
| 431 |
} |
| 432 |
if got, want := CollectAndCount(c, "some_other_total"), 0; got != want { |
| 433 |
t.Errorf("unexpected metric count, got %d, want %d", got, want) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
func TestCollectAndFormat(t *testing.T) { |
| 438 |
const expected = `# HELP foo_bar A value that represents the number of bars in foo. |
| 439 |
# TYPE foo_bar counter |
| 440 |
foo_bar{fizz="bang"} 1 |
| 441 |
` |
| 442 |
c := prometheus.NewCounterVec( |
| 443 |
prometheus.CounterOpts{ |
| 444 |
Name: "foo_bar", |
| 445 |
Help: "A value that represents the number of bars in foo.", |
| 446 |
}, |
| 447 |
[]string{"fizz"}, |
| 448 |
) |
| 449 |
c.WithLabelValues("bang").Inc() |
| 450 |
|
| 451 |
got, err := CollectAndFormat(c, expfmt.TypeTextPlain, "foo_bar") |
| 452 |
if err != nil { |
| 453 |
t.Errorf("unexpected error: %s", err.Error()) |
| 454 |
} |
| 455 |
|
| 456 |
gotS := string(got) |
| 457 |
if err != nil { |
| 458 |
t.Errorf("unexpected error: %s", err.Error()) |
| 459 |
} |
| 460 |
|
| 461 |
if gotS != expected { |
| 462 |
t.Errorf("unexpected metric output, got %q, expected %q", gotS, expected) |
| 463 |
} |
| 464 |
} |