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

Add M3-Limit-Instance-Multiple header #3518

Merged
merged 2 commits into from
May 25, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func ParseLimit(req *http.Request, header, formValue string, defaultLimit int) (
return defaultLimit, nil
}

// ParseInstanceMultiple parses request instance multiple from header.
func ParseInstanceMultiple(req *http.Request, defaultValue float32) (float32, error) {
if str := req.Header.Get(headers.LimitInstanceMultipleHeader); str != "" {
v, err := strconv.ParseFloat(str, 32)
if err != nil {
err = fmt.Errorf(
"could not parse instance multiple: input=%s, err=%w", str, err)
return 0, err
}
return float32(v), nil
}
return defaultValue, nil
}

// ParseRequireExhaustive parses request limit require exhaustive from header or
// query string.
func ParseRequireExhaustive(req *http.Request, defaultValue bool) (bool, error) {
Expand Down Expand Up @@ -214,9 +228,13 @@ func (b fetchOptionsBuilder) newFetchOptions(
if err != nil {
return nil, nil, err
}

fetchOpts.SeriesLimit = seriesLimit
fetchOpts.InstanceMultiple = b.opts.Limits.InstanceMultiple

instanceMultiple, err := ParseInstanceMultiple(req, b.opts.Limits.InstanceMultiple)
if err != nil {
return nil, nil, err
}
fetchOpts.InstanceMultiple = instanceMultiple

docsLimit, err := ParseLimit(req, headers.LimitMaxDocsHeader,
"docsLimit", b.opts.Limits.DocsLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,20 @@ func TestTimeoutParseWithGetRequestParam(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, timeout, time.Millisecond)
}

func TestInstanceMultiple(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
m, err := ParseInstanceMultiple(req, 2.0)
require.NoError(t, err)
require.Equal(t, float32(2.0), m)

req.Header.Set(headers.LimitInstanceMultipleHeader, "3.0")
m, err = ParseInstanceMultiple(req, 2.0)
require.NoError(t, err)
require.Equal(t, float32(3.0), m)

req.Header.Set(headers.LimitInstanceMultipleHeader, "blah")
_, err = ParseInstanceMultiple(req, 2.0)
require.Error(t, err)
require.Contains(t, err.Error(), "could not parse instance multiple")
}
3 changes: 3 additions & 0 deletions src/x/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const (
// the number of time series returned by each storage node.
LimitMaxSeriesHeader = M3HeaderPrefix + "Limit-Max-Series"

// LimitInstanceMultipleHeader overrides the PerQueryLimitsConfiguration.InstanceMultiple for the request.
LimitInstanceMultipleHeader = M3HeaderPrefix + "Limit-Instance-Multiple"

// LimitMaxDocsHeader is the M3 limit docs header that limits
// the number of docs returned by each storage node.
LimitMaxDocsHeader = M3HeaderPrefix + "Limit-Max-Docs"
Expand Down