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

Prometheus: Variable query editor improvements #69884

Merged
merged 11 commits into from
Jun 16, 2023

Conversation

bohandley
Copy link
Contributor

@bohandley bohandley commented Jun 11, 2023

Fixes #69859

What is this feature?
The Prom variable query editor allows a user to choose a label_values query type. This allows a user to choose a label name and an optional metric. These are the following updates to the label_values query type inputs.

  • Use the metric select in the Prom query variable editor.
  • Allow the metric to be filtered with optional label filters.
  • Allow the metric to be clearable.
  • When a metric is selected, filter the options of required label name select dropdown
  • Add variables to the options in the select drop downs (labels and metric for the label_values query type).
  • Styling changes and copy

Screenshot 2023-06-11 at 7 03 17 PM

Screenshot 2023-06-11 at 7 03 27 PM

Why do we need this feature?
The previous metric input for the label_values query type was a text input that was too small and the metric was not able to be filtered. There was also a recent PR to allow for the label_values label input to accept variables. These changes listed above add functionality to the query variable editor and allow the customer greater flexibility in creating a variable.

Who is this feature for?
Anyone using the prom query variable editor.

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@bohandley bohandley added area/dashboard/templating datasource/Prometheus area/frontend no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes labels Jun 11, 2023
@bohandley bohandley added this to the 10.1.x milestone Jun 11, 2023
@bohandley bohandley self-assigned this Jun 11, 2023
@bohandley bohandley changed the title Bohandley/use metric select in var editor Prometheus: Variable query editor improvements Jun 12, 2023
@bohandley bohandley marked this pull request as ready for review June 12, 2023 12:16
@bohandley bohandley requested review from a team and grafanabot as code owners June 12, 2023 12:16
@bohandley bohandley removed the request for review from grafanabot June 12, 2023 13:56
@gtk-grafana
Copy link
Contributor

@bohandley looks like this might address #69859

@itsmylife
Copy link
Contributor

I check out the branch and open my existing dashboard with a bunch of variables in it.

Manual testing results:

  • When I keep adding new label filters the row gets wider. I expect it to be wrapped.
  • After I select a Label I expect to see the results. I have to click another place to trigger to run the query

@itsmylife
Copy link
Contributor

I think the definition column will be filled in another PR, right?
image

Copy link
Contributor

@itsmylife itsmylife left a comment

Choose a reason for hiding this comment

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

Some nitpicks but the rest is 🚀

variableEditor,
}: MetricsLabelsSectionProps) {
// fixing the use of 'as' from refactoring
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the type is QueryBuilderLabelFilter[]. Do we need @ts-ignore here?

I see that it is about LabelFilter.tsx and specifically it is about onLabelsChange function. Could you please check if that's possible to have the proper types? Let's avoid ts-ignore as much as we can.

Copy link
Contributor Author

@bohandley bohandley Jun 14, 2023

Choose a reason for hiding this comment

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

@itsmylife tsmylife This is what we had before

onChange={onChangeLabels as (labelFilters: Array<Partial<QueryBuilderLabelFilter>>) => void}

https://github.com/grafana/grafana/pull/69884/files#r1229794280

and

  const onChangeLabels = (labels: QueryBuilderLabelFilter[]) => {
    onChange({ ...query, labels });
  };

https://github.com/grafana/grafana/pull/69884/files#r1229795429

When I moved this into a new file, using onChangeLabels as was not permitted by prettier. I removed the as and using the type QueryBuilderLabelFilter[] this caused a type error.
https://github.com/grafana/grafana/pull/69884/files#r1229803709

I am not sure how to fix this as the function definition. The deinition, onChangeLabels as (labelFilters: Array<Partial<QueryBuilderLabelFilter>>) => void}, is same in the LabelFilters.tsx component but betterer won't allow using as. Do you have any suggestions?

Screenshot 2023-06-14 at 11 27 05 AM

Copy link
Contributor

Choose a reason for hiding this comment

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

I do not have any suggestions but I checked it briefly and realized that it is not a simple change to make. So we can leave it like this and address it later on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we create an issue with the tech-debt label to address this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will create the issue once this PR is merged to track this.

const onGetLabelNames = async (forLabel: Partial<QueryBuilderLabelFilter>): Promise<SelectableValue[]> => {
// If no metric we need to use a different method
if (!query.metric) {
// Todo add caching but inside language provider!
Copy link
Contributor

Choose a reason for hiding this comment

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

@gtk-grafana I see it in this PR. Do we track this todo in somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove the todo, the prometheusResourceBrowserCache will cache this

debounceDuration={datasource.getDebounceTimeInMilliseconds()}
getLabelValuesAutofillSuggestions={getLabelValuesAutocompleteSuggestions}
labelsFilters={query.labels}
// eslint-ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Link for discussion about not using ts-ignore

@@ -35,171 +32,6 @@ export interface Props {
export const PromQueryBuilder = React.memo<Props>((props) => {
const { datasource, query, onChange, onRunQuery, data, showExplain } = props;
const [highlightedOp, setHighlightedOp] = useState<QueryBuilderOperation | undefined>();
const onChangeLabels = (labels: QueryBuilderLabelFilter[]) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Link for discussion about not using ts-ignore

debounceDuration={datasource.getDebounceTimeInMilliseconds()}
getLabelValuesAutofillSuggestions={getLabelValuesAutocompleteSuggestions}
labelsFilters={query.labels}
onChange={onChangeLabels}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Link for discussion about not using ts-ignore

@bohandley
Copy link
Contributor Author

bohandley commented Jun 14, 2023

@itsmylife

I check out the branch and open my existing dashboard with a bunch of variables in it.

Manual testing results:

  • When I keep adding new label filters the row gets wider. I expect it to be wrapped.

*EDIT labels are now wrapped

  • After I select a Label I expect to see the results. I have to click another place to trigger to run the query

*EDIT see latest commit for improved behavior for selects and inputs. Running onChange now for selecting a label

@bohandley
Copy link
Contributor Author

@itsmylife wrap is working, looks better now 🎁

Labelfiltersflex.mov

@itsmylife
Copy link
Contributor

The wrapping looks good.

I experienced strange behavior though. Probably not related to your changes but let's see.
I have a dashboard that has many variables defined. I clicked variables and click one of the existing ones which has the query type "label values". To test wrapping I clicked the plus button multiple times. It works nice and well. But after a while (a couple of seconds later) all my empty label filters are gone. I see that in the network tab, there are some queries that keep running in the background. Dashboard has a 5s refresh setting. Each time I got the result from api/v1/series endpoint the filters are gone.

@bohandley
Copy link
Contributor Author

The wrapping looks good.

I experienced strange behavior though. Probably not related to your changes but let's see. I have a dashboard that has many variables defined. I clicked variables and click one of the existing ones which has the query type "label values". To test wrapping I clicked the plus button multiple times. It works nice and well. But after a while (a couple of seconds later) all my empty label filters are gone. I see that in the network tab, there are some queries that keep running in the background. Dashboard has a 5s refresh setting. Each time I got the result from api/v1/series endpoint the filters are gone.

I was not able to reproduce this. It could be a timing issue due to dashboard setup? I have tried this with a prometheus instance and cortex but cannot reproduce this. After our discussion on slack, we are attributing this to a dashboard setup. I will make a note of this issue for future work. Thank you!

Screen.Recording.2023-06-16.at.10.05.27.AM.mov

@itsmylife
Copy link
Contributor

The wrapping looks good.
I experienced strange behavior though. Probably not related to your changes but let's see. I have a dashboard that has many variables defined. I clicked variables and click one of the existing ones which has the query type "label values". To test wrapping I clicked the plus button multiple times. It works nice and well. But after a while (a couple of seconds later) all my empty label filters are gone. I see that in the network tab, there are some queries that keep running in the background. Dashboard has a 5s refresh setting. Each time I got the result from api/v1/series endpoint the filters are gone.

I was not able to reproduce this. It could be a timing issue due to dashboard setup? I have tried this with a prometheus instance and cortex but cannot reproduce this. After our discussion on slack, we are attributing this to a dashboard setup. I will make a note of this issue for future work. Thank you!
Screen.Recording.2023-06-16.at.10.05.27.AM.mov

I created a brand new dashboard (I was using my old one) and tried again. So I cannot reproduce it either :) So I think it is good to go.

@bohandley bohandley merged commit 3425012 into main Jun 16, 2023
@bohandley bohandley deleted the bohandley/use-metric-select-in-var-editor branch June 16, 2023 19:03
LudoVio pushed a commit that referenced this pull request Jun 26, 2023
* refactor metric select and label filters, add variables to label names dropdown

* use MetricsLabelsSection in variable editor

* use variable editor UI with MetricsLabelsSection

* filter label names by optional metric and allow metric to be cleared

* fix tests

* testing types for onChangeLabels

* testing types for onChangeLabels

* testing types for onChangeLabels

* Ismails review, remove comment and indent

* wrap label filters

* improved behavior for selects and inputs, add tests and document behavior
@ricky-undeadcoders ricky-undeadcoders modified the milestones: 10.1.x, 10.1.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dashboard/templating area/frontend datasource/Prometheus no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

template variables/Query Type/Label Values optional Metrics field is is fixed size
4 participants