Remove a dependency by using go-cmp for `testutil.CollectAndCompare()`.
Note that the string output of `cmp.Diff()` is non-stable, so users
depending on stable `error` strings will nolonger work.
Signed-off-by: SuperQ <superq@gmail.com>
* testutil compareMetricFamilies: make less error-prone
The functions `GatherAndCompare`, `ScrapeAndCompare` and others that use
`compareMetricFamilies` under the hood can return no error if
`metricNames` includes none of the names found in the scraped/gathered
results. To avoid false Positves (an error being the negative case), we
can return an error if there is is at least one name in `metricNames`
that is not in the filtered results.
Fixes: https://github.com/prometheus/client_golang/issues/1351
Signed-off-by: leonnicolas <leonloechner@gmx.de>
* Add missing metricNames to error
In to see which metric names are missing, we can add them to the error
message.
Signed-off-by: leonnicolas <leonloechner@gmx.de>
* Apply suggestions from code review
- remove if nil check
- use two nested loops instead of map
- use new function `hasMetricByName` for readability
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
Signed-off-by: leonnicolas <60091705+leonnicolas@users.noreply.github.com>
* prometheus/testutil/testutil_test.go: compare complete error
Before we would only compare the error prefix in `TestScrapeAndCompare`.
Signed-off-by: leonnicolas <leonloechner@gmx.de>
---------
Signed-off-by: leonnicolas <leonloechner@gmx.de>
Signed-off-by: leonnicolas <60091705+leonnicolas@users.noreply.github.com>
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
CollectAndFormat is a helper function that returns the formatted metrics
to the caller, allowing them to use it how they want. This is different
to CollectAndCompare where the comparison is done strictly on behalf of
the caller. Often it is more convenient to perform a simple substring
match on the formatted metric.
Signed-off-by: Jack Cassidy <j.cassidy45@gmail.com>
Change the lintMetricTypeInName linter inside promlint to only trigger an error when the metric name matches the type of the metric.
Signed-off-by: foehammer127 <foehammer@disroot.org>
Due to an inconsistency in the text protocol between encoding and
decoding, it was not possible to use the testutil.CollectAndCompare
function to test metrics with empty help values. To fix this, normalize
empty help values from the expected/want side of the test so that they
compare correctly with empty values on the actual/got side of the test.
Signed-off-by: Billy Keyes <bluekeyes@gmail.com>
* Refactor promlint validation structure
Signed-off-by: João Vilaça <jvilaca@redhat.com>
* Add support for custom validations in promlint
Signed-off-by: João Vilaça <jvilaca@redhat.com>
* Keep backwards compatibility
Signed-off-by: João Vilaça <jvilaca@redhat.com>
---------
Signed-off-by: João Vilaça <jvilaca@redhat.com>
We tested this function runtime in both cases using "testing",
and the runtime for this pr is much shorter.
Signed-off-by: alitman <alitman@redhat.com>
* testutil: Add ScrapeAndCompare
Signed-off-by: sazary <soroosh@azary.ir>
* testutil: Use %w verb wherever we're using an error in fmt.Errorf
Signed-off-by: sazary <soroosh@azary.ir>
* Format
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@users.noreply.github.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@gmail.com>
* fix assorted oddities found by golangci-lint
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
* permanently enable the linters
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
* post-rebase blues
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
Now that we have also added CollectAndLint and GatherAndLint, I
thought we should bring CollectAndCount in line. So:
- Add GatherAndCount.
- Add filtering by metric name.
- Add tests.
Minor wart: CollectAndCount should now return `(int, error)`, but that
would be a breaking change as the current version just returns
`int`. I decided to let the new version panic when it should return an
error. An error is anyway very unlikely, so the biggest annoyance here
is really just the inconsistency.
Signed-off-by: beorn7 <beorn@grafana.com>
Also, change all the `dto.MetricFamily` arguments to pointers to be
more consistent with what we do in client_golang in general.
Signed-off-by: beorn7 <beorn@grafana.com>
reflect.DeepEqual is not suitable for zero occurrences of repeated
proto messages. This changes the comparison to act on the string
representation of proto messages.
Signed-off-by: beorn7 <beorn@soundcloud.com>
This is for types we don't want to export but which are used in
different packages within client_golang.
Currently, that's only NormalizeMetricFamilies (used in the prometheus
package and in the testutil package). More to be added as needed.
Signed-off-by: beorn7 <beorn@soundcloud.com>
- Expected text format is now read from an io.Reader.
- Metrics are gathered from a Gatherer.
- Added a convenience wrapper to collect from a Collector.
Signed-off-by: beorn7 <beorn@soundcloud.com>
`testutil` is more in line with stdlib naming conventions.
The package should be below `prometheus` as it only provides utils to
test exposition code, not to test HTTP client code.
Signed-off-by: beorn7 <beorn@soundcloud.com>