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: Add capability to filter label names by metric in template variable editor #70452

Merged
merged 10 commits into from
Jun 22, 2023

Conversation

gtk-grafana
Copy link
Contributor

@gtk-grafana gtk-grafana commented Jun 21, 2023

What is this feature?

Currently if you want to use the label names in the variable editor, you have to use ALL of them. Which can be a tad much for many use-cases:
image

The "label names" function in Grafana is an interface for the prometheus /api/v1/labels endpoint. The new input field introduced in this PR provides a way to optionally provide a value for the match[] parameter.

Why do we need this feature?
To help Prometheus users build templated dashboards. People can have many thousands of labels, at which point dumping them all into a variable is not great UX. This allows folks to limit the labels returned to whatever metric names match the provided regex. Example: If I want the labels from node_arp_entries, and prometheus_build_info, I can write a query of node_arp_entries|prometheus_build_info which will return all the labels from those metrics.

image

Who is this feature for?
Prometheus users with lots of labels.

Which issue(s) does this PR fix?:
Fixes #70451

Special notes for your reviewer:
Also I wanted it to work with partial strings, for example node_arp_entr would be sent as .*node_arp_entr.*, if users want it to match multiple things like node_arp_|prometheus_build_ they'll need to build a proper regex node_arp_.*|prometheus_build_.*, which will output .*node_arp_.*|prometheus_build_.*.*, which looks weird but still should return the proper results, except this might require folks to assert the start and end of the strings explicitly (^node_arp_.*|^prometheus_build_entries$) to prevent any metrics that contain node_arp_ but don't start with it.

Edit: Another UI I can imagine is we provide repeatable inputs, each with another match[] parameter.

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. (No feature toggle, but I'm fine with GA release)
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@gtk-grafana gtk-grafana marked this pull request as ready for review June 21, 2023 19:32
@gtk-grafana gtk-grafana requested review from a team, grafanabot and lwandz13 as code owners June 21, 2023 19:32
@@ -164,15 +171,21 @@ export const PromVariableQueryEditor = ({ onChange, query, datasource }: Props)
}
};

const onLabelNamesMatchChange = (regex: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we want to only call onchange with the blur event?

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 would argue so, running a query on change only works on prometheus instances where you don't mind running extra or partial queries. Onblur gives you time to write whatever regex/metric names you want without pinging the server before you're ready.

/**
* Call onchange for metric change if metrics names (regex) query type
* Debounce this because to not call the API for every keystroke.
*/
const onMetricChange = debounce((value: string) => {
const onMetricChange = (value: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was the debounce not working correctly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't debouncing, it would just delay each execution 300ms, so three keystrokes would cause 3 queries

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch!

@@ -967,7 +967,7 @@ export class PrometheusDatasource
// this is used to get label keys, a.k.a label names
// it is used in metric_find_query.ts
// and in Tempo here grafana/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx
async getTagKeys(options?: any) {
async getTagKeys(options?: { series: string[] }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🏆 🎉

…eryEditor.test.tsx

Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
@gtk-grafana gtk-grafana added this to the 10.1.x milestone Jun 22, 2023
@gtk-grafana gtk-grafana self-assigned this Jun 22, 2023
@gtk-grafana gtk-grafana changed the title Prometheus: label_names with optional match[] Prometheus: Add ability to filter by metric names with label names function in template variable editor Jun 22, 2023
@gtk-grafana gtk-grafana changed the title Prometheus: Add ability to filter by metric names with label names function in template variable editor Prometheus: Add feature to filter label names by metric (regex) in template variable editor Jun 22, 2023
@gtk-grafana gtk-grafana changed the title Prometheus: Add feature to filter label names by metric (regex) in template variable editor Prometheus: Add capability to filter label names by metric (regex) in template variable editor Jun 22, 2023
@gtk-grafana gtk-grafana changed the title Prometheus: Add capability to filter label names by metric (regex) in template variable editor Prometheus: Add capability to filter label names by metric in template variable editor Jun 22, 2023
Copy link
Contributor

@bohandley bohandley left a comment

Choose a reason for hiding this comment

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

LGTM!

@gtk-grafana gtk-grafana merged commit 7cb1f65 into main Jun 22, 2023
28 checks passed
@gtk-grafana gtk-grafana deleted the gtk-grafana/label_names-match-param branch June 22, 2023 19:11
LudoVio pushed a commit that referenced this pull request Jun 26, 2023
…e variable editor (#70452)

* Adds new text input in prometheus template variable UI that allows label names function to filter values by metric. 
Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
harisrozajac pushed a commit that referenced this pull request Jun 30, 2023
…e variable editor (#70452)

* Adds new text input in prometheus template variable UI that allows label names function to filter values by metric. 
Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prometheus: label_names() support for Prometheus match parameter
4 participants