Skip to content

Commit 018375f

Browse files
committed
fix: do not evaluate incomplete samples from datadog
Signed-off-by: Markus Dobel <[email protected]>
1 parent 660ed74 commit 018375f

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

pkg/metrics/providers/datadog.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,20 @@ func (p *DatadogProvider) RunQuery(query string) (float64, error) {
143143
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
144144
}
145145

146-
pl := res.Series[0].Pointlist
146+
// in case of more than one series in the response, pick the first time series from the response
147+
pl := res.Series[0].Pointlist
147148
if len(pl) < 1 {
148149
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
149150
}
150151

151-
vs := pl[len(pl)-1]
152+
// pick the first (oldest) timestamp/value pair from the time series, at the beginning of the interval
153+
// must not pick the newest one from the end of the interval, since it almost always contains an incomplete bucket
154+
vs := pl[0]
152155
if len(vs) < 1 {
153156
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
154157
}
155158

159+
// return the second element of the pair: the value
156160
return vs[1], nil
157161
}
158162

pkg/metrics/providers/datadog_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestDatadogProvider_RunQuery(t *testing.T) {
7575
assert.GreaterOrEqual(t, to, now)
7676
}
7777

78-
json := fmt.Sprintf(`{"series": [{"pointlist": [[1577232000000,29325.102158814265],[1577318400000,56294.46758591842],[1577404800000,%f]]}]}`, expected)
78+
json := fmt.Sprintf(`{"series": [{"pointlist": [[1577232000000,%f],[1577318400000,56294.46758591842],[1577404800000,29325.102158814265]]}]}`, expected)
7979
w.Write([]byte(json))
8080
}))
8181
defer ts.Close()

0 commit comments

Comments
 (0)