Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] Remove empty series from output when dropping NaNs #1682

Merged
merged 2 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever true? If not, can we remove the option entirely perhaps?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty happy to remove it since I don't really know why you'd want to keep nans, but you'll have to convert Ben :p

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, if you're asking if it can theoretically be true, there's a config option to keep nans in the output; don't know why anyone would turn it on, but it's there :p

) {
// 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.