Skip to content

Commit e9388db

Browse files
authored
Handle empty series correctly (#86)
Behaviour of this changed in thanos-io/thanos#3010 -- this now generates an error, as it probably should, but we sent back a series with no datapoints in it previously.
1 parent 87be6a0 commit e9388db

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

pkg/store/store.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ func (store *OpenTSDBStore) Series(
248248
if err != nil {
249249
return err
250250
}
251+
if res == nil {
252+
// OpenTSDB can return a series with no datapoints, in Prometheus this
253+
// is just not returned.
254+
continue
255+
}
251256
if err := server.Send(res); err != nil {
252257
return err
253258
}
@@ -650,10 +655,14 @@ func (store *OpenTSDBStore) convertOpenTSDBResultsToSeriesResponse(respI *opents
650655
}
651656
chunks = append(chunks, aggrChunk)
652657
}
653-
return storepb.NewSeriesResponse(&storepb.Series{
654-
Labels: seriesLabels,
655-
Chunks: chunks,
656-
}), len(dps), nil
658+
var response *storepb.SeriesResponse
659+
if len(chunks) > 0 {
660+
response = storepb.NewSeriesResponse(&storepb.Series{
661+
Labels: seriesLabels,
662+
Chunks: chunks,
663+
})
664+
}
665+
return response, len(dps), nil
657666
}
658667

659668
func convertPromQLMatcherToFilter(matcher storepb.LabelMatcher) (opentsdb.Filter, error) {

pkg/store/store_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -979,23 +979,21 @@ func TestConvertOpenTSDBResultsToSeriesResponse(t *testing.T) {
979979
Tags: map[string]string{},
980980
Dps: newDps(map[string]interface{}{}),
981981
},
982-
expectedOutput: storepb.NewSeriesResponse(&storepb.Series{
983-
Labels: []storepb.Label{{Name: "__name__", Value: "metric"}},
984-
Chunks: []storepb.AggrChunk{},
985-
}),
986-
expectedChunkTypes: []storepb.Aggr{},
982+
expectedOutput: nil,
987983
},
988984
{
989985
input: opentsdb.QueryRespItem{
990986
Metric: "metric.with.dot.and-dash",
991987
Tags: map[string]string{},
992-
Dps: newDps(map[string]interface{}{}),
988+
Dps: newDps(map[string]interface{}{
989+
"1": 1.0,
990+
}),
993991
},
994992
expectedOutput: storepb.NewSeriesResponse(&storepb.Series{
995993
Labels: []storepb.Label{{Name: "__name__", Value: "metric:with:dot:and_dash"}},
996-
Chunks: []storepb.AggrChunk{},
994+
Chunks: []storepb.AggrChunk{{MinTime: 1, MaxTime: 1}},
997995
}),
998-
expectedChunkTypes: []storepb.Aggr{},
996+
expectedChunkTypes: []storepb.Aggr{storepb.Aggr_RAW},
999997
},
1000998
{
1001999
input: opentsdb.QueryRespItem{
@@ -1187,13 +1185,22 @@ func TestConvertOpenTSDBResultsToSeriesResponse(t *testing.T) {
11871185
expectedChunkTypes: []storepb.Aggr{storepb.Aggr_RAW},
11881186
},
11891187
}
1190-
for _, test := range testCases {
1188+
for i, test := range testCases {
11911189
store := NewOpenTSDBStore(
11921190
log.NewJSONLogger(&testLogger{t}), nil, nil, time.Duration(0), 1*time.Minute, test.storeLabels, nil, nil, false, true, "foo")
11931191
converted, _, err := store.convertOpenTSDBResultsToSeriesResponse(&test.input)
11941192
if err != nil {
11951193
t.Errorf("unexpected error: %s", err.Error())
11961194
}
1195+
if converted == nil && test.expectedOutput == nil {
1196+
continue // Expected nothing, got nothing.
1197+
}
1198+
if test.expectedOutput == nil {
1199+
t.Errorf("expected nil, got %v", converted)
1200+
}
1201+
if converted == nil {
1202+
t.Errorf("%d: expected response, got nil", i)
1203+
}
11971204
if len(converted.GetSeries().Labels) == len(test.expectedOutput.GetSeries().Labels) {
11981205
// Labels must be alphabetically sorted
11991206
for i, label := range converted.GetSeries().Labels {
@@ -1206,7 +1213,7 @@ func TestConvertOpenTSDBResultsToSeriesResponse(t *testing.T) {
12061213
t.Errorf("number of tags does not match")
12071214
}
12081215
if len(test.expectedOutput.GetSeries().Chunks) != len(converted.GetSeries().Chunks) {
1209-
t.Error("number of chunks does not match")
1216+
t.Errorf("%d: number of chunks does not match", i)
12101217
}
12111218
for ci, chunk := range test.expectedOutput.GetSeries().Chunks {
12121219
convertedChunk := converted.GetSeries().Chunks[ci]

0 commit comments

Comments
 (0)