Skip to content

Commit 284ca0f

Browse files
ArthurSensArthur Silva Sens
and
Arthur Silva Sens
authored
Optinally print OM created lines (#1408)
Signed-off-by: Arthur Silva Sens <[email protected]> Co-authored-by: Arthur Silva Sens <[email protected]>
1 parent 93c851f commit 284ca0f

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

examples/createdtimestamps/main.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// A simple example of how to exposed created timestamps in OpenMetrics format.
15+
16+
package main
17+
18+
import (
19+
"log"
20+
"net/http"
21+
"time"
22+
23+
"github.com/prometheus/client_golang/prometheus"
24+
"github.com/prometheus/client_golang/prometheus/promhttp"
25+
)
26+
27+
func main() {
28+
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
29+
Name: "http_request_duration_seconds",
30+
Help: "A histogram of the HTTP request durations in seconds.",
31+
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
32+
})
33+
34+
// Create non-global registry.
35+
registry := prometheus.NewRegistry()
36+
registry.MustRegister(
37+
requestDurations,
38+
)
39+
40+
go func() {
41+
for {
42+
// Record fictional latency.
43+
now := time.Now()
44+
requestDurations.Observe(time.Since(now).Seconds())
45+
time.Sleep(600 * time.Millisecond)
46+
}
47+
}()
48+
49+
// Expose /metrics HTTP endpoint using the created custom registry.
50+
http.Handle(
51+
"/metrics", promhttp.HandlerFor(
52+
registry,
53+
promhttp.HandlerOpts{
54+
EnableOpenMetrics: true,
55+
EnableOpenMetricsTextCreatedSamples: true,
56+
}),
57+
)
58+
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics
59+
log.Fatalln(http.ListenAndServe(":8080", nil))
60+
}

prometheus/promhttp/http.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
207207
if encodingHeader != string(Identity) {
208208
rsp.Header().Set(contentEncodingHeader, encodingHeader)
209209
}
210-
enc := expfmt.NewEncoder(w, contentType)
210+
211+
var enc expfmt.Encoder
212+
if opts.EnableOpenMetricsTextCreatedSamples {
213+
enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines())
214+
} else {
215+
enc = expfmt.NewEncoder(w, contentType)
216+
}
211217

212218
// handleError handles the error according to opts.ErrorHandling
213219
// and returns true if we have to abort after the handling.
@@ -408,6 +414,21 @@ type HandlerOpts struct {
408414
// (which changes the identity of the resulting series on the Prometheus
409415
// server).
410416
EnableOpenMetrics bool
417+
// EnableOpenMetricsTextCreatedSamples specifies if this handler should add, extra, synthetic
418+
// Created Timestamps for counters, histograms and summaries, which for the current
419+
// version of OpenMetrics are defined as extra series with the same name and "_created"
420+
// suffix. See also the OpenMetrics specification for more details
421+
// https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#counter-1
422+
//
423+
// Created timestamps are used to improve the accuracy of reset detection,
424+
// but the way it's designed in OpenMetrics 1.0 it also dramatically increases cardinality
425+
// if the scraper does not handle those metrics correctly (converting to created timestamp
426+
// instead of leaving those series as-is). New OpenMetrics versions might improve
427+
// this situation.
428+
//
429+
// Prometheus introduced the feature flag 'created-timestamp-zero-ingestion'
430+
// in version 2.50.0 to handle this situation.
431+
EnableOpenMetricsTextCreatedSamples bool
411432
// ProcessStartTime allows setting process start timevalue that will be exposed
412433
// with "Process-Start-Time-Unix" response header along with the metrics
413434
// payload. This allow callers to have efficient transformations to cumulative

0 commit comments

Comments
 (0)