Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit a7d3d0e

Browse files
committed
Force the naming convention
For counter type the convention is to use _total suffix https://prometheus.io/docs/practices/naming/#metric-names Added _rate suffix for Rate type that doesn't have a 1:1 mapping in Prometheus.
1 parent fa02497 commit a7d3d0e

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

pkg/remotewrite/remotewrite.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ type seriesWithMeasure struct {
253253
func (swm seriesWithMeasure) MapPrompb() []*prompb.TimeSeries {
254254
var newts []*prompb.TimeSeries
255255

256-
mapMonoSeries := func(s metrics.TimeSeries, t time.Time) prompb.TimeSeries {
256+
mapMonoSeries := func(s metrics.TimeSeries, suffix string, t time.Time) prompb.TimeSeries {
257257
return prompb.TimeSeries{
258-
// TODO: should we add the suffix for
259-
// Counter, Rate and Gauge?
260-
Labels: MapSeries(s, ""),
258+
Labels: MapSeries(s, suffix),
261259
Samples: []*prompb.Sample{
262260
{Timestamp: t.UnixMilli()},
263261
},
@@ -266,17 +264,17 @@ func (swm seriesWithMeasure) MapPrompb() []*prompb.TimeSeries {
266264

267265
switch swm.Metric.Type {
268266
case metrics.Counter:
269-
ts := mapMonoSeries(swm.TimeSeries, swm.Latest)
267+
ts := mapMonoSeries(swm.TimeSeries, "total", swm.Latest)
270268
ts.Samples[0].Value = swm.Measure.(*metrics.CounterSink).Value
271269
newts = []*prompb.TimeSeries{&ts}
272270

273271
case metrics.Gauge:
274-
ts := mapMonoSeries(swm.TimeSeries, swm.Latest)
272+
ts := mapMonoSeries(swm.TimeSeries, "", swm.Latest)
275273
ts.Samples[0].Value = swm.Measure.(*metrics.GaugeSink).Value
276274
newts = []*prompb.TimeSeries{&ts}
277275

278276
case metrics.Rate:
279-
ts := mapMonoSeries(swm.TimeSeries, swm.Latest)
277+
ts := mapMonoSeries(swm.TimeSeries, "rate", swm.Latest)
280278
// pass zero duration here because time is useless for formatting rate
281279
rateVals := swm.Measure.(*metrics.RateSink).Format(time.Duration(0))
282280
ts.Samples[0].Value = rateVals["rate"]

pkg/remotewrite/remotewrite_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,29 @@ func TestOutputConvertToPbSeries(t *testing.T) {
5555
Time: time.Date(2022, time.September, 1, 0, 0, 0, 0, time.UTC),
5656
Value: 2,
5757
},
58+
metrics.Sample{
59+
TimeSeries: metrics.TimeSeries{
60+
Metric: registry.MustNewMetric("metric3", metrics.Rate),
61+
Tags: tagset,
62+
},
63+
Time: time.Date(2022, time.September, 1, 0, 0, 0, 0, time.UTC),
64+
Value: 7,
65+
},
5866
}
5967

6068
o := Output{
6169
tsdb: make(map[metrics.TimeSeries]*seriesWithMeasure),
6270
}
6371

6472
pbseries := o.convertToPbSeries(samples)
65-
require.Len(t, pbseries, 2)
66-
require.Len(t, o.tsdb, 2)
73+
require.Len(t, pbseries, 3)
74+
require.Len(t, o.tsdb, 3)
6775

6876
unix1sept := int64(1661990400 * 1000) // in ms
6977
exp := []*prompb.TimeSeries{
7078
{
7179
Labels: []*prompb.Label{
72-
{Name: "__name__", Value: "k6_metric1"},
80+
{Name: "__name__", Value: "k6_metric1_total"},
7381
{Name: "tagk1", Value: "tagv1"},
7482
},
7583
Samples: []*prompb.Sample{
@@ -78,13 +86,22 @@ func TestOutputConvertToPbSeries(t *testing.T) {
7886
},
7987
{
8088
Labels: []*prompb.Label{
81-
{Name: "__name__", Value: "k6_metric2"},
89+
{Name: "__name__", Value: "k6_metric2_total"},
8290
{Name: "tagk1", Value: "tagv1"},
8391
},
8492
Samples: []*prompb.Sample{
8593
{Value: 2, Timestamp: unix1sept},
8694
},
8795
},
96+
{
97+
Labels: []*prompb.Label{
98+
{Name: "__name__", Value: "k6_metric3_rate"},
99+
{Name: "tagk1", Value: "tagv1"},
100+
},
101+
Samples: []*prompb.Sample{
102+
{Value: 1, Timestamp: unix1sept},
103+
},
104+
},
88105
}
89106

90107
sortByNameLabel(pbseries)

0 commit comments

Comments
 (0)