Skip to content

Commit

Permalink
[query] Remove empty series from output when dropping NaNs (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnikola authored May 30, 2019
1 parent f4c2694 commit 68580c0
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/query/api/v1/handler/prometheus/native/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,37 @@ func parseQuery(r *http.Request) (string, error) {
return queries[0], nil
}

func filterNaNSeries(series []*ts.Series) []*ts.Series {
filtered := series[:0]
for _, s := range series {
dps := s.Values().Datapoints()
hasVal := false
for _, dp := range dps {
if !math.IsNaN(dp.Value) {
hasVal = true
break
}
}

if hasVal {
filtered = append(filtered, s)
}
}

return filtered
}

func renderResultsJSON(
w io.Writer,
series []*ts.Series,
params models.RequestParams,
keepNans bool,
) {
// NB: if dropping NaNs, drop series with only NaNs from output entirely.
if !keepNans {
series = filterNaNSeries(series)
}

jw := json.NewWriter(w)
jw.BeginObject()

Expand Down
46 changes: 38 additions & 8 deletions src/query/api/v1/handler/prometheus/native/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/m3db/m3/src/query/util/logging"
xtest "github.com/m3db/m3/src/x/test"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -351,14 +352,6 @@ func TestRenderResultsJSONWithDroppedNaNs(t *testing.T) {
]
],
"step_size_ms": 10000
},
{
"metric": {
"biz": "baz",
"qux": "qaz"
},
"values": [],
"step_size_ms": 10000
}
]
}
Expand Down Expand Up @@ -429,3 +422,40 @@ func mustPrettyJSON(t *testing.T, str string) string {
require.NoError(t, err)
return string(pretty)
}

func TestSanitizeSeries(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

nan := math.NaN()
testData := []struct {
name string
data []float64
}{
{"1", []float64{nan, nan, nan, nan}},
{"2", []float64{nan, nan, nan, 1}},
{"3", []float64{nan, nan, nan, nan}},
{"4", []float64{nan, nan, 1, nan}},
{"5", []float64{1, 1, 1, 1}},
{"6", []float64{nan, nan, nan, nan}},
}

series := make([]*ts.Series, 0, len(testData))
tags := models.NewTags(0, models.NewTagOptions())
for _, d := range testData {
vals := ts.NewMockValues(ctrl)
dps := make(ts.Datapoints, 0, len(d.data))
for _, p := range d.data {
dps = append(dps, ts.Datapoint{Value: p})
}

vals.EXPECT().Datapoints().Return(dps)
series = append(series, ts.NewSeries([]byte(d.name), vals, tags))
}

series = filterNaNSeries(series)
require.Equal(t, 3, len(series))
assert.Equal(t, "2", string(series[0].Name()))
assert.Equal(t, "4", string(series[1].Name()))
assert.Equal(t, "5", string(series[2].Name()))
}
1 change: 1 addition & 0 deletions src/query/generated/mocks/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//go:generate sh -c "mockgen -package=downsample $PACKAGE/src/cmd/services/m3coordinator/downsample Downsampler,MetricsAppender,SamplesAppender | genclean -pkg $PACKAGE/src/cmd/services/m3coordinator/downsample -out $GOPATH/src/$PACKAGE/src/cmd/services/m3coordinator/downsample/downsample_mock.go"
//go:generate sh -c "mockgen -package=storage -destination=$GOPATH/src/$PACKAGE/src/query/storage/storage_mock.go $PACKAGE/src/query/storage Storage"
//go:generate sh -c "mockgen -package=m3 -destination=$GOPATH/src/$PACKAGE/src/query/storage/m3/m3_mock.go $PACKAGE/src/query/storage/m3 Storage"
//go:generate sh -c "mockgen -package=ts -destination=$GOPATH/src/$PACKAGE/src/query/ts/ts_mock.go $PACKAGE/src/query/ts Values"
//go:generate sh -c "mockgen -package=block -destination=$GOPATH/src/$PACKAGE/src/query/block/block_mock.go $PACKAGE/src/query/block Block,StepIter,SeriesIter,Builder,Step,UnconsolidatedBlock,UnconsolidatedStepIter,UnconsolidatedSeriesIter,UnconsolidatedStep"
//go:generate sh -c "mockgen -package=ingest -destination=$GOPATH/src/$PACKAGE/src/cmd/services/m3coordinator/ingest/write_mock.go $PACKAGE/src/cmd/services/m3coordinator/ingest DownsamplerAndWriter"
//go:generate sh -c "mockgen -package=transform -destination=$GOPATH/src/$PACKAGE/src/query/executor/transform/types_mock.go $PACKAGE/src/query/executor/transform OpNode"
Expand Down
141 changes: 141 additions & 0 deletions src/query/ts/ts_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 68580c0

Please sign in to comment.