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] Allow customizing request parsing for /query handler #2884

Closed
wants to merge 5 commits into from

Conversation

vpranckaitis
Copy link
Collaborator

What this PR does / why we need it:

Allows customizing request parsing when handling PromQL's api/v1/query requests

Special notes for your reviewer:

Does this PR introduce a user-facing and/or backwards incompatible change?:

NONE

Does this PR require updating code package or user-facing documentation?:

NONE

@codecov
Copy link

codecov bot commented Nov 12, 2020

Codecov Report

Merging #2884 (c70d0f5) into master (07d0997) will decrease coverage by 0.0%.
The diff coverage is 80.0%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #2884     +/-   ##
========================================
- Coverage    71.9%   71.9%   -0.1%     
========================================
  Files        1096    1096             
  Lines       99792   99781     -11     
========================================
- Hits        71844   71830     -14     
- Misses      23028   23031      +3     
  Partials     4920    4920             
Flag Coverage Δ
aggregator 75.8% <ø> (-0.1%) ⬇️
cluster 85.0% <ø> (ø)
collector 84.3% <ø> (ø)
dbnode 79.0% <ø> (-0.1%) ⬇️
m3em 74.4% <ø> (ø)
m3ninx 73.1% <ø> (-0.1%) ⬇️
metrics 17.2% <ø> (ø)
msg 74.0% <ø> (+0.1%) ⬆️
query 68.8% <80.0%> (+<0.1%) ⬆️
x 80.1% <ø> (-0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 07d0997...1031323. Read the comment docs.

Copy link
Collaborator

@soundvibe soundvibe left a comment

Choose a reason for hiding this comment

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

LGTM in general.
Maybe we could also provide similar way of using custom request parser for instant read handler as well?

@@ -63,6 +76,18 @@ func NewReadInstantHandler(opts Options, hOpts options.HandlerOptions) http.Hand
return newReadInstantHandler(opts, hOpts, queryable)
}

// DefaultReadRequestParser returns the default function that parse read request arguments
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: . at the end.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// DefaultReadRequestParser returns the default function that parse read request arguments
// DefaultReadRequestParser returns the default function that parses read request arguments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -39,15 +39,20 @@ import (
"go.uber.org/zap"
)

// RequestParser is a function that parses request arguments
Copy link
Collaborator

@soundvibe soundvibe Nov 13, 2020

Choose a reason for hiding this comment

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

nit: . at the end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@soundvibe soundvibe removed their assignment Nov 13, 2020
@@ -63,6 +76,18 @@ func NewReadInstantHandler(opts Options, hOpts options.HandlerOptions) http.Hand
return newReadInstantHandler(opts, hOpts, queryable)
}

// DefaultReadRequestParser returns the default function that parse read request arguments
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// DefaultReadRequestParser returns the default function that parse read request arguments
// DefaultReadRequestParser returns the default function that parses read request arguments.

@@ -63,6 +76,18 @@ func NewReadInstantHandler(opts Options, hOpts options.HandlerOptions) http.Hand
return newReadInstantHandler(opts, hOpts, queryable)
}

// DefaultReadRequestParser returns the default function that parse read request arguments
func DefaultReadRequestParser(opts options.HandlerOptions) RequestParser {
Copy link
Collaborator

Choose a reason for hiding this comment

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

hOpts(for consistency with how options.HandlerOptions is called in another function).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -39,15 +39,20 @@ import (
"go.uber.org/zap"
)

// RequestParser is a function that parses request arguments
type RequestParser func(ctx context.Context, r *http.Request) (models.RequestParams, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you are planning to have more extension points like this (eg. ResponseHandler and so on), might be better to introduce an interface for them, so that you would have to inject a single thing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Maybe existing Options could be used for that?

// Options defines options for PromQL handler.
type Options struct {
PromQLEngine *promql.Engine
}

E.g. add a field for read request parser and create a function NewOptions(), which would set the default parser from the get-go. The latter could be changed on demand.

opts := prom.NewOptions(promEngine)
defaultParser := opts.ReadRequestParser
opts.ReadRequestParser = func(/* ... */) /* ... */ {
  params, err := defaultParser(/* ... */)
  // modify params
  return params, err
}

Should be easy to add additional ways to configure the handler in a backwards compatible way. We could also get rid of NewReadHandlerWithCustomParser function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally Options would be immutable (see other use cases).
I suggest to discuss the approach with @soundvibe .

Copy link
Collaborator

Choose a reason for hiding this comment

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

BTW there is an example of what I meant in my suggestion: https://github.com/m3db/m3/blob/2c170ebd9c4acf6f132213eb07bf7c87a1077b14/src/dbnode/storage/types.go#L1393-L1397
Not sure how applicable to your case, but this would be more convenient than function injecting from the point where you have to inject more than a single function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, something like that. But please treat it just as a suggesstion - you know your use case in full picture better than I do.

@vpranckaitis vpranckaitis requested a review from linasm November 13, 2020 15:01
@vpranckaitis vpranckaitis removed the request for review from linasm November 18, 2020 08:32
@vpranckaitis vpranckaitis deleted the vilius/custom_read_request_parser branch January 8, 2021 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants