Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38e0758

Browse files
author
Arthur Silva Sens
committed
expfmt/encoder: Allow opt-in for OM created lines
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 1cb3bf1 commit 38e0758

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

expfmt/encode.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
139139
// interface is kept for backwards compatibility.
140140
// In cases where the Format does not allow for UTF-8 names, the global
141141
// NameEscapingScheme will be applied.
142-
func NewEncoder(w io.Writer, format Format) Encoder {
142+
//
143+
// NewEncoder can be called with additional options to customize the OpenMetrics text output.
144+
// For example:
145+
// NewEncoder(w, FmtOpenMetrics_1_0_0, WithCreatedLines())
146+
//
147+
// Extra options are ignored for all other formats.
148+
func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder {
143149
escapingScheme := format.ToEscapingScheme()
144150

145151
switch format.FormatType() {
@@ -178,7 +184,7 @@ func NewEncoder(w io.Writer, format Format) Encoder {
178184
case TypeOpenMetrics:
179185
return encoderCloser{
180186
encode: func(v *dto.MetricFamily) error {
181-
_, err := MetricFamilyToOpenMetrics(w, model.EscapeMetricFamily(v, escapingScheme))
187+
_, err := MetricFamilyToOpenMetrics(w, model.EscapeMetricFamily(v, escapingScheme), options...)
182188
return err
183189
},
184190
close: func() error {

expfmt/openmetrics_create.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,24 @@ import (
2828
dto "github.com/prometheus/client_model/go"
2929
)
3030

31-
type toOpenMetrics struct {
31+
type encoderOption struct {
3232
withCreatedLines bool
3333
}
3434

35-
type ToOpenMetricsOption func(*toOpenMetrics)
35+
type EncoderOption func(*encoderOption)
3636

37-
func WithCreatedLines() ToOpenMetricsOption {
38-
return func(t *toOpenMetrics) {
37+
// WithCreatedLines is an EncoderOption that configures the OpenMetrics encoder
38+
// to include _created lines (See
39+
// https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#counter-1).
40+
// Created timestamps can improve the accuracy of series reset detection, but
41+
// come with a bandwidth cost.
42+
//
43+
// At the time of writing, created timestamp ingestion is still experimental in
44+
// Prometheus and need to be enabled with the feature-flag
45+
// `--feature-flag=created-timestamp-zero-ingestion`, and breaking changes are
46+
// still possible. Therefore, it is recommended to use this feature with caution.
47+
func WithCreatedLines() EncoderOption {
48+
return func(t *encoderOption) {
3949
t.withCreatedLines = true
4050
}
4151
}
@@ -85,8 +95,8 @@ func WithCreatedLines() ToOpenMetricsOption {
8595
//
8696
// - The value of Counters is not checked. (OpenMetrics doesn't allow counters
8797
// with a `NaN` value.)
88-
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...ToOpenMetricsOption) (written int, err error) {
89-
toOM := toOpenMetrics{}
98+
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...EncoderOption) (written int, err error) {
99+
toOM := encoderOption{}
90100
for _, option := range options {
91101
option(&toOM)
92102
}

expfmt/openmetrics_create_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestCreateOpenMetrics(t *testing.T) {
4242

4343
scenarios := []struct {
4444
in *dto.MetricFamily
45-
options []ToOpenMetricsOption
45+
options []EncoderOption
4646
out string
4747
}{
4848
// 0: Counter, timestamp given, no _total suffix.
@@ -343,7 +343,7 @@ unknown_name{name_1="value 1"} -1.23e-45
343343
},
344344
},
345345
},
346-
options: []ToOpenMetricsOption{WithCreatedLines()},
346+
options: []EncoderOption{WithCreatedLines()},
347347
out: `# HELP summary_name summary docstring
348348
# TYPE summary_name summary
349349
summary_name{quantile="0.5"} -1.23
@@ -398,7 +398,7 @@ summary_name_created{name_1="value 1",name_2="value 2"} 12345.6
398398
},
399399
},
400400
},
401-
options: []ToOpenMetricsOption{WithCreatedLines()},
401+
options: []EncoderOption{WithCreatedLines()},
402402
out: `# HELP request_duration_microseconds The response latency.
403403
# TYPE request_duration_microseconds histogram
404404
request_duration_microseconds_bucket{le="100.0"} 123
@@ -537,7 +537,7 @@ request_duration_microseconds_count 2693
537537
},
538538
},
539539
},
540-
options: []ToOpenMetricsOption{WithCreatedLines()},
540+
options: []EncoderOption{WithCreatedLines()},
541541
out: `# HELP foos Number of foos.
542542
# TYPE foos counter
543543
foos_total 42.0

0 commit comments

Comments
 (0)