Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ddtrace/tracer/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package tracer

import (
"bytes"
"encoding/json"
"io"
"math"
Expand Down Expand Up @@ -121,9 +122,24 @@ func sampledByRate(n uint64, rate float64) bool {
return n*knuthFactor <= uint64(rate*math.MaxUint64)
}

// formatKnuthSamplingRate formats a sampling rate as a string with up to 6 decimal digits
// formatKnuthSamplingRate formats a sampling rate as a string with up to 6
// decimal places, trimming trailing zeros. It uses AppendFloat with a
// stack-allocated buffer to avoid heap allocations.
func formatKnuthSamplingRate(rate float64) string {
return strconv.FormatFloat(rate, 'g', 6, 64)
var buf [24]byte
b := strconv.AppendFloat(buf[:0], rate, 'f', 6, 64)
// Trim trailing zeros after decimal point, then the dot itself if needed.
if i := bytes.IndexByte(b, '.'); i >= 0 {
end := len(b)
for end > i+1 && b[end-1] == '0' {
end--
}
if b[end-1] == '.' {
end--
}
b = b[:end]
}
return string(b)
}

// serviceEnvKey is used as a map key for per-service sampling rates,
Expand Down
12 changes: 12 additions & 0 deletions ddtrace/tracer/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,10 @@ func TestKnuthSamplingRateWithFloatRules(t *testing.T) {
{"six_decimals", 0.123456, "0.123456"},
{"seven_decimals_rounded", 0.1234567, "0.123457"},
{"trailing_zeros", 0.100000, "0.1"},
{"rate_1_strips_trailing_zeros", 1.0, "1"},
{"six_decimal_precision_boundary", 0.000001, "0.000001"},
{"below_precision_rounds_to_zero", 0.0000001, "0"},
{"rounds_up_to_one_millionth", 0.00000051, "0.000001"},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -2260,6 +2264,14 @@ func TestKnuthSamplingRateWithFloatRules(t *testing.T) {
}
}

func BenchmarkFormatKnuthSamplingRate(b *testing.B) {
rates := []float64{1.0, 0.5, 0.000001, 0.0000001, 0.00000051, 0.7654321}
b.ResetTimer()
for i := 0; i < b.N; i++ {
formatKnuthSamplingRate(rates[i%len(rates)])
}
}

func TestCappedRate(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading