1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-27 22:49:15 +02:00

Use slices.SortFunc in OpenCensus bridge instead of sort package (#4990)

This commit is contained in:
Tyler Yahn
2024-02-28 23:16:56 -08:00
committed by GitHub
parent 0510d11b8d
commit 4ea22d37ac
2 changed files with 64 additions and 12 deletions

View File

@@ -4,11 +4,12 @@
package internal // import "go.opentelemetry.io/otel/bridge/opencensus/internal/ocmetric"
import (
"cmp"
"errors"
"fmt"
"math"
"reflect"
"sort"
"slices"
"strconv"
ocmetricdata "go.opencensus.io/metric/metricdata"
@@ -200,8 +201,9 @@ func convertExemplar(ocExemplar *ocmetricdata.Exemplar) (metricdata.Exemplar[flo
exemplar.FilteredAttributes = append(exemplar.FilteredAttributes, convertKV(k, v))
}
}
sortable := attribute.Sortable(exemplar.FilteredAttributes)
sort.Sort(&sortable)
slices.SortFunc(exemplar.FilteredAttributes, func(a, b attribute.KeyValue) int {
return cmp.Compare(a.Key, b.Key)
})
return exemplar, err
}
@@ -377,18 +379,12 @@ func convertQuantiles(snapshot ocmetricdata.Snapshot) []metricdata.QuantileValue
Value: value,
})
}
sort.Sort(byQuantile(quantileValues))
slices.SortFunc(quantileValues, func(a, b metricdata.QuantileValue) int {
return cmp.Compare(a.Quantile, b.Quantile)
})
return quantileValues
}
// byQuantile implements sort.Interface for []metricdata.QuantileValue
// based on the Quantile field.
type byQuantile []metricdata.QuantileValue
func (a byQuantile) Len() int { return len(a) }
func (a byQuantile) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byQuantile) Less(i, j int) bool { return a[i].Quantile < a[j].Quantile }
// convertAttrs converts from OpenCensus attribute keys and values to an
// OpenTelemetry attribute Set.
func convertAttrs(keys []ocmetricdata.LabelKey, values []ocmetricdata.LabelValue) (attribute.Set, error) {

View File

@@ -7,7 +7,9 @@ import (
"errors"
"fmt"
"math"
"math/rand"
"reflect"
"strconv"
"testing"
"time"
@@ -1168,3 +1170,57 @@ func TestConvertKV(t *testing.T) {
})
}
}
func BenchmarkConvertExemplar(b *testing.B) {
const attchmentsN = 10
data := make([]*ocmetricdata.Exemplar, b.N)
for i := range data {
a := make(ocmetricdata.Attachments, attchmentsN)
for j := 0; j < attchmentsN; j++ {
a[strconv.Itoa(j)] = rand.Int63()
}
data[i] = &ocmetricdata.Exemplar{
Value: rand.NormFloat64(),
Timestamp: time.Now(),
Attachments: a,
}
}
var out metricdata.Exemplar[float64]
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, _ = convertExemplar(data[n])
}
_ = out
}
func BenchmarkConvertQuantiles(b *testing.B) {
const percentileN = 20
data := make([]ocmetricdata.Snapshot, b.N)
for i := range data {
p := make(map[float64]float64, percentileN)
for j := 0; j < percentileN; j++ {
v := rand.Float64()
for v == 0 {
// Convert from [0, 1) interval to (0, 1).
v = rand.Float64()
}
v *= 100 // Convert from (0, 1) interval to (0, 100).
p[v] = rand.ExpFloat64()
}
data[i] = ocmetricdata.Snapshot{Percentiles: p}
}
var out []metricdata.QuantileValue
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
out = convertQuantiles(data[n])
}
_ = out
}