Skip to content

Commit 33ed3e6

Browse files
author
Arthur Silva Sens
committed
Move OpenMetrics options to a separate struct
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 7fd2dcf commit 33ed3e6

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

examples/createdtimestamps/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ func main() {
5151
"/metrics", promhttp.HandlerFor(
5252
registry,
5353
promhttp.HandlerOpts{
54-
EnableOpenMetrics: true,
55-
EnableOpenMetricsCreatedMetrics: true,
54+
OpenMetricsOptions: promhttp.OpenMetricsOptions{
55+
EnableOpenMetrics: true,
56+
EnableOpenMetricsCreatedMetrics: true,
57+
},
5658
}),
5759
)
5860
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics

examples/exemplars/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func main() {
6161
"/metrics", promhttp.HandlerFor(
6262
registry,
6363
promhttp.HandlerOpts{
64-
EnableOpenMetrics: true,
64+
OpenMetricsOptions: promhttp.OpenMetricsOptions{
65+
EnableOpenMetrics: true,
66+
},
6567
}),
6668
)
6769
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics

prometheus/promhttp/http.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
160160
}
161161

162162
var contentType expfmt.Format
163-
if opts.EnableOpenMetrics {
163+
if opts.EnableOpenMetrics || opts.OpenMetricsOptions.EnableOpenMetrics {
164164
contentType = expfmt.NegotiateIncludingOpenMetrics(req.Header)
165165
} else {
166166
contentType = expfmt.Negotiate(req.Header)
@@ -181,7 +181,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
181181
}
182182

183183
var enc expfmt.Encoder
184-
if opts.EnableOpenMetricsCreatedMetrics {
184+
if opts.OpenMetricsOptions.EnableOpenMetricsCreatedMetrics {
185185
enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines())
186186
} else {
187187
enc = expfmt.NewEncoder(w, contentType)
@@ -366,6 +366,32 @@ type HandlerOpts struct {
366366
// away). Until the implementation is improved, it is recommended to
367367
// implement a separate timeout in potentially slow Collectors.
368368
Timeout time.Duration
369+
// If true, the experimental OpenMetrics encoding is added to the
370+
// possible options during content negotiation. Note that Prometheus
371+
// 2.5.0+ will negotiate OpenMetrics as first priority. OpenMetrics is
372+
// the only way to transmit exemplars. However, the move to OpenMetrics
373+
// is not completely transparent. Most notably, the values of "quantile"
374+
// labels of Summaries and "le" labels of Histograms are formatted with
375+
// a trailing ".0" if they would otherwise look like integer numbers
376+
// (which changes the identity of the resulting series on the Prometheus
377+
// server).
378+
//
379+
// Deprecated: Use OpenMetricsOptions.EnableOpenMetrics instead.
380+
EnableOpenMetrics bool
381+
// OpenMetricsOptions holds settings for the experimental OpenMetrics encoding.
382+
// It can be used to enable OpenMetrics encoding and for setting extra options.
383+
OpenMetricsOptions OpenMetricsOptions
384+
// ProcessStartTime allows setting process start timevalue that will be exposed
385+
// with "Process-Start-Time-Unix" response header along with the metrics
386+
// payload. This allow callers to have efficient transformations to cumulative
387+
// counters (e.g. OpenTelemetry) or generally _created timestamp estimation per
388+
// scrape target.
389+
// NOTE: This feature is experimental and not covered by OpenMetrics or Prometheus
390+
// exposition format.
391+
ProcessStartTime time.Time
392+
}
393+
394+
type OpenMetricsOptions struct {
369395
// If true, the experimental OpenMetrics encoding is added to the
370396
// possible options during content negotiation. Note that Prometheus
371397
// 2.5.0+ will negotiate OpenMetrics as first priority. OpenMetrics is
@@ -391,14 +417,6 @@ type HandlerOpts struct {
391417
// _created lines will result in increased cardinality and no improvements
392418
// in reset detection.
393419
EnableOpenMetricsCreatedMetrics bool
394-
// ProcessStartTime allows setting process start timevalue that will be exposed
395-
// with "Process-Start-Time-Unix" response header along with the metrics
396-
// payload. This allow callers to have efficient transformations to cumulative
397-
// counters (e.g. OpenTelemetry) or generally _created timestamp estimation per
398-
// scrape target.
399-
// NOTE: This feature is experimental and not covered by OpenMetrics or Prometheus
400-
// exposition format.
401-
ProcessStartTime time.Time
402420
}
403421

404422
// gzipAccepted returns whether the client will accept gzip-encoded content.

0 commit comments

Comments
 (0)