-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
After #1143, the function testutil.GatherAndCompare:
client_golang/prometheus/testutil/testutil.go
Lines 197 to 203 in 1bae6c1
| // GatherAndCompare gathers all metrics from the provided Gatherer and compares | |
| // it to an expected output read from the provided Reader in the Prometheus text | |
| // exposition format. If any metricNames are provided, only metrics with those | |
| // names are compared. | |
| func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...string) error { | |
| return TransactionalGatherAndCompare(prometheus.ToTransactionalGatherer(g), expected, metricNames...) | |
| } |
client_golang/prometheus/testutil/testutil.go
Lines 205 to 222 in 1bae6c1
| // TransactionalGatherAndCompare gathers all metrics from the provided Gatherer and compares | |
| // it to an expected output read from the provided Reader in the Prometheus text | |
| // exposition format. If any metricNames are provided, only metrics with those | |
| // names are compared. | |
| func TransactionalGatherAndCompare(g prometheus.TransactionalGatherer, expected io.Reader, metricNames ...string) error { | |
| got, done, err := g.Gather() | |
| defer done() | |
| if err != nil { | |
| return fmt.Errorf("gathering metrics failed: %w", err) | |
| } | |
| wanted, err := convertReaderToMetricFamily(expected) | |
| if err != nil { | |
| return err | |
| } | |
| return compareMetricFamilies(got, wanted, metricNames...) | |
| } |
client_golang/prometheus/testutil/testutil.go
Lines 236 to 245 in 1bae6c1
| // compareMetricFamilies would compare 2 slices of metric families, and optionally filters both of | |
| // them to the `metricNames` provided. | |
| func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...string) error { | |
| if metricNames != nil { | |
| got = filterMetrics(got, metricNames) | |
| expected = filterMetrics(expected, metricNames) | |
| } | |
| return compare(got, expected) | |
| } |
became error-prone, as we filter the expected metrics in compareMetricFamilies as well now, if we make a mistake in one of the arguments expected or metricNames, the function will no longer return an error.
If e.g. we make a mistake in the name of the metric (provide a metric that is not exported because of a typo or re-using a variable containing the wrong metric name), the function will return nil.
Or e.g. if we provide an expected with the wrong metrics to a faulty code, we could never be aware of that.
I think expected lost its safeguard/source of truth utility.
I expect the user/tester to have full control on expected, I don't see why one couldn't provide the exact series they expect.
Plus having "dynamic" expected makes the tests hard to grasp and debug. No one needs tests with too many logic/intelligence that needs to be tested as well :) Way beneficial when they are more explicit even if this result in a little bit more code.
Looking at the code that was justifying the change:
it's still not making use of the change yet, but I think they can easily split their expected and pass it as a map or sth to their test function...
Discovered this while adding a test for kubernetes etcd metrics: kubernetes/kubernetes#120827. I copied/pasted a test function and the test was passing because I forgot to change the metric name.