-
Notifications
You must be signed in to change notification settings - Fork 12k
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 Request: template variable string manipulations #8259
Comments
Would be nice to be able to grab parts of variable, too, e.g. This would allow showing much nicer labels with repeat panels/rows (no need to show full value like |
any work around for this request yet? |
I'm trying to understand if this can be solved already via http://docs.grafana.org/reference/templating/#query-options |
In my case I have a But theses hostnames can be rather long: "srv42-nyc-prod.example.com" which can create line wrapping on small "Singlestat" panel for example. The ability to perform a substring/regex operation on my |
I have a similar use case to C-Duv. |
@billyaustin84 use the variable regex support for that |
I'm still uncertain where the manipulated variables are being used: in panel queries or somewhere else? I screenshot would help immensely. Also helpful, if @billyaustin84 and @C-Duv could confirm that they tried using http://docs.grafana.org/reference/templating/#query-options and failed. |
it would probably be used in series alias formatting & panel titles. |
When I initially added the support for |
For particular example I faced impossibility to just use simple conversions like |
I also have a use-case in which template variables represent applications, and these applications can be namespaced using These characters are removed in the metrics collection process and replaced by But then if you want to propose dynamic links in text panels for ex. to the app / API / whatever, you've to do the opposite transformation, ie. |
@davkal, I already used regex option for my It works for querying data (using But for labels in graphs's legend or in graph's title/description I sometimes want to display a shorter hostname (eg. I know I could change the variable's regex so that it only contains |
my example: as a result i can't query both nginx metrics and kubernetes metrics in one dashboard, it would be very useful if we could have some basic string manipulation in variables section, |
This is exactly one of my use-cases. I have metrics with part of the path in upper-case, and part in lower-case. It would be great to be able to create a single variable that could be transformed to fit either use-case, on a given dashboard. |
Also my usecase. I have some metrics that are tagged with the hosts IP address and some that are tagged with the hostname (which contains the ip address ip-x-x-x-x.blah) I'd like a way to use one variable to drive both queries. |
My issue: I filter queries using lowercase environment names but I want environments to show in UPPERCASE in template panel titles. So generally something like
|
My situation is I'm templating switches using the Zabbix plugin and what I need is the title will contain the interface name and description from the dashboard variable $ifalias. The metrics will use the interface name extracted from the variable and catenate with strings like ":Bits received :Bits sent etc" and dynamically create a panel for every interface on the switch. The only piece missing is having the interface description in the title as so far I'm just using a variable with the interface name. If I could just drop everything after the first space in the variable. The full string will be: (Ethernet3/1 Alias myinterfacedescription). I can use "Interface ${ifalias regex manipulation}|: Bits received" Is this possible now? I'm going to redo the Zabbix item prototype to drop the prepended IF and space so I don't need to worry about that. In the above example "item" field, replace I'm so close it would be amazing if I could do this. We're about to roll out a lot of switches and this will be a huge time saver. |
I also have a need for this. I take a list or measurements from InfluxDB as my variable, i need the first two parts of the measurement name to build consistent panels.
I wish for my variable list to be The result should be I can pick !!!!! |
My use-case is repeating rows over 'tuples' of tag values. If I could create variables like:
And then use only part of the variable in right part of the query (two different tags). This would allow me to create the kind of repeating dashboards that I want to. |
My use case is turning "host_example_com" to "host.example.com", something I thought would be easy to do... |
My use case is to be able to define a list of http endpoints to monitor, like: |
@Fanduzi can't you use regex to remove the |
+1 the ability to manipulate the strings like this would be immensely useful! |
For me, i'd like to maintain a single list of router/interface pairs e.g.
I'd like to have a repeating panel on this variable list, that then uses the first part before _ to populate the Host field (Zabbix plugin) and the second part the Item field within a larger regex itself if i can create nested variables that extract the parts of the parent variable to achieve this effect then that would work for me too |
I think it would be nice to have maintainers views on this instead of adding use-cases and use-cases year after year 😄 |
Thank you! Exactly what I was looking for! |
Any updates on this? My specific usecase is a Grafana Cloud instance with both logs and metrics datasources -- and I want to include a textbox on a dashboard with a |
Going to add my cry into the void for this seemingly-simple functionality! Would be a great gift if this were implemented, I literally just want to capitalize my dashboard titles with variables in them... It drives me nuts! 😵💫 |
I just wanted to chime in here that this would be a really useful feature to have 🙏 pls consider implementing this grafana team, ty ❤️ |
I just wanted to add that while I see regex option in query vaiables a the solution for most of the use-cases here, |
I would love to have this feature as well ps @yourbuddyconner 👋 |
I'm banging my head as well... I very much need this functionality |
As a temporary solution I use a hidden variable in between text box and actual query
|
not sure why this feature request is such long time pending now. |
Bump, would be useful for us as well. We have variables set in the format of |
👍 on this request. I needed to use prometheus' |
Wow, 6 years running... this might be some sort of record. I have been stretching my data source since a long time as this simple function is not available on the visualization front end.. |
It will be convenient for the dashboard of multiple data sources if this feature exists. |
Here's another specific real-world example where no solution is possible using regex from dashboard query variable or regex transform for panel legends... I have repeating rows for a variable specifying network interfaces. If these are virtual sub-interfaces (children of a parent physical interface), they are named like host_intf="router1_Eth0.123" where 123 is typically a VLAN ID. One property of these sub-interfaces is that there are no metrics related to errors or discards, but those metrics are associated with the underlying physical interface. I want to process the repeating row variable to strip the dot and trailing numbers, which yields the underlying physical interface label. Then I can have panels in a row that show the virtual subinterface traffic and QoS drops, alongside the physical interface Ethernet discards and errors (using the row variable with string processing prior to interpolation in the query for the panel). Without the straight-forward Grafana feature support for string processing a variable prior to interpolation, the only way I can do this is to redefine my label schema to include a reference to parent physical interface from each subinterface (either through the original metrics or through an "info" metric (e.g. * on() group_left() info_metric). But I have 2 million interfaces and 525,600 historical samples (1 year @ 1 min intervals). That's a big hassle to change, would take weeks just to load the data with 100% cpu even after I code it up, and it's millions extra in timeseries, etc. I suppose I could also create a relational database to map these (ugly) and then chain it as a query variable... but that wouldn't work to align each instance of the virtual interface in a repeated row, with the associated physical interface variable in a panel in that row (and have the appropriate association for the panel in each repeated row). There are many other examples- this is just a concrete one that doesn't need charts and numbers to understand (but it might need some network engineering domain specific knowledge to understand). |
Another use case. I have a master/detail view in a dashboard. With a datalink in a column in the top panel which updates the rows in the detail panel. I use in the data link for the master panel:
The problem is that var-alertnameselected is included TWICE because it is part of the builtin ${__all_variables} string. This works fine for single selection variables, as grafana seems to pick the first one. But when I change the variable to multi-select, it keeps adding more and more selected choices to the variable. I want the datalink to be:
|
I too have been watching this issue for ages hoping for such a feature. Variable name: RegexedReplacedVariable
Variable regex: Nasty workaround, but works... |
This has gone on a while, with some solutions made but nothing universal. I think the best next step for this ticket is to make feature requests for additional string manipulations that we currently don't support, and close this since we do support some for variables. |
Could you share more about how this can be used within a metrics query? At least if this workaround works we have something to use in the meantime |
So lets say you have a metric that has a label which is an interface's MAC address in a dashed format (aa-aa-aa-aa-aa-aa), and you want to filter that metric based on a MAC address from a templating variable (either from a label_values query, or just a user input text variable) but that variable's MAC address is in a colon form (aa:aa:aa:aa:aa:aa); |
The supported variable formats are here. If your use-case is not supported please open a new separate feature request with the specific use case or open a contribution with a variable formatter. I will close this issue as it has grown very large and contains quite a lot of use cases related to variable formats and encourage everyone to open issues for each separate case. |
@mdvictor Those are string formatting options, not string manipulation options. Quite a difference between that and what I asked for almost 7 years ago. But, this issue has 67 comments and 156 thumbs ups indicating support. If you would rather have a similar number of separate issues for the same thing instead of just one I won't stop you. |
@CyBeRoni I understand your concerns and closing this issue doesn't mean that we are never going implement a solution for it or accept a PR around it. This issue has indeed been up for a while and has also collected quite a number of use cases, making it difficult to see what is supported now and what isn't. At this moment we are also not looking into adding a universal solution for string manipulations in variables but would rather extend the format options linked above with ways to replace |
Ever spend hours on something you think will be simple, because it seems like such basic and obvious functionality that you're sure you must just be doing something wrong? You can't believe it's so difficult to get it working! Surely there's a solution that you're just missing, so you keep searching. Then you finally come across a Github issue that's many years old, full of people asking for that exact thing, just as incredulous as you that this isn't implemented... and you scroll through, hoping to find an answer, only to have your hopes crushed when you see the maintainers close the issue with no resolution. Yeah, I hate when that happens. |
Ok, so I'm not just complaining, I'll also add a solution I eventually came up with after lots of searching and cobbling together other, smarter people's ideas, in the hopes that it helps someone else. Here's the problem I was trying to solve. I have a dashboard variable named Those namespaces might have an associated database named something like I start with a standard variable named I use that
But the databases... well, I wish we would have just named them the same as the namespaces, but we didn't. So, here's how I'm getting this done. I created another var named
It looks like this: And here's that regex so you can copy/paste:
Then I can use the
I don't know if this is the best way to do this, or even a "right" way, but it seems to be working. 🤷 |
Any updates on this? The previous attempt #29754 was closed 3 years ago with the following reason:
Is there any alternative being worked on? Or can we revive this old contribution to introduce some kind of string manipulation for variables? I'm just adding my +1 to all the reasons previously mentioned. |
Is there a PR already tracking basic needs like replacing |
For now I found a workaround for what I wanted to do, sharing it here in case it can help anyone else: I have a panel with a Prometheus graph and the PromQL query contains a variable from the Grafana dashboard called |
It would be nice if Grafana template variables could have some string manipulations applied. This could be useful to be able to refer to data about the same thing from various datasources from a variable, where that thing is named slightly differently for technical reasons. For instance, if you have a website that you refer to by the name "some_site" in your systems, but you have an AWS S3 bucket for it where you can't use underscores, you could transform that name to 'some-site', or 'somesite' instead.
The immediately obvious ideas for what kind of manipulations to support would be the various variable substitutions that bash supports. In bash I would do the above example by using
${var//_/-}
or${var//_}
.The text was updated successfully, but these errors were encountered: