client_golang/prometheus/testutil
leonnicolas c7c7509669
testutil compareMetricFamilies: make less error-prone (#1424)
* 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>
2024-05-15 10:50:24 +01:00
..
diff feat(dependency): replace go-spew package (#1499) 2024-04-22 06:49:49 +00:00
promlint Add LintDuplicateMetric to promlint 2024-03-16 12:12:08 +00:00
lint.go fix assorted oddities found by golangci-lint (#1040) 2022-08-03 06:30:51 +02:00
lint_test.go Improve various comments 2020-04-25 15:59:53 +02:00
testutil.go testutil compareMetricFamilies: make less error-prone (#1424) 2024-05-15 10:50:24 +01:00
testutil_test.go testutil compareMetricFamilies: make less error-prone (#1424) 2024-05-15 10:50:24 +01:00