diff options
Diffstat (limited to 'vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go')
-rw-r--r-- | vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go b/vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go index cea5a90..b0ee467 100644 --- a/vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go +++ b/vendor/github.com/prometheus/client_golang/prometheus/promhttp/http.go | |||
@@ -144,7 +144,12 @@ func HandlerFor(reg prometheus.Gatherer, opts HandlerOpts) http.Handler { | |||
144 | } | 144 | } |
145 | } | 145 | } |
146 | 146 | ||
147 | contentType := expfmt.Negotiate(req.Header) | 147 | var contentType expfmt.Format |
148 | if opts.EnableOpenMetrics { | ||
149 | contentType = expfmt.NegotiateIncludingOpenMetrics(req.Header) | ||
150 | } else { | ||
151 | contentType = expfmt.Negotiate(req.Header) | ||
152 | } | ||
148 | header := rsp.Header() | 153 | header := rsp.Header() |
149 | header.Set(contentTypeHeader, string(contentType)) | 154 | header.Set(contentTypeHeader, string(contentType)) |
150 | 155 | ||
@@ -163,22 +168,38 @@ func HandlerFor(reg prometheus.Gatherer, opts HandlerOpts) http.Handler { | |||
163 | enc := expfmt.NewEncoder(w, contentType) | 168 | enc := expfmt.NewEncoder(w, contentType) |
164 | 169 | ||
165 | var lastErr error | 170 | var lastErr error |
171 | |||
172 | // handleError handles the error according to opts.ErrorHandling | ||
173 | // and returns true if we have to abort after the handling. | ||
174 | handleError := func(err error) bool { | ||
175 | if err == nil { | ||
176 | return false | ||
177 | } | ||
178 | lastErr = err | ||
179 | if opts.ErrorLog != nil { | ||
180 | opts.ErrorLog.Println("error encoding and sending metric family:", err) | ||
181 | } | ||
182 | errCnt.WithLabelValues("encoding").Inc() | ||
183 | switch opts.ErrorHandling { | ||
184 | case PanicOnError: | ||
185 | panic(err) | ||
186 | case HTTPErrorOnError: | ||
187 | httpError(rsp, err) | ||
188 | return true | ||
189 | } | ||
190 | // Do nothing in all other cases, including ContinueOnError. | ||
191 | return false | ||
192 | } | ||
193 | |||
166 | for _, mf := range mfs { | 194 | for _, mf := range mfs { |
167 | if err := enc.Encode(mf); err != nil { | 195 | if handleError(enc.Encode(mf)) { |
168 | lastErr = err | 196 | return |
169 | if opts.ErrorLog != nil { | 197 | } |
170 | opts.ErrorLog.Println("error encoding and sending metric family:", err) | 198 | } |
171 | } | 199 | if closer, ok := enc.(expfmt.Closer); ok { |
172 | errCnt.WithLabelValues("encoding").Inc() | 200 | // This in particular takes care of the final "# EOF\n" line for OpenMetrics. |
173 | switch opts.ErrorHandling { | 201 | if handleError(closer.Close()) { |
174 | case PanicOnError: | 202 | return |
175 | panic(err) | ||
176 | case ContinueOnError: | ||
177 | // Handled later. | ||
178 | case HTTPErrorOnError: | ||
179 | httpError(rsp, err) | ||
180 | return | ||
181 | } | ||
182 | } | 203 | } |
183 | } | 204 | } |
184 | 205 | ||
@@ -318,6 +339,16 @@ type HandlerOpts struct { | |||
318 | // away). Until the implementation is improved, it is recommended to | 339 | // away). Until the implementation is improved, it is recommended to |
319 | // implement a separate timeout in potentially slow Collectors. | 340 | // implement a separate timeout in potentially slow Collectors. |
320 | Timeout time.Duration | 341 | Timeout time.Duration |
342 | // If true, the experimental OpenMetrics encoding is added to the | ||
343 | // possible options during content negotiation. Note that Prometheus | ||
344 | // 2.5.0+ will negotiate OpenMetrics as first priority. OpenMetrics is | ||
345 | // the only way to transmit exemplars. However, the move to OpenMetrics | ||
346 | // is not completely transparent. Most notably, the values of "quantile" | ||
347 | // labels of Summaries and "le" labels of Histograms are formatted with | ||
348 | // a trailing ".0" if they would otherwise look like integer numbers | ||
349 | // (which changes the identity of the resulting series on the Prometheus | ||
350 | // server). | ||
351 | EnableOpenMetrics bool | ||
321 | } | 352 | } |
322 | 353 | ||
323 | // gzipAccepted returns whether the client will accept gzip-encoded content. | 354 | // gzipAccepted returns whether the client will accept gzip-encoded content. |