From 33ed3e6fd1be1bf1ab5d4202e2beee53c2b3e39f Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Sun, 18 Feb 2024 15:29:40 -0300 Subject: [PATCH] Move OpenMetrics options to a separate struct Signed-off-by: Arthur Silva Sens --- examples/createdtimestamps/main.go | 6 +++-- examples/exemplars/main.go | 4 +++- prometheus/promhttp/http.go | 38 ++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/examples/createdtimestamps/main.go b/examples/createdtimestamps/main.go index f83b9594d..581335b03 100644 --- a/examples/createdtimestamps/main.go +++ b/examples/createdtimestamps/main.go @@ -51,8 +51,10 @@ func main() { "/metrics", promhttp.HandlerFor( registry, promhttp.HandlerOpts{ - EnableOpenMetrics: true, - EnableOpenMetricsCreatedMetrics: true, + OpenMetricsOptions: promhttp.OpenMetricsOptions{ + EnableOpenMetrics: true, + EnableOpenMetricsCreatedMetrics: true, + }, }), ) // To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics diff --git a/examples/exemplars/main.go b/examples/exemplars/main.go index 798c2dfd0..be74c383f 100644 --- a/examples/exemplars/main.go +++ b/examples/exemplars/main.go @@ -61,7 +61,9 @@ func main() { "/metrics", promhttp.HandlerFor( registry, promhttp.HandlerOpts{ - EnableOpenMetrics: true, + OpenMetricsOptions: promhttp.OpenMetricsOptions{ + EnableOpenMetrics: true, + }, }), ) // To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics diff --git a/prometheus/promhttp/http.go b/prometheus/promhttp/http.go index 1fd50aebb..1ced7170a 100644 --- a/prometheus/promhttp/http.go +++ b/prometheus/promhttp/http.go @@ -160,7 +160,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO } var contentType expfmt.Format - if opts.EnableOpenMetrics { + if opts.EnableOpenMetrics || opts.OpenMetricsOptions.EnableOpenMetrics { contentType = expfmt.NegotiateIncludingOpenMetrics(req.Header) } else { contentType = expfmt.Negotiate(req.Header) @@ -181,7 +181,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO } var enc expfmt.Encoder - if opts.EnableOpenMetricsCreatedMetrics { + if opts.OpenMetricsOptions.EnableOpenMetricsCreatedMetrics { enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines()) } else { enc = expfmt.NewEncoder(w, contentType) @@ -366,6 +366,32 @@ type HandlerOpts struct { // away). Until the implementation is improved, it is recommended to // implement a separate timeout in potentially slow Collectors. Timeout time.Duration + // If true, the experimental OpenMetrics encoding is added to the + // possible options during content negotiation. Note that Prometheus + // 2.5.0+ will negotiate OpenMetrics as first priority. OpenMetrics is + // the only way to transmit exemplars. However, the move to OpenMetrics + // is not completely transparent. Most notably, the values of "quantile" + // labels of Summaries and "le" labels of Histograms are formatted with + // a trailing ".0" if they would otherwise look like integer numbers + // (which changes the identity of the resulting series on the Prometheus + // server). + // + // Deprecated: Use OpenMetricsOptions.EnableOpenMetrics instead. + EnableOpenMetrics bool + // OpenMetricsOptions holds settings for the experimental OpenMetrics encoding. + // It can be used to enable OpenMetrics encoding and for setting extra options. + OpenMetricsOptions OpenMetricsOptions + // ProcessStartTime allows setting process start timevalue that will be exposed + // with "Process-Start-Time-Unix" response header along with the metrics + // payload. This allow callers to have efficient transformations to cumulative + // counters (e.g. OpenTelemetry) or generally _created timestamp estimation per + // scrape target. + // NOTE: This feature is experimental and not covered by OpenMetrics or Prometheus + // exposition format. + ProcessStartTime time.Time +} + +type OpenMetricsOptions struct { // If true, the experimental OpenMetrics encoding is added to the // possible options during content negotiation. Note that Prometheus // 2.5.0+ will negotiate OpenMetrics as first priority. OpenMetrics is @@ -391,14 +417,6 @@ type HandlerOpts struct { // _created lines will result in increased cardinality and no improvements // in reset detection. EnableOpenMetricsCreatedMetrics bool - // ProcessStartTime allows setting process start timevalue that will be exposed - // with "Process-Start-Time-Unix" response header along with the metrics - // payload. This allow callers to have efficient transformations to cumulative - // counters (e.g. OpenTelemetry) or generally _created timestamp estimation per - // scrape target. - // NOTE: This feature is experimental and not covered by OpenMetrics or Prometheus - // exposition format. - ProcessStartTime time.Time } // gzipAccepted returns whether the client will accept gzip-encoded content.