Skip to content

Commit

Permalink
feat: update om ct syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Manik Rana <[email protected]>
  • Loading branch information
Maniktherana committed Feb 12, 2025
1 parent cc17dab commit 546fe59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 91 deletions.
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 @@ import (
"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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
}
}

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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
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 @@ func writeOpenMetricsSample(
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 @@ func writeOpenMetricsSample(
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 @@ func writeOpenMetricsNameAndLabelPairs(
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

0 comments on commit 546fe59

Please sign in to comment.