You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While trying to write a test for a custom collector, I discovered that the testutil.CollectAndCompare function does not work with metrics that have an empty help string. The issue appears to be a discrepancy between the encoding and decoding of the text format:
This example program shows how the comparison fails regardless of whether the HELP comment appears in the expected text:
Example
package main
import (
"fmt""strings""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/testutil"
)
typeCollectorstruct{}
func (cCollector) Describe(chchan<-*prometheus.Desc) {}
func (cCollector) Collect(chchan<- prometheus.Metric) {
ch<-prometheus.MustNewConstMetric(
prometheus.NewDesc("sample_metric", "", nil, nil),
prometheus.GaugeValue,
0,
)
}
funcmain() {
withHelp:=`# HELP sample_metric# TYPE sample_metric gaugesample_metric 0`withoutHelp:=`# TYPE sample_metric gaugesample_metric 0`fmt.Println("--- With HELP ---")
fmt.Print(testutil.CollectAndCompare(Collector{}, strings.NewReader(withHelp)))
fmt.Println()
fmt.Println("--- Without HELP ---")
fmt.Print(testutil.CollectAndCompare(Collector{}, strings.NewReader(withoutHelp)))
}
My guess is that the test functions should normalize empty help fields before comparing metrics, rather than changing how encoding or decoding works. If that sounds reasonable, I can look at submitting a PR.
The text was updated successfully, but these errors were encountered:
Version: 1.16.0
While trying to write a test for a custom collector, I discovered that the
testutil.CollectAndCompare
function does not work with metrics that have an empty help string. The issue appears to be a discrepancy between the encoding and decoding of the text format:Help
to a non-nil value.Help
field is non-nil, the text encoding writes aHELP
lineHelp
field if the line only contains whitespace after the keyword.This example program shows how the comparison fails regardless of whether the
HELP
comment appears in the expected text:Example
My guess is that the test functions should normalize empty help fields before comparing metrics, rather than changing how encoding or decoding works. If that sounds reasonable, I can look at submitting a PR.
The text was updated successfully, but these errors were encountered: