diff --git a/processor/probabilisticsamplerprocessor/fnvhasher.go b/processor/probabilisticsamplerprocessor/fnvhasher.go index c12d803f2975..d0fd366f1d26 100644 --- a/processor/probabilisticsamplerprocessor/fnvhasher.go +++ b/processor/probabilisticsamplerprocessor/fnvhasher.go @@ -4,6 +4,7 @@ package probabilisticsamplerprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/probabilisticsamplerprocessor" import ( + "encoding/binary" "hash/fnv" ) @@ -19,8 +20,6 @@ func computeHash(b []byte, seed uint32) uint32 { // i32tob converts a seed to a byte array to be used as part of fnv.Write() func i32tob(val uint32) []byte { r := make([]byte, 4) - for i := uint32(0); i < 4; i++ { - r[i] = byte((val >> (8 * i)) & 0xff) - } + binary.LittleEndian.PutUint32(r, val) return r } diff --git a/processor/probabilisticsamplerprocessor/fnvhasher_test.go b/processor/probabilisticsamplerprocessor/fnvhasher_test.go new file mode 100644 index 000000000000..5f69d3fcb0e0 --- /dev/null +++ b/processor/probabilisticsamplerprocessor/fnvhasher_test.go @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package probabilisticsamplerprocessor + +import ( + "testing" +) + +func BenchmarkSeedConversion(b *testing.B) { + val := uint32(0x3024001) // Just a random 32 bit int + b.ResetTimer() + for i := 0; i < b.N; i++ { + i32tob(val) + } +}