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

Commit 0b4cdbb

Browse files
authored
Merge pull request #72 from grafana/stats-time-seconds
Trend stats in base unit
2 parents 7de5db5 + fddb22f commit 0b4cdbb

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

pkg/remotewrite/trend.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (sink *extendedTrendSink) MapPrompb(series metrics.TimeSeries, t time.Time)
5050
tg.CacheNameIndex()
5151

5252
for stat, statfn := range sink.trendStats {
53-
tg.Append(stat, statfn(sink.TrendSink))
53+
tg.Append(stat, adaptUnit(series.Metric.Contains, statfn(sink.TrendSink)))
5454
}
5555
return tg.series
5656
}
@@ -136,21 +136,17 @@ func newNativeHistogramSink(m *metrics.Metric) *nativeHistogramSink {
136136
}
137137

138138
func (sink *nativeHistogramSink) Add(s metrics.Sample) {
139-
if s.Metric.Contains == metrics.Time {
140-
// The Prometheus' convention is to use seconds
141-
// as time unit.
142-
//
143-
// It isn't a requirement but having the current factor fixed to 1.1 then
144-
// have seconds is beneficial for having a better resolution.
145-
//
146-
// The assumption is that an higher precision is required
147-
// in case of under-second and more relaxed in case of higher values.
148-
sink.H.Observe(s.Value / 1000)
149-
} else {
150-
// If the Value type is not defined any assumption can be done
151-
// because the Sample's Value could contains any unit.
152-
sink.H.Observe(s.Value)
153-
}
139+
// The Prometheus' convention is to use seconds
140+
// as time unit.
141+
//
142+
// It isn't a requirement but having the current factor fixed to 1.1 then
143+
// have seconds is beneficial for having a better resolution.
144+
//
145+
// The assumption is that an higher precision is required
146+
// in case of under-second and more relaxed in case of higher values.
147+
// If the Value type is not defined any assumption can be done
148+
// because the Sample's Value could contains any unit.
149+
sink.H.Observe(adaptUnit(s.Metric.Contains, s.Value))
154150
}
155151

156152
// TODO: create a smaller Sink interface for this Output.
@@ -228,3 +224,16 @@ func baseUnit(vt metrics.ValueType) string {
228224
return ""
229225
}
230226
}
227+
228+
// adaptUnit converts the generated value into the expected base unit
229+
// as requested by the Prometheus convention.
230+
//
231+
// Time: converted to seconds from milliseconds.
232+
// Data: k6 emits it in Bytes so it already fine.
233+
// Other: use the submitted unit.
234+
func adaptUnit(vt metrics.ValueType, v float64) float64 {
235+
if vt == metrics.Time {
236+
return v / 1000
237+
}
238+
return v
239+
}

0 commit comments

Comments
 (0)