Lockstep variable values in template titles


I’ve been really enjoying the network monitoring with Grafana and Prometheus series by Kim Miroy. Thank you for those blog posts.

I’m using using Prometheus 2.28.0 and Grafana v8.3.2 with templates and I’m trying to configure each panel in a template with the “IfName ifAlias” in the title.

For example

et-0/0/1 - NTT Darkfibre #123456

Example metric
ifHCInOctets{hostname="switch-in-america",ifAlias="NTT Darkfibre #123456",ifDescr="Gigabit Ethernet0/0/1",ifIndex="10124",ifName="et-0/0/1",instance="",job="switch"}

I’ve tried this a few ways,

Variables Version 1
$hostname query_result(sum by (hostname)({job=~"switches"}))
hostname regex .*hostname="(.*?)".*
$interface query_result(ifHCInOctets{hostname="$hostname"})
interface regex .*ifName="(.*?)",.*

Variables Version 2

$hostname query_result(sum by (hostname)({job=~"switches"}))
$interface label_values(ifHCInOctets{hostname="$hostname"}, ifName)
$description label_values(ifHCInOctets{hostname="$hostname"}, ifAlias)

Obviously I can’t use regex to filter out multiple values using groups with the second version but that doesn’t matter as we’ll see below.

Within the variables configuration web page page using the regex below, the two regex groups return fine, however the second group value isn’t used in the panel title. [Filter variables with regex | Grafana Labs]

regex attempt /ifAlias="(?<text>[^"]+)|ifName="(?<value>[^"]+)/g

Advanced variables
I tried following the variable format found here Advanced variable format options | Grafana Labs

What I would have expected when using ${variable:text} was the concatenation of the port and description values but only one was displayed. If a description didn’t exist, then the port name was in the panel title. So it looks like these group values are treated separately still when referenced in the Panel title. That’s kind of neat, but not what i was looking for.

I’ve also tried creating a combination variable that references $interface and $description but it’s clear I’m running two for loops and I’d only ever return the first item of the second loop. The same issue with the second variable occurs if you simply add $description to the Title because the function returning the description variable isn’t in lockstep with the interface variable.

I imagine what’s happening in the variables return two discrete values so the behaviour is like two loops running.

for ifName in switch:
    return ifName
for description in switch
    return description

If I was writing some python to get the desired output it’d look more like this.

for port,desc in switch:
     return f"{port} {desc}"

Other findings
I’ve read through this SO post but this focus on combing labels for two metrics not concatenating label values within the same metric. prometheus - Combine label values from 2 different metrics: Grafana - Stack Overflow.

label_values({__name__=~ "namedprocess_namegroup_cpu_seconds_total|node_uname_info"}, job)

If there’s a nice way to do this I’d love to know. For now I have a C query in the panel that queries the description which is a hidden value legend.

Thank you for your time.