Sort metrics by timestamp in prometheus output (#5534)

This commit is contained in:
Daniel Nelson 2019-03-05 11:08:02 -08:00 committed by GitHub
parent a0527db037
commit b5adaff07f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 1 deletions

View File

@ -364,13 +364,27 @@ func (p *PrometheusClient) addMetricFamily(point telegraf.Metric, sample *Sample
addSample(fam, sample, sampleID) addSample(fam, sample, sampleID)
} }
// Sorted returns a copy of the metrics in time ascending order. A copy is
// made to avoid modifying the input metric slice since doing so is not
// allowed.
func sorted(metrics []telegraf.Metric) []telegraf.Metric {
batch := make([]telegraf.Metric, 0, len(metrics))
for i := len(metrics) - 1; i >= 0; i-- {
batch = append(batch, metrics[i])
}
sort.Slice(batch, func(i, j int) bool {
return batch[i].Time().Before(batch[j].Time())
})
return batch
}
func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
now := p.now() now := p.now()
for _, point := range metrics { for _, point := range sorted(metrics) {
tags := point.Tags() tags := point.Tags()
sampleID := CreateSampleID(tags) sampleID := CreateSampleID(tags)