Skip to content

Commit 240345e

Browse files
author
Arthur Silva Sens
committed
Optinally print OM created lines
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 8c7e30f commit 240345e

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

examples/createdtimestamps/main.go

Lines changed: 60 additions & 0 deletions
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+
EnableOpenMetricsCreatedMetrics: true,
56+
}),
57+
)
58+
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics
59+
log.Fatalln(http.ListenAndServe(":8081", nil))
60+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ require (
2929
)
3030

3131
exclude github.com/prometheus/client_golang v1.12.1
32+
33+
replace github.com/prometheus/common => github.com/ArthurSens/common v0.0.0-20240202142709-6a6b93b6b111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/ArthurSens/common v0.0.0-20240202142709-6a6b93b6b111 h1:W9c8LaygwdqrtdNxx7rtjuMjZVolfG3bV12TVpW7ob8=
2+
github.com/ArthurSens/common v0.0.0-20240202142709-6a6b93b6b111/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ=
13
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
24
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
35
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
@@ -33,8 +35,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3335
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3436
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
3537
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
36-
github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y=
37-
github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ=
3838
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
3939
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
4040
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=

prometheus/promhttp/http.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
180180
w = gz
181181
}
182182

183-
enc := expfmt.NewEncoder(w, contentType)
183+
var enc expfmt.Encoder
184+
if opts.EnableOpenMetricsCreatedMetrics {
185+
enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines())
186+
} else {
187+
enc = expfmt.NewEncoder(w, contentType)
188+
}
184189

185190
// handleError handles the error according to opts.ErrorHandling
186191
// and returns true if we have to abort after the handling.
@@ -371,6 +376,21 @@ type HandlerOpts struct {
371376
// (which changes the identity of the resulting series on the Prometheus
372377
// server).
373378
EnableOpenMetrics bool
379+
// If 'EnableOpenMetrics' is true, 'EnableOpenMetricsCreatedMetrics' allows
380+
// to add extra '_created' lines for counters, histograms and summaries,
381+
// as defined in the OpenMetrics specification (see
382+
// https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#counter-1).
383+
// Created timestamps are used to improve the accuracy of reset detection,
384+
// but be aware that it also increases the size of the payload.
385+
//
386+
// Prometheus introduced the feature flag 'created-timestamp-zero-ingestion'
387+
// in version 2.50.0, but with support limited to the Prometheus protobuf
388+
// format. Starting in Prometheus XXXX, the feature flag will be extended
389+
// to the OpenMetrics text format. If using Prometheus XXXX or later, it
390+
// is recommended to enable the feature flag in Prometheus, otherwise enabling
391+
// _created lines will result in increased cardinality and no improvements
392+
// in reset detection.
393+
EnableOpenMetricsCreatedMetrics bool
374394
// ProcessStartTime allows setting process start timevalue that will be exposed
375395
// with "Process-Start-Time-Unix" response header along with the metrics
376396
// payload. This allow callers to have efficient transformations to cumulative

0 commit comments

Comments
 (0)