Skip to content

Commit

Permalink
Move OpenMetrics options to a separate struct
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
Arthur Silva Sens committed Feb 18, 2024
1 parent 7fd2dcf commit 33ed3e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 4 additions & 2 deletions examples/createdtimestamps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion examples/exemplars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 28 additions & 10 deletions prometheus/promhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 33ed3e6

Please sign in to comment.