Skip to content

Commit ee9374a

Browse files
committed
Encode sketch average explicitly
To avoid numeric stability issues when computing it as sum/cnt.
1 parent e3e9947 commit ee9374a

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

dogstatsd-http-serializer/src/main/java/com/datadoghq/dogstatsd/http/serializer/SketchMetric.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/** Builder for sketch timeseries. */
1111
public class SketchMetric extends Metric<SketchMetric> {
12-
private static final int VALUES_PER_SKETCH_POINT = 3;
12+
private static final int VALUES_PER_SKETCH_POINT = 4;
1313

1414
SketchMetric(PayloadBuilder pb, int type, String name) {
1515
super(pb, type, name);
@@ -25,6 +25,7 @@ protected SketchMetric self() {
2525
*
2626
* @param timestamp Timestamp of the point in seconds since Unix epoch.
2727
* @param sum Total sum of all observed values.
28+
* @param avg Average of all observed values.
2829
* @param min Minimum observed value.
2930
* @param max Maximum observed value.
3031
* @param cnt Number of observed values.
@@ -35,6 +36,7 @@ protected SketchMetric self() {
3536
public SketchMetric addPoint(
3637
long timestamp,
3738
double sum,
39+
double avg,
3840
double min,
3941
double max,
4042
long cnt,
@@ -47,6 +49,7 @@ public SketchMetric addPoint(
4749

4850
pb.timestamps.put(timestamp);
4951
pb.values.put(sum);
52+
pb.values.put(avg);
5053
pb.values.put(min);
5154
pb.values.put(max);
5255
pb.counts.put(cnt);
@@ -85,6 +88,9 @@ void encodeValues(ValueType valueType) {
8588
r.putSint64(
8689
Column.valsSint64,
8790
(long) pb.values.get(VALUES_PER_SKETCH_POINT * i + 2));
91+
r.putSint64(
92+
Column.valsSint64,
93+
(long) pb.values.get(VALUES_PER_SKETCH_POINT * i + 3));
8894
r.putSint64(Column.valsSint64, pb.counts.get(i));
8995
}
9096
break;
@@ -98,6 +104,9 @@ void encodeValues(ValueType valueType) {
98104
r.putFloat32(
99105
Column.valsFloat32,
100106
(float) pb.values.get(VALUES_PER_SKETCH_POINT * i + 2));
107+
r.putFloat32(
108+
Column.valsFloat32,
109+
(float) pb.values.get(VALUES_PER_SKETCH_POINT * i + 3));
101110
r.putSint64(Column.valsSint64, pb.counts.get(i));
102111
}
103112
break;
@@ -108,6 +117,8 @@ void encodeValues(ValueType valueType) {
108117
Column.valsFloat64, pb.values.get(VALUES_PER_SKETCH_POINT * i + 1));
109118
r.putFloat64(
110119
Column.valsFloat64, pb.values.get(VALUES_PER_SKETCH_POINT * i + 2));
120+
r.putFloat64(
121+
Column.valsFloat64, pb.values.get(VALUES_PER_SKETCH_POINT * i + 3));
111122
r.putSint64(Column.valsSint64, pb.counts.get(i));
112123
}
113124
break;

dogstatsd-http-serializer/src/test/java/com/datadoghq/dogstatsd/http/serializer/PayloadBuilderTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ public void handle(byte[] p) {
4444

4545
b.sketch("ijk")
4646
.setTags(Arrays.asList(new String[] {"foo", "baz"}))
47-
.addPoint(100, 4.75, 1.25, 1.75, 3, new int[] {1351, 1373}, new int[] {1, 2})
48-
.addPoint(110, 6.5, 2.25, 2.75, 5, new int[] {1389, 1402}, new int[] {2, 3})
47+
.addPoint(
48+
100,
49+
4.75,
50+
4.75 / 3,
51+
1.25,
52+
1.75,
53+
3,
54+
new int[] {1351, 1373},
55+
new int[] {1, 2})
56+
.addPoint(
57+
110, 6.5, 6.5 / 5, 2.25, 2.75, 5, new int[] {1389, 1402}, new int[] {2, 3})
4958
.close();
5059

5160
b.rate("lm").setInterval(10).addPoint(100, 3.14).close();
@@ -60,7 +69,7 @@ public void handle(byte[] p) {
6069
new int[] {
6170
// MetricData
6271
(3 << 3) | 2,
63-
188,
72+
196,
6473
1,
6574
// dictNameStr
6675
(1 << 3) | 2,
@@ -194,14 +203,19 @@ public void handle(byte[] p) {
194203
4,
195204
6,
196205
10,
197-
// valsFloat32, list(pack('<ffffff', 4.75, 1.25, 1.75, 6.5, 2.25, 2.75))
206+
// valsFloat32, list(pack('<ffffffff', 4.75, 4.75/3, 1.25, 1.75, 6.5, 6.5/5,
207+
// 2.25, 2.75))
198208
(18 << 3) | 2,
199209
1,
200-
24,
210+
32,
201211
0,
202212
0,
203213
152,
204214
64,
215+
171,
216+
170,
217+
202,
218+
63,
205219
0,
206220
0,
207221
160,
@@ -214,6 +228,10 @@ public void handle(byte[] p) {
214228
0,
215229
208,
216230
64,
231+
102,
232+
102,
233+
166,
234+
63,
217235
0,
218236
0,
219237
16,

0 commit comments

Comments
 (0)