lib/protoparser/datadogsketches: add more permalinks to the original source code

These permalinks should help verifying the correctness of the code

This is a follow-up after 07213f4e0c

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5775
This commit is contained in:
Aliaksandr Valialkin 2024-02-07 21:33:18 +02:00
parent 07213f4e0c
commit 093798375e
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 24 additions and 19 deletions

View file

@ -9,11 +9,11 @@ import (
) )
var ( var (
// https://github.com/DataDog/opentelemetry-mapping-go/blob/48d52eeea60d28da2e14c154a24557c4d290c6e2/pkg/quantile/config.go // These constants were obtained from https://github.com/DataDog/opentelemetry-mapping-go/blob/48d52eeea60d28da2e14c154a24557c4d290c6e2/pkg/quantile/config.go
epsillon = 1.0 / 128 eps = 1.0 / 128
gamma = 1 + 2*epsillon gamma = 1 + 2*eps
gammaLn = math.Log(gamma) gammaLn = math.Log1p(2 * eps)
defaultMin = 0.981e-9 defaultMin = 1e-9
bias = 1 - int(math.Floor(math.Log(defaultMin)/gammaLn)) bias = 1 - int(math.Floor(math.Log(defaultMin)/gammaLn))
quantiles = []float64{0.5, 0.75, 0.9, 0.95, 0.99} quantiles = []float64{0.5, 0.75, 0.9, 0.95, 0.99}
) )
@ -174,7 +174,7 @@ func (s *Sketch) ToSummary() []*Metric {
timestamp := d.Ts * 1000 timestamp := d.Ts * 1000
points[j] = Point{ points[j] = Point{
Timestamp: timestamp, Timestamp: timestamp,
Value: d.valueForQuantile(q), Value: d.quantile(q),
} }
sumPoints[j] = Point{ sumPoints[j] = Point{
Timestamp: timestamp, Timestamp: timestamp,
@ -285,7 +285,8 @@ func (d *Dogsketch) unmarshalProtobuf(src []byte) (err error) {
return nil return nil
} }
func (d *Dogsketch) valueForQuantile(q float64) float64 { // This function has been copied from https://github.com/DataDog/opentelemetry-mapping-go/blob/48d52eeea60d28da2e14c154a24557c4d290c6e2/pkg/quantile/sparse.go#L92
func (d *Dogsketch) quantile(q float64) float64 {
switch { switch {
case d.Cnt == 0: case d.Cnt == 0:
return 0 return 0
@ -312,10 +313,7 @@ func (d *Dogsketch) valueForQuantile(q float64) float64 {
weight := (cnt - rank) / float64(n) weight := (cnt - rank) / float64(n)
vLow := f64(ks[i]) vLow := f64(ks[i])
vHigh := vLow * gamma vHigh := vLow * gamma
switch i { if i == 0 {
case len(ns):
vHigh = d.Max
case 0:
vLow = d.Min vLow = d.Min
} }
return vLow*weight + vHigh*(1-weight) return vLow*weight + vHigh*(1-weight)
@ -323,15 +321,22 @@ func (d *Dogsketch) valueForQuantile(q float64) float64 {
return d.Max return d.Max
} }
// This function has been copied from https://github.com/DataDog/opentelemetry-mapping-go/blob/48d52eeea60d28da2e14c154a24557c4d290c6e2/pkg/quantile/config.go#L54
func f64(k int32) float64 { func f64(k int32) float64 {
switch { // See https://github.com/DataDog/opentelemetry-mapping-go/blob/48d52eeea60d28da2e14c154a24557c4d290c6e2/pkg/quantile/key.go#L14
case k < 0: if k <= -((1 << 15) - 1) {
return -f64(-k) return math.Inf(-1)
case k == math.MaxInt16 || k == math.MinInt16: }
return math.Inf(int(k)) if k >= ((1 << 15) - 1) {
case k == 0: return math.Inf(1)
}
if k == 0 {
return 0 return 0
} }
if k < 0 {
return -f64(-k)
}
exp := float64(int(k) - bias) exp := float64(int(k) - bias)
return math.Pow(gamma, exp) return math.Pow(gamma, exp)
} }

View file

@ -5,10 +5,10 @@ import (
"testing" "testing"
) )
func TestPointsForQuantile(t *testing.T) { func TestDogsketchQuantile(t *testing.T) {
f := func(d *Dogsketch, q float64, vExpected float64) { f := func(d *Dogsketch, q float64, vExpected float64) {
t.Helper() t.Helper()
v := d.valueForQuantile(q) v := d.quantile(q)
if math.Abs(v-vExpected) > 0.4 { if math.Abs(v-vExpected) > 0.4 {
t.Fatalf("unexpected value; got %v; want %v", v, vExpected) t.Fatalf("unexpected value; got %v; want %v", v, vExpected)
} }