Skip to content

Commit

Permalink
Fixes #778 - Exemplars should be sorted before sending to Cloud Ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Dec 19, 2023
1 parent 18da23f commit bac16d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions exporter/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -927,6 +928,10 @@ func (m *metricMapper) exemplars(exs pmetric.ExemplarSlice, projectID string) []
for i := 0; i < exs.Len(); i++ {
exemplars[i] = m.exemplar(exs.At(i), projectID)
}
sort.Slice(exemplars, func(i, j int) bool {
return exemplars[i].Value < exemplars[j].Value
})

return exemplars
}

Expand Down
25 changes: 25 additions & 0 deletions exporter/collector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,31 @@ func TestZeroCountExponentialHistogramPointToTimeSeries(t *testing.T) {
assert.Equal(t, int32(3), hdp.BucketOptions.GetExponentialBuckets().NumFiniteBuckets)
}

func TestExemplarSorted(t *testing.T) {
mapper, shutdown := newTestMetricMapper()
defer shutdown()
exemplars := pmetric.NewExemplarSlice()

exemplar := exemplars.AppendEmpty()
exemplar.SetTimestamp(pcommon.NewTimestampFromTime(start))
exemplar.SetDoubleValue(2)

exemplar2 := exemplars.AppendEmpty()
exemplar2.SetTimestamp(pcommon.NewTimestampFromTime(start))
exemplar2.SetDoubleValue(1)

exemplar3 := exemplars.AppendEmpty()
exemplar3.SetTimestamp(pcommon.NewTimestampFromTime(start))
exemplar3.SetDoubleValue(3)

result := mapper.exemplars(exemplars, mapper.cfg.ProjectID)
assert.Equal(t, len(result), 3)
// Ensure exemplars are in value order.
assert.Equal(t, float64(1), result[0].Value)
assert.Equal(t, float64(2), result[1].Value)
assert.Equal(t, float64(3), result[2].Value)
}

func TestExemplarNoAttachements(t *testing.T) {
mapper, shutdown := newTestMetricMapper()
defer shutdown()
Expand Down

0 comments on commit bac16d7

Please sign in to comment.