@@ -59,7 +59,8 @@ func (b blockingCollector) Collect(ch chan<- prometheus.Metric) {
59
59
func TestHandlerErrorHandling (t * testing.T ) {
60
60
61
61
// 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.
63
64
reg := prometheus .NewRegistry ()
64
65
65
66
cnt := prometheus .NewCounter (prometheus.CounterOpts {
@@ -92,25 +93,47 @@ func TestHandlerErrorHandling(t *testing.T) {
92
93
errorHandler := HandlerFor (reg , HandlerOpts {
93
94
ErrorLog : logger ,
94
95
ErrorHandling : HTTPErrorOnError ,
96
+ Registry : reg ,
95
97
})
96
98
continueHandler := HandlerFor (reg , HandlerOpts {
97
99
ErrorLog : logger ,
98
100
ErrorHandling : ContinueOnError ,
101
+ Registry : reg ,
99
102
})
100
103
panicHandler := HandlerFor (reg , HandlerOpts {
101
104
ErrorLog : logger ,
102
105
ErrorHandling : PanicOnError ,
106
+ Registry : reg ,
103
107
})
104
108
wantMsg := `error gathering metrics: error collecting metric Desc{fqName: "invalid_metric", help: "not helpful", constLabels: {}, variableLabels: []}: collect error
105
109
`
106
110
wantErrorBody := `An error has occurred while serving metrics:
107
111
108
112
error collecting metric Desc{fqName: "invalid_metric", help: "not helpful", constLabels: {}, variableLabels: []}: collect error
109
113
`
110
- wantOKBody := `# HELP name docstring
114
+ wantOKBody1 := `# HELP name docstring
111
115
# TYPE name counter
112
116
name{constname="constvalue",labelname="val1"} 1
113
117
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
114
137
# HELP the_count Ah-ah-ah! Thunder and lightning!
115
138
# TYPE the_count counter
116
139
the_count 0
@@ -137,8 +160,8 @@ the_count 0
137
160
if got := logBuf .String (); got != wantMsg {
138
161
t .Errorf ("got log message %q, want %q" , got , wantMsg )
139
162
}
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 )
142
165
}
143
166
144
167
defer func () {
0 commit comments