Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an inline OM created timestamp syntax #760

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 24 additions & 69 deletions expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
"strconv"
"strings"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/prometheus/common/model"

Check failure on line 25 in expfmt/openmetrics_create.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (goimports)
"google.golang.org/protobuf/types/known/timestamppb"

dto "github.com/prometheus/client_model/go"
)
Expand Down Expand Up @@ -245,8 +244,6 @@
}
}

var createdTsBytesWritten int

// Finally the samples, one line for each.
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") {
compliantName = compliantName + "_total"
Expand All @@ -262,12 +259,8 @@
n, err = writeOpenMetricsSample(
w, compliantName, "", metric, "", 0,
metric.Counter.GetValue(), 0, false,
metric.Counter.Exemplar,
metric.Counter.Exemplar, toOM.withCreatedLines, metric.Counter.CreatedTimestamp,
)
if toOM.withCreatedLines && metric.Counter.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, compliantName, "_total", metric, "", 0, metric.Counter.GetCreatedTimestamp())
n += createdTsBytesWritten
}
case dto.MetricType_GAUGE:
if metric.Gauge == nil {
return written, fmt.Errorf(
Expand All @@ -277,7 +270,7 @@
n, err = writeOpenMetricsSample(
w, compliantName, "", metric, "", 0,
metric.Gauge.GetValue(), 0, false,
nil,
nil, toOM.withCreatedLines, nil,
)
case dto.MetricType_UNTYPED:
if metric.Untyped == nil {
Expand All @@ -288,7 +281,7 @@
n, err = writeOpenMetricsSample(
w, compliantName, "", metric, "", 0,
metric.Untyped.GetValue(), 0, false,
nil,
nil, toOM.withCreatedLines, nil,
)
case dto.MetricType_SUMMARY:
if metric.Summary == nil {
Expand All @@ -301,7 +294,7 @@
w, compliantName, "", metric,
model.QuantileLabel, q.GetQuantile(),
q.GetValue(), 0, false,
nil,
nil, toOM.withCreatedLines, metric.Summary.CreatedTimestamp,
)
written += n
if err != nil {
Expand All @@ -311,7 +304,7 @@
n, err = writeOpenMetricsSample(
w, compliantName, "_sum", metric, "", 0,
metric.Summary.GetSampleSum(), 0, false,
nil,
nil, toOM.withCreatedLines, metric.Summary.CreatedTimestamp,
)
written += n
if err != nil {
Expand All @@ -320,12 +313,8 @@
n, err = writeOpenMetricsSample(
w, compliantName, "_count", metric, "", 0,
0, metric.Summary.GetSampleCount(), true,
nil,
nil, toOM.withCreatedLines, metric.Summary.CreatedTimestamp,
)
if toOM.withCreatedLines && metric.Summary.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, compliantName, "", metric, "", 0, metric.Summary.GetCreatedTimestamp())
n += createdTsBytesWritten
}
case dto.MetricType_HISTOGRAM:
if metric.Histogram == nil {
return written, fmt.Errorf(
Expand All @@ -338,7 +327,7 @@
w, compliantName, "_bucket", metric,
model.BucketLabel, b.GetUpperBound(),
0, b.GetCumulativeCount(), true,
b.Exemplar,
b.Exemplar, toOM.withCreatedLines, metric.Histogram.CreatedTimestamp,
)
written += n
if err != nil {
Expand All @@ -353,7 +342,7 @@
w, compliantName, "_bucket", metric,
model.BucketLabel, math.Inf(+1),
0, metric.Histogram.GetSampleCount(), true,
nil,
nil, toOM.withCreatedLines, metric.Histogram.CreatedTimestamp,
)
written += n
if err != nil {
Expand All @@ -363,7 +352,7 @@
n, err = writeOpenMetricsSample(
w, compliantName, "_sum", metric, "", 0,
metric.Histogram.GetSampleSum(), 0, false,
nil,
nil, toOM.withCreatedLines, metric.Histogram.CreatedTimestamp,
)
written += n
if err != nil {
Expand All @@ -372,12 +361,8 @@
n, err = writeOpenMetricsSample(
w, compliantName, "_count", metric, "", 0,
0, metric.Histogram.GetSampleCount(), true,
nil,
nil, toOM.withCreatedLines, metric.Histogram.CreatedTimestamp,
)
if toOM.withCreatedLines && metric.Histogram.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, compliantName, "", metric, "", 0, metric.Histogram.GetCreatedTimestamp())
n += createdTsBytesWritten
}
default:
return written, fmt.Errorf(
"unexpected type in metric %s %s", compliantName, metric,
Expand Down Expand Up @@ -409,6 +394,7 @@
additionalLabelName string, additionalLabelValue float64,
floatValue float64, intValue uint64, useIntValue bool,
exemplar *dto.Exemplar,
writeCT bool, createdTimestamp *timestamppb.Timestamp,
) (int, error) {
written := 0
n, err := writeOpenMetricsNameAndLabelPairs(
Expand Down Expand Up @@ -445,6 +431,18 @@
return written, err
}
}
if writeCT && createdTimestamp != nil {
createdSuffixWritten, err := w.WriteString(" ct@")
written += createdSuffixWritten
if err != nil {
return written, err
}
n, err = writeOpenMetricsFloat(w, float64(createdTimestamp.AsTime().UnixNano())/1e9)
written += n
if err != nil {
return written, err
}
}
if exemplar != nil && len(exemplar.Label) > 0 {
n, err = writeExemplar(w, exemplar)
written += n
Expand Down Expand Up @@ -568,49 +566,6 @@
return written, nil
}

// writeOpenMetricsCreated writes the created timestamp for a single time series
// following OpenMetrics text format to w, given the metric name, the metric proto
// message itself, optionally a suffix to be removed, e.g. '_total' for counters,
// an additional label name with a float64 value (use empty string as label name if
// not required) and the timestamp that represents the created timestamp.
// The function returns the number of bytes written and any error encountered.
func writeOpenMetricsCreated(w enhancedWriter,
name, suffixToTrim string, metric *dto.Metric,
additionalLabelName string, additionalLabelValue float64,
createdTimestamp *timestamppb.Timestamp,
) (int, error) {
written := 0
n, err := writeOpenMetricsNameAndLabelPairs(
w, strings.TrimSuffix(name, suffixToTrim)+"_created", metric.Label, additionalLabelName, additionalLabelValue,
)
written += n
if err != nil {
return written, err
}

err = w.WriteByte(' ')
written++
if err != nil {
return written, err
}

// TODO(beorn7): Format this directly from components of ts to
// avoid overflow/underflow and precision issues of the float
// conversion.
n, err = writeOpenMetricsFloat(w, float64(createdTimestamp.AsTime().UnixNano())/1e9)
written += n
if err != nil {
return written, err
}

err = w.WriteByte('\n')
written++
if err != nil {
return written, err
}
return written, nil
}

// writeExemplar writes the provided exemplar in OpenMetrics format to w. The
// function returns the number of bytes written and any error encountered.
func writeExemplar(w enhancedWriter, e *dto.Exemplar) (int, error) {
Expand Down
40 changes: 18 additions & 22 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,16 @@ unknown_name{name_1="value 1"} -1.23e-45
options: []EncoderOption{WithCreatedLines()},
out: `# HELP summary_name summary docstring
# TYPE summary_name summary
summary_name{quantile="0.5"} -1.23
summary_name{quantile="0.9"} 0.2342354
summary_name{quantile="0.99"} 0.0
summary_name_sum -3.4567
summary_name_count 42
summary_name_created 12345.6
summary_name{name_1="value 1",name_2="value 2",quantile="0.5"} 1.0
summary_name{name_1="value 1",name_2="value 2",quantile="0.9"} 2.0
summary_name{name_1="value 1",name_2="value 2",quantile="0.99"} 3.0
summary_name_sum{name_1="value 1",name_2="value 2"} 2010.1971
summary_name_count{name_1="value 1",name_2="value 2"} 4711
summary_name_created{name_1="value 1",name_2="value 2"} 12345.6
summary_name{quantile="0.5"} -1.23 [email protected]
summary_name{quantile="0.9"} 0.2342354 [email protected]
summary_name{quantile="0.99"} 0.0 [email protected]
summary_name_sum -3.4567 [email protected]
summary_name_count 42 [email protected]
summary_name{name_1="value 1",name_2="value 2",quantile="0.5"} 1.0 [email protected]
summary_name{name_1="value 1",name_2="value 2",quantile="0.9"} 2.0 [email protected]
summary_name{name_1="value 1",name_2="value 2",quantile="0.99"} 3.0 [email protected]
summary_name_sum{name_1="value 1",name_2="value 2"} 2010.1971 [email protected]
summary_name_count{name_1="value 1",name_2="value 2"} 4711 [email protected]
`,
},
// 7: Histogram
Expand Down Expand Up @@ -404,14 +402,13 @@ summary_name_created{name_1="value 1",name_2="value 2"} 12345.6
out: `# HELP request_duration_microseconds The response latency.
# TYPE request_duration_microseconds histogram
# UNIT request_duration_microseconds microseconds
request_duration_microseconds_bucket{le="100.0"} 123
request_duration_microseconds_bucket{le="120.0"} 412
request_duration_microseconds_bucket{le="144.0"} 592
request_duration_microseconds_bucket{le="172.8"} 1524
request_duration_microseconds_bucket{le="+Inf"} 2693
request_duration_microseconds_sum 1.7560473e+06
request_duration_microseconds_count 2693
request_duration_microseconds_created 12345.6
request_duration_microseconds_bucket{le="100.0"} 123 [email protected]
request_duration_microseconds_bucket{le="120.0"} 412 [email protected]
request_duration_microseconds_bucket{le="144.0"} 592 [email protected]
request_duration_microseconds_bucket{le="172.8"} 1524 [email protected]
request_duration_microseconds_bucket{le="+Inf"} 2693 [email protected]
request_duration_microseconds_sum 1.7560473e+06 [email protected]
request_duration_microseconds_count 2693 [email protected]
`,
},
// 8: Histogram with missing +Inf bucket.
Expand Down Expand Up @@ -544,8 +541,7 @@ request_duration_microseconds_count 2693
options: []EncoderOption{WithCreatedLines()},
out: `# HELP foos Number of foos.
# TYPE foos counter
foos_total 42.0
foos_created 12345.6
foos_total 42.0 [email protected]
`,
},
// 11: Simple Counter without created line.
Expand Down
Loading