Commit Graph

26 Commits

Author SHA1 Message Date
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
Jack Cassidy 36b9f46811
add: CollectAndFormat to testutil (#1503)
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>
2024-05-09 15:08:21 +00:00
dongjiang 2f59eb2687
feat(dependency): replace go-spew package (#1499)
* remove go-spew package

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* fix gofumpt lint

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* fix unittest unicode

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* dongjiang, change go-cmp to reflect

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* fix lint

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* Revert "dongjiang, change go-cmp to reflect"

This reverts commit bfbe25e926.

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* change diff func

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* dongjiang, update diff package

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* update testutil

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

* Fix imports, goimports friendly

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

---------

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Co-authored-by: Kemal Akkoyun <kakkoyun@gmail.com>
2024-04-22 06:49:49 +00:00
Owen Williams 6d039205b8 deps: bump prometheus/common version
Fixes https://github.com/prometheus/client_golang/issues/1446

Signed-off-by: Owen Williams <owen.williams@grafana.com>
2024-02-23 13:28:17 -05:00
Billy Keyes 80d3f0b5b3
Normalize empty help values in CollectAndCompare (#1378)
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>
2023-11-20 09:57:30 +00:00
Daria Bialobrzeska eb339714f5
Filter expected metrics as well in CollectAndCompare (#1143)
* Filter expected metrics as well in CollectAndCompare

Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

* Add testcase for multiple expected metrics
Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

* Change test values for filtering multiple expected metrics
Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

---------

Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>
2023-03-22 10:45:45 +00:00
Soroosh Azary Marhabi 1638da9ae4
testutil: Add ScrapeAndCompare (#1043)
* 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>
2022-08-05 16:27:47 +02:00
Christoph Mewes 618194de6a
fix assorted oddities found by golangci-lint (#1040)
* 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>
2022-08-03 06:30:51 +02:00
Soroosh Azary Marhabi 2cfd1eb960
Enable same linters as the Prometheus repo itself (#1056)
* Add gofumpt to github workflow & fix all files for it

Signed-off-by: sazary <soroosh@azary.ir>

* Add goimports to golangci & fix it's issues

Signed-off-by: sazary <soroosh@azary.ir>

* Add revive to golangci & fix it's issues

Signed-off-by: sazary <soroosh@azary.ir>

* Add errcheck & misspell to golangci and fix their issues

Signed-off-by: sazary <soroosh@azary.ir>

* Add govet & gosimple to golangci and fix their issues

Signed-off-by: sazary <soroosh@azary.ir>

* Enable all default linters of golangci

Signed-off-by: sazary <soroosh@azary.ir>
2022-06-17 09:04:06 +02:00
Sourik Ghosh cd90f33be8
smart diff to testutil.GatherAndCompare (#998)
* added smart diff to testutil.GatherAndCompare

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* v2 testuitls added for better diff

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* replaced CollectAndCompare to CollectAndCompareV2 in testutil_test

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* renamed methods from v2 to withT

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* replaced testify with custom diff func

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* difflib GetUnifiedDiffString added with test

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* license meta data added to file

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>

* moved difflib to internal

Signed-off-by: Sourik Ghosh <sourikghosh31@gmail.com>
2022-04-13 18:38:05 +01:00
Bartlomiej Plotka 1f81b3e913
Added Transactional Gatherer allowed cached solutions (#989)
* Added cached collector.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

update.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Attempt 2

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Added blocking registry, with raw collector and transactional handler.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Added fast path to normal (empty) registry to save 8 allocs and 3K5B per Gather.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Simplified API, added tests.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Fix.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Simplified implementation.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Added benchmark.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

Optimized.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Optimization attempt.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Revert "Optimization attempt."

This reverts commit 2fcaf51be9.

Optimization was not worth it:

 benchstat v1.txt v2.txt
name                                                           old time/op    new time/op    delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12    2.64µs ± 0%    4.05µs ± 0%   ~     (p=1.000 n=1+1)
CachedTGatherer_Update/Update_of_all_elements_with_reset-12       701ms ± 0%     358ms ± 0%   ~     (p=1.000 n=1+1)
CachedTGatherer_Update/Gather-12                                  535µs ± 0%  703934µs ± 0%   ~     (p=1.000 n=1+1)

name                                                           old alloc/op   new alloc/op   delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12      208B ± 0%      208B ± 0%   ~     (all equal)
CachedTGatherer_Update/Update_of_all_elements_with_reset-12      40.2MB ± 0%    41.1MB ± 0%   ~     (p=1.000 n=1+1)
CachedTGatherer_Update/Gather-12                                 48.6kB ± 0%    84.3kB ± 0%   ~     (p=1.000 n=1+1)

name                                                           old allocs/op  new allocs/op  delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12      3.00 ± 0%      3.00 ± 0%   ~     (all equal)
CachedTGatherer_Update/Update_of_all_elements_with_reset-12        6.00 ± 0%   4003.00 ± 0%   ~     (p=1.000 n=1+1)
CachedTGatherer_Update/Gather-12                                  1.00k ± 0%     2.01k ± 0%   ~     (p=1.000 n=1+1)

* nit.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Another optimization attempt.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* rename and further optimization.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Hopefully final optimization.

benchstat -delta-test=none v6.txt v9.txt
name                                                           old time/op    new time/op    delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12    13.1ms ± 0%     0.0ms ± 0%  -99.81%
CachedTGatherer_Update/Update_of_all_elements_with_reset-12       309ms ± 0%     282ms ± 0%   -8.77%
CachedTGatherer_Update/Gather-12                                  422ms ± 0%       0ms ± 0%  -99.95%

name                                                           old alloc/op   new alloc/op   delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12      208B ± 0%      208B ± 0%    0.00%
CachedTGatherer_Update/Update_of_all_elements_with_reset-12      2.47kB ± 0%    1.67kB ± 0%  -32.56%
CachedTGatherer_Update/Gather-12                                 52.8kB ± 0%    24.6kB ± 0%  -53.34%

name                                                           old allocs/op  new allocs/op  delta
CachedTGatherer_Update/Update_of_one_element_without_reset-12      3.00 ± 0%      3.00 ± 0%    0.00%
CachedTGatherer_Update/Update_of_all_elements_with_reset-12        0.00           0.00         0.00%
CachedTGatherer_Update/Gather-12                                  1.00k ± 0%     0.00k ± 0%  -99.60%

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Removed obsolete comment

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Fixed tests.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Removed cache.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Fixed tests.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Re-add cache.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Removed cache.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
2022-02-23 11:22:52 +00:00
beorn7 0577ef6c57 Improve CollectAndCount
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>
2020-05-14 00:17:45 +02:00
beorn7 dc79bd6093 Improve various comments
Signed-off-by: beorn7 <beorn@grafana.com>
2020-04-25 15:59:53 +02:00
beorn7 39dbb24d13 Add helper functions for linting to testutil
Signed-off-by: beorn7 <beorn@grafana.com>
2020-04-24 23:44:59 +02:00
Bartlomiej Plotka 9e9cc003f8 Added testutil.CollectAndCount
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
2020-01-09 11:20:19 +00:00
beorn7 b3d6096432 Improve testutil error messages
This also satisfy newer staticcheck versions.

Signed-off-by: beorn7 <beorn@soundcloud.com>
2019-01-06 00:22:03 +01:00
beorn7 c4c1f3461e Fix tiny punctuation issue in doc comment
Signed-off-by: beorn7 <beorn@soundcloud.com>
2018-11-15 18:40:33 +01:00
PhilipGough f9739b3d97
Compare text strings directly in testutil
Signed-off-by: PhilipGough <philip.p.gough@gmail.com>
2018-11-15 16:03:57 +00:00
beorn7 86702ea6b4 Fix metric comparison for empty labels
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>
2018-11-13 16:25:02 +01:00
beorn7 74a2f46d2c Add package documentation
Signed-off-by: beorn7 <beorn@soundcloud.com>
2018-09-04 14:02:20 +02:00
beorn7 1301cf8fcd Add helper function to extract a simple float value from a metric
Signed-off-by: beorn7 <beorn@soundcloud.com>

foo
2018-09-04 14:02:19 +02:00
beorn7 7be86f93c1 Create an internal package
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>
2018-09-03 00:18:11 +02:00
beorn7 5ba0993f6f Improved interface
- 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>
2018-09-02 21:07:38 +02:00
beorn7 154f28a316 Fix import grouping
Signed-off-by: beorn7 <beorn@soundcloud.com>
2018-08-23 00:05:02 +02:00
beorn7 e60f998e9b Make license headers consistent
Signed-off-by: beorn7 <beorn@soundcloud.com>
2018-08-22 23:59:40 +02:00
beorn7 df0210c26c Rename testutils to testutil and move below prometheus dir
`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>
2018-08-22 23:57:08 +02:00