Skip to content

Commit

Permalink
expfmt/openmetrics: Write created timestamps for counters, summaries …
Browse files Browse the repository at this point in the history
…and histograms

Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
Arthur Silva Sens committed Feb 2, 2024
1 parent a3bdb9e commit 3f2b35b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 9 deletions.
93 changes: 90 additions & 3 deletions expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ import (
"strings"

"github.com/prometheus/common/model"
"google.golang.org/protobuf/types/known/timestamppb"

dto "github.com/prometheus/client_model/go"
)

type toOpenMetrics struct {
withCreatedLines bool
}

type ToOpenMetricsOption func(*toOpenMetrics)

func WithCreatedLines() ToOpenMetricsOption {
return func(t *toOpenMetrics) {
t.withCreatedLines = true
}
}

// MetricFamilyToOpenMetrics converts a MetricFamily proto message into the
// OpenMetrics text format and writes the resulting lines to 'out'. It returns
// the number of bytes written and any error encountered. The output will have
Expand Down Expand Up @@ -64,15 +77,20 @@ import (
// its type will be set to `unknown` in that case to avoid invalid OpenMetrics
// output.
//
// - No support for the following (optional) features: `# UNIT` line, `_created`
// line, info type, stateset type, gaugehistogram type.
// - No support for the following (optional) features: `# UNIT` line, info type,
// stateset type, gaugehistogram type.
//
// - The size of exemplar labels is not checked (i.e. it's possible to create
// exemplars that are larger than allowed by the OpenMetrics specification).
//
// - The value of Counters is not checked. (OpenMetrics doesn't allow counters
// with a `NaN` value.)
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int, err error) {
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...ToOpenMetricsOption) (written int, err error) {
toOM := toOpenMetrics{}
for _, option := range options {
option(&toOM)
}

name := in.GetName()
if name == "" {
return 0, fmt.Errorf("MetricFamily has no name: %s", in)
Expand Down Expand Up @@ -164,6 +182,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int
return
}

var createdTsBytesWritten int
// Finally the samples, one line for each.
for _, metric := range in.Metric {
switch metricType {
Expand All @@ -181,6 +200,10 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int
metric.Counter.GetValue(), 0, false,
metric.Counter.Exemplar,
)
if toOM.withCreatedLines && metric.Counter.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, name, "_total", metric, "", 0, metric.Counter.GetCreatedTimestamp())
n += createdTsBytesWritten
}
case dto.MetricType_GAUGE:
if metric.Gauge == nil {
return written, fmt.Errorf(
Expand Down Expand Up @@ -235,6 +258,10 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int
0, metric.Summary.GetSampleCount(), true,
nil,
)
if metric.Summary.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, name, "", metric, "", 0, metric.Summary.GetCreatedTimestamp())
n += createdTsBytesWritten
}
case dto.MetricType_HISTOGRAM:
if metric.Histogram == nil {
return written, fmt.Errorf(
Expand Down Expand Up @@ -283,6 +310,10 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int
0, metric.Histogram.GetSampleCount(), true,
nil,
)
if metric.Histogram.CreatedTimestamp != nil {
createdTsBytesWritten, err = writeOpenMetricsCreated(w, name, "", metric, "", 0, metric.Histogram.GetCreatedTimestamp())
n += createdTsBytesWritten
}
default:
return written, fmt.Errorf(
"unexpected type in metric %s %s", name, metric,
Expand Down Expand Up @@ -473,6 +504,62 @@ 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 := w.WriteString(strings.TrimSuffix(name, suffixToTrim))
written += n
if err != nil {
return written, err
}

n, err = w.WriteString("_created")
written += n
if err != nil {
return written, err
}

n, err = writeOpenMetricsLabelPairs(
w, metric.Label, additionalLabelName, additionalLabelValue,
)
written += n
if err != nil {
return written, err
}

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

ts := createdTimestamp.AsTime()
// 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(ts.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
44 changes: 38 additions & 6 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func TestCreateOpenMetrics(t *testing.T) {
t.Error(err)
}

scenarios := []struct {
in *dto.MetricFamily
out string
var scenarios = []struct {
in *dto.MetricFamily
options []ToOpenMetricsOption
out string
}{
// 0: Counter, timestamp given, no _total suffix.
{
Expand Down Expand Up @@ -298,6 +299,7 @@ unknown_name{name_1="value 1"} -1.23e-45
Value: proto.Float64(0),
},
},
CreatedTimestamp: openMetricsTimestamp,
},
},
{
Expand Down Expand Up @@ -328,22 +330,26 @@ unknown_name{name_1="value 1"} -1.23e-45
Value: proto.Float64(3),
},
},
CreatedTimestamp: openMetricsTimestamp,
},
},
},
},
options: []ToOpenMetricsOption{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
`,
},
// 7: Histogram
Expand Down Expand Up @@ -379,10 +385,12 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
CumulativeCount: proto.Uint64(2693),
},
},
CreatedTimestamp: openMetricsTimestamp,
},
},
},
},
options: []ToOpenMetricsOption{WithCreatedLines()},
out: `# HELP request_duration_microseconds The response latency.
# TYPE request_duration_microseconds histogram
request_duration_microseconds_bucket{le="100.0"} 123
Expand All @@ -392,6 +400,7 @@ 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
`,
},
// 8: Histogram with missing +Inf bucket.
Expand Down Expand Up @@ -514,7 +523,30 @@ request_duration_microseconds_count 2693
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(42),
Value: proto.Float64(42),
CreatedTimestamp: openMetricsTimestamp,
},
},
},
},
options: []ToOpenMetricsOption{WithCreatedLines()},
out: `# HELP foos Number of foos.
# TYPE foos counter
foos_total 42.0
foos_created 12345.6
`,
},
// 11: Simple Counter without created line.
{
in: &dto.MetricFamily{
Name: proto.String("foos_total"),
Help: proto.String("Number of foos."),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
&dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(42),
CreatedTimestamp: openMetricsTimestamp,
},
},
},
Expand All @@ -524,7 +556,7 @@ request_duration_microseconds_count 2693
foos_total 42.0
`,
},
// 11: No metric.
// 12: No metric.
{
in: &dto.MetricFamily{
Name: proto.String("name_total"),
Expand All @@ -540,7 +572,7 @@ foos_total 42.0

for i, scenario := range scenarios {
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
n, err := MetricFamilyToOpenMetrics(out, scenario.in)
n, err := MetricFamilyToOpenMetrics(out, scenario.in, scenario.options...)
if err != nil {
t.Errorf("%d. error: %s", i, err)
continue
Expand Down

0 comments on commit 3f2b35b

Please sign in to comment.