Skip to content

Commit 4d8144c

Browse files
committed
Add test for the new promhttp_metric_handler_errors_total metric
Signed-off-by: beorn7 <[email protected]>
1 parent 3fa9fca commit 4d8144c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

prometheus/promhttp/http_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func (b blockingCollector) Collect(ch chan<- prometheus.Metric) {
5959
func TestHandlerErrorHandling(t *testing.T) {
6060

6161
// Create a registry that collects a MetricFamily with two elements,
62-
// another with one, and reports an error.
62+
// another with one, and reports an error. Further down, we'll use the
63+
// same registry in the HandlerOpts.
6364
reg := prometheus.NewRegistry()
6465

6566
cnt := prometheus.NewCounter(prometheus.CounterOpts{
@@ -92,25 +93,47 @@ func TestHandlerErrorHandling(t *testing.T) {
9293
errorHandler := HandlerFor(reg, HandlerOpts{
9394
ErrorLog: logger,
9495
ErrorHandling: HTTPErrorOnError,
96+
Registry: reg,
9597
})
9698
continueHandler := HandlerFor(reg, HandlerOpts{
9799
ErrorLog: logger,
98100
ErrorHandling: ContinueOnError,
101+
Registry: reg,
99102
})
100103
panicHandler := HandlerFor(reg, HandlerOpts{
101104
ErrorLog: logger,
102105
ErrorHandling: PanicOnError,
106+
Registry: reg,
103107
})
104108
wantMsg := `error gathering metrics: error collecting metric Desc{fqName: "invalid_metric", help: "not helpful", constLabels: {}, variableLabels: []}: collect error
105109
`
106110
wantErrorBody := `An error has occurred while serving metrics:
107111
108112
error collecting metric Desc{fqName: "invalid_metric", help: "not helpful", constLabels: {}, variableLabels: []}: collect error
109113
`
110-
wantOKBody := `# HELP name docstring
114+
wantOKBody1 := `# HELP name docstring
111115
# TYPE name counter
112116
name{constname="constvalue",labelname="val1"} 1
113117
name{constname="constvalue",labelname="val2"} 1
118+
# HELP promhttp_metric_handler_errors_total Total number of internal errors encountered by the promhttp metric handler.
119+
# TYPE promhttp_metric_handler_errors_total counter
120+
promhttp_metric_handler_errors_total{cause="encoding"} 0
121+
promhttp_metric_handler_errors_total{cause="gathering"} 1
122+
# HELP the_count Ah-ah-ah! Thunder and lightning!
123+
# TYPE the_count counter
124+
the_count 0
125+
`
126+
// It might happen that counting the gathering error makes it to the
127+
// promhttp_metric_handler_errors_total counter before it is gathered
128+
// itself. Thus, we have to bodies that are acceptable for the test.
129+
wantOKBody2 := `# HELP name docstring
130+
# TYPE name counter
131+
name{constname="constvalue",labelname="val1"} 1
132+
name{constname="constvalue",labelname="val2"} 1
133+
# HELP promhttp_metric_handler_errors_total Total number of internal errors encountered by the promhttp metric handler.
134+
# TYPE promhttp_metric_handler_errors_total counter
135+
promhttp_metric_handler_errors_total{cause="encoding"} 0
136+
promhttp_metric_handler_errors_total{cause="gathering"} 2
114137
# HELP the_count Ah-ah-ah! Thunder and lightning!
115138
# TYPE the_count counter
116139
the_count 0
@@ -137,8 +160,8 @@ the_count 0
137160
if got := logBuf.String(); got != wantMsg {
138161
t.Errorf("got log message %q, want %q", got, wantMsg)
139162
}
140-
if got := writer.Body.String(); got != wantOKBody {
141-
t.Errorf("got body %q, want %q", got, wantOKBody)
163+
if got := writer.Body.String(); got != wantOKBody1 && got != wantOKBody2 {
164+
t.Errorf("got body %q, want either %q or %q", got, wantOKBody1, wantOKBody2)
142165
}
143166

144167
defer func() {

0 commit comments

Comments
 (0)