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] Refactor query code, add warnings to prom output #2265

Merged
merged 18 commits into from
Apr 17, 2020

Conversation

arnikola
Copy link
Collaborator

No description provided.

@robskillington robskillington changed the title [query] refactor query code, add warnings to prom output [query] Refactor query code, add warnings to prom output Apr 14, 2020

func (c *ctxCanceller) WatchForCancel(
ctx context.Context,
cancel context.CancelFunc,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably don't need cancel func here since not used?

"warnings": [
"foo_bar",
"baz_qux"
],
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

promReadMetrics: newPromReadMetrics(taggedScope),
// timeoutOps: opts.TimeoutOpts(),
// keepEmpty: opts.Config().ResultOptions.KeepNans,
// instrumentOpts: opts.InstrumentOpts(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Believe can remove the comments now?

return h
}

func (h *PromReadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timer := h.promReadMetrics.fetchTimerSuccess.Start()
fetchOpts, rErr := h.fetchOptionsBuilder.NewFetchOptions(r)
defer timer.Stop()
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

ctx := context.WithValue(r.Context(), handler.HeaderKey, r.Header)
logger := logging.WithContext(ctx, h.opts.InstrumentOpts())

ParsedOptions, rErr := ParseRequest(ctx, r, h.opts)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a local var, so can just be lower camel cased yeah? i.e. parsedOpts

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, must have gotten dragged into a rename

opentracingext.Error.Set(sp, true)
logger.Error("range query error",
zap.Error(err),
zap.Any("ParsedOptions", ParsedOptions))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Name here I assume can be zap.Any("parsedOpts", parsedOpts)

numSteps := int64(params.End.Sub(params.Start) / params.Step)
maxComputedDatapoints := h.limitsCfg.MaxComputedDatapoints()
if maxComputedDatapoints > 0 && numSteps > maxComputedDatapoints {
func validateRequest(params *models.RequestParams, maxPoints int) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does params necessarily need to be a pointer type? Could it be just params models.RequestParams (I notice we do &params when we call this which might be unnecessary)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't think so; just kept the types as is generally

@@ -77,6 +72,12 @@ func read(
return emptyResult, err
}

// Detect clients closing connections.
if cancelWatcher != nil {
ctx, cancel := context.WithTimeout(ctx, fetchOpts.Timeout)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to defer cancel() here?

w.Header().Set("Content-Type", "application/json")
handleroptions.AddWarningHeaders(w, result.meta)
return result.series, params, nil
return ParsedOptions{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason not to create the cancel watcher here instead of later and tacking onto the ParsedOptions

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 trying to avoid taking a http.ResponseWriter into the read function

@@ -159,7 +170,8 @@ func testPromReadInstantHandler(
]
}
}
`, at0.Unix(), value0, at1.Unix(), value1))
`, jsonWarnings, at0.Unix(), value0, at1.Unix(), value1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should get rid of the fmt.Sprintf stuff and introduce types JSONMap, JSONArray and then add a MustPrettyJSONObject(t, ....).

I prototyped this here, looks easy to drop in I think?
https://play.golang.org/p/sTatAL6vaEE

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hah, nice :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, wouldn't using maps lead to mismatches in data? Also, just from my experience doing it in the last few minutes, building these structures seems more error prone than just writing up the expected JSON?

Copy link
Collaborator

@robskillington robskillington Apr 16, 2020

Choose a reason for hiding this comment

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

If we MustPrettyJSON both of them, then they should get the correct ordering as per JSON pretty printing behavior, should actually mean less possibility of writing a wrong JSON output matcher.

}

data, err := proto.Marshal(resp)
if err != nil {
h.promReadMetrics.fetchErrorsServer.Inc(1)
// h.promReadMetrics.fetchErrorsServer.Inc(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can restore this?

Copy link
Collaborator Author

@arnikola arnikola Apr 16, 2020

Choose a reason for hiding this comment

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

The metrics will get incremented in the calling function, which has the actual metrics

result, err := h.engine.ExecuteProm(ctx, query, queryOpts, fetchOpts)
// Detect clients closing connections.
if cancelWatcher != nil {
cancelWatcher.WatchForCancel(ctx, cancel)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like we pepper cancel watchers in a ton of places, is there a way to basically make this middleware instead?

Just seems very messy the setup/tear down of it (even though its useful itself).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah; I only saw these in a few places tbh; wouldn't be opposed to pulling it into some sort of middleware

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Might add a todo for this?

Copy link
Collaborator

@robskillington robskillington left a comment

Choose a reason for hiding this comment

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

LGTM

@arnikola arnikola merged commit eea6199 into master Apr 17, 2020
@justinjc justinjc deleted the arnikola/handler-refactor branch May 14, 2020 16:13
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.

2 participants