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: Improve prometheus query variable editor #63529

Merged
merged 21 commits into from
Feb 27, 2023

Conversation

bohandley
Copy link
Contributor

This is the updated PR for the improved Prom Query Variable Editor that was reverted
The first was reverted because it did not migrate a variable created for a kubernetes-mixin using libsonnet. This is now handled and there are unit tests for this scenario.

Fixes #59559 (allow newlines in Prom query result variable query editor)
Fixes https://github.com/grafana/observability-metrics-squad/issues/48

What is this feature?
This changes the prometheus query variable editor to a group of selects and inputs to better explain the functions, arguments, and endpoint calls used in the prometheus variable queries.

Previous single input:
Screen Shot 2022-11-05 at 12 16 57 PM

New function select and variable inputs:
Screen Shot 2022-11-05 at 12 17 30 PM

Full proposal here.

Why do we need this feature?
This is in line with the Grafana 10 goal of "Getting Started." More clarity around creating variables in Prometheus will help onboard new customers.

Who is this feature for?
This feature is for anyone using the Prometheus datasource plugin who wants to create template variables.
This is for all users of prometheus datasource plugin, from expert to beginner.

Special notes for your reviewer:

  1. Please create a prometheus variable in the main branch, for example, label_names()
  2. Switch to the new branch and open the variable editor to see that the function has been correctly passed to the inputs.
  3. Check that the variables are returned.
  4. Look for confusing language and readability. Are the tooltips helpful?

@bohandley bohandley merged commit 2372b7c into main Feb 27, 2023
@bohandley bohandley deleted the bohandley/improve-prom-query-var-editor branch February 27, 2023 13:38
@samjewell
Copy link
Contributor

Exciting! Thanks for this 🙏

ryantxu pushed a commit that referenced this pull request Mar 2, 2023
* add prom query var editor with tests and migrations

* fix migration, now query not expr

* fix label_values migration

* remove comments

* fix label_values() variables order

* update UI and use more clear language

* fix tests

* use null coalescing operators

* allow users to query label values with label and metric if they have not set there flavor and version

* use enums instead of numbers for readability

* fix label&metrics switch

* update type in qv editor

* reuse datasource function to get all label names, getLabelNames(), prev named getTagKeys()

* use getLabelNames in the query var editor

* make label_values() variables label and metric more readable in the migration

* fix tooltip for label_values to remove API reference

* clean up tooltips and allow newlines in query_result function

* change function wording and exprType to query type/qryType for readability

* update prometheus query variable docs

* Update public/app/plugins/datasource/prometheus/components/VariableQueryEditor.tsx

Co-authored-by: Galen Kistler <109082771+gtk-grafana@users.noreply.github.com>

* handle jsonnet grafana as code variables

---------

Co-authored-by: Galen Kistler <109082771+gtk-grafana@users.noreply.github.com>
export interface PromVariableQuery extends DataQuery {
query?: string;
expr?: string;
qryType?: PromVariableQueryType;
Copy link
Member

Choose a reason for hiding this comment

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

@bohandley missspelled? This now in the persisted model, and we have this cryptic enum now numeric values in the persisted model (PromVariableQueryType)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@torkelo I meant to spell qryType that way but I can change it if that is not correct or not clear. When a user selects the query type of label names, label values, etc, this is the PromVariableQueryType. We use this to identify which should be selected in the new variable editor. This could use some refactoring to make that more clear.

All of this could be refactored though, as I originally built it to migrate to the new editor and then migrate back to the string to preserve the definitions in the variables list. I believe we are moving away from that so all of this could be much clearer with less code.

Copy link
Member

@torkelo torkelo Sep 20, 2023

Choose a reason for hiding this comment

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

@bohandley well this is far from clear and is now in millions of dashboard json files as this part of the saved model, so really hurts the as code story (where dashboards are defined using the save model). Not only is "qryType" a very ugly thing to have as a property name in code but also in the save model, AND it just very cryptic numbers (0, 1, 2, 3) that have no meaning, enums must always have string values so we can make sense of them when looking at the save model.

Copy link
Member

Choose a reason for hiding this comment

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

and why do do we have query property and expr property?? some code comments on what these properties are would also help :)

Copy link
Member

Choose a reason for hiding this comment

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

If anyone would change the order of that enum type without a migration many dashboards would now break

Copy link
Member

Choose a reason for hiding this comment

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

btw, think the PR as whole is awesome, just need to make sure the save model is clear and not fragile and inscrutable :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bohandley well this is far from clear and is now in millions of dashboard json files as this part of the saved model, so really hurts the as code story (where dashboards are defined using the save model). Not only is "qryType" a very ugly thing to have as a property name in code but also in the save model, AND it just very cryptic numbers (0, 1, 2, 3) that have no meaning, enums must always have string values so we can make sense of them when looking at the save model.

@torkelo thank you! This is true, I had not considered the code story as it relates to dashboards, my apologies. This makes total sense. To see qryType: 1 in the dashboard json is both ugly and unclear as it relates to the code story. I will make sure any enums have string values moving forward.

and why do do we have query property and expr property?? some code comments on what these properties are would also help :)

Yes! I can add the code comments to the appropriate spot. I have some in the editor but I can see that this doesn't help when developers are reading the types to understand building a dashboard as code. The query and expr property are both there because I also had to migrate from standard variable support (which uses expr) and custom variable support (which uses query). So both fields would be present. I can add code comments for that!

// 1. Changing from standard to custom variable editor changes the string attr from expr to query
// 2. jsonnet grafana as code passes a variable as a string
const variableQuery = variableMigration(query);

I am sure there is a cleaner way to write all of this now, and I will create an issue to clean up some some of this. I will also add a reference point to make sure that any changes in the save model are as you say "clear, not fragile and inscrutable" 💯 Thank you for the feedback @torkelo!

Copy link
Member

Choose a reason for hiding this comment

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

@bohandley great! 👍

Copy link
Contributor Author

@bohandley bohandley Sep 20, 2023

Choose a reason for hiding this comment

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

To track these issues: #75175 Thank you!

Comment on lines -52 to -57
toDataQuery(query: StandardVariableQuery): PromQuery {
return {
refId: 'PrometheusDatasource-VariableQuery',
expr: query.query,
};
}
Copy link
Member

Choose a reason for hiding this comment

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

@bohandley
Without this I am unsure how this works with old dashboards? I can only see the migration being done for the variable query editor not when queries are executed. But maybe I am missing it

Copy link
Member

Choose a reason for hiding this comment

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

I think I know why it works still now, so don't worry about the comment above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template Variables: Query-based. Newlines in query cause silent failure
5 participants