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

[Feature] Parametrized adhoc queries #13109

Closed
bobrik opened this issue Aug 31, 2018 · 10 comments
Closed

[Feature] Parametrized adhoc queries #13109

bobrik opened this issue Aug 31, 2018 · 10 comments

Comments

@bobrik
Copy link
Contributor

bobrik commented Aug 31, 2018

Ad-hoc queries cannot have parameters right now (v5.2.1):

image

We use a Clickhouse datasource with a database that has 14016 fields. That's not usable for autocomplete and it even takes forever to download and parse JSON with all these fields:

To make this workable, we want to have additional settings for adhoc filters, in our case the filter would narrow down fields to a specific table instead of all databases and tables.

@belm0
Copy link
Contributor

belm0 commented Dec 9, 2019

Specifically, I'd like to have ad-hoc filters apply to only a limited set of metrics.

For example with Prometheus, we have a device_info metric with labels like "hardware_model", "software_version", etc. These labels do not exist in all metrics, so the ad-hoc filtering as it's currently implemented is not usable. I'd like an ad-hoc filter that applies only to the device_info metric.

@stevenvachon
Copy link
Contributor

stevenvachon commented Oct 15, 2020

This would be useful for the mongodb-datasource and wavefront-datasource enterprise plugins which currently need to rely on additional template variables (with hardcoded names) that act as links to values such as a query or metric name.

@scottlepp @ryantxu @torkelo

@scottlepp
Copy link
Contributor

@belm0 @bobrik One workaround is to create a template variable and reference that from getTagKeys/getTagValues.

@belm0
Copy link
Contributor

belm0 commented Oct 15, 2020

One workaround is to create a template variable and reference that from getTagKeys/getTagValues.

I'm not sure how the datasource API is used. Would you give more detail on how that would work?

@scottlepp
Copy link
Contributor

scottlepp commented Oct 16, 2020

When you create an adHoc filter you will see 2 drop downs added to your dashboard. One for the filter key and one for the filter value. When you select from the filter key, a method on your datasource class, getTagKeys, will be called to get the key values for the dropdown. When you select from the filter value, getTagValues is called.

If you only want keys/values related to device_info, you could create a template variable, something like "metric_link" and set the value to "device_info". Then in getTagKeys, you can reference that variable. Here is an example:

// partial datasource class

import { getTemplateSrv } from '@grafana/runtime';

async getTagKeys() {
    // ad hoc filters require a metric - use a known dashboard variable to define it
    const metric = getTemplateSrv().replace('$metric_link');  // device_info
    // your code here to fetch the keys
    const keys = await this.fetchKeys(metric);
    // must return an array: [{text: 'key'}] ... which is shown in the keys drop down
    return keys.map(k => ({ text: k}));
}

// the key you select is provided in options
async getTagValues(options) {
    // ad hoc filters require a metric - use a known dashboard variable to define it
    const metric = getTemplateSrv().replace('$metric_link');  // device_info
    // your code here to fetch the values for this metric 
    const values = await this.fetchValues(options.key, metric);
   // must return an array: [{text: 'value'}] 
    return values.map(v => ({ text: v}));
}

@belm0
Copy link
Contributor

belm0 commented Oct 16, 2020

Thank you for explaining-- I'm not sure how that applies to my use case:

  1. I want only keys/values related to device_info in the adhoc dropdown
  2. I want the selected key/value only applied to the device_info metric in queries

Screen Shot 2020-10-17 at 8 00 25 AM

@scottlepp
Copy link
Contributor

@belm0 create a "constant" template variable and set the value to "device_info". Then reference that variable in getTagKeys/getTagValues. Now you have the metric you want for your query.

@belm0
Copy link
Contributor

belm0 commented Oct 18, 2020

When an adhoc template variable is created, every metric of every query in the dashboard gets substituted with it. So if some dashboard query references another metric, say device_temperature, which does not contain the labels of device_info, then the adhoc key/value will be incorrectly substituted into it and the query results will be empty. Am I misunderstanding?

@natellium
Copy link
Contributor

Hey @bobrik thanks for the request! We're now using github discussions to track feature requests and improve how we engage in feature discovery, development and delivery. Therefore I'm closing this issue but if you still want your feature to be considered it'd be great if you could reopen it as a discussion! Here is a bit more info about how to open feature requests as discussions.

User essentials squad (deprecated) automation moved this from To be prioritised to Done Nov 17, 2021
@alexforster
Copy link

Per #73424 can we have this reopened?

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

No branches or pull requests

8 participants