Syntax for legend options {{hostname}}?

What is the syntax for Legend options. Specifically where I currently have {{hostname}} {{ifName}} I would like it to show hostname and (ifAlias or ifDescr or ifName) in that order.

Something like {{hostname}} {{ifAlias|ifDescr|ifName}} with preference on the left.

If no other options are available other than label value substitution, how could I accomplish this? Not all of my interfaces have descriptions of aliases defined, but if they are defined I prefer to see those instead of the interface name.

1 Like

@lonelysysop I wondered the same. After searching for a while in the documentation ang web search results, I looked into the source code.

According to grafana/packages/grafana-data/src/utils/legend.ts at 71b98163e52306c5da36b243bd74e74f634920a0 · grafana/grafana · GitHub the legend template format only supports simple field replacement

/** replace labels in a string.  Used for loki+prometheus legend formats */
export function renderLegendFormat(aliasPattern: string, aliasData: Labels): string {
  const aliasRegex = /\{\{\s*(.+?)\s*\}\}/g;
  return aliasPattern.replace(aliasRegex, (_, g1) => (aliasData[g1] ? aliasData[g1] : g1));
}

Associated tests at grafana/packages/grafana-data/src/utils/legend.test.ts at main · grafana/grafana · GitHub

Looks a workaround would be to add a transform step.

Your need for conditional formatting would need a deeper look.

In my case, I was expecting to format string and truncate it. The promising format-stringis currently in public preview would need testing (not yet available in my grafana environment)

:sweat_smile:Came here a year later just to say thanks. This must be the poorest documented feature I’ve ever encountered in a project the size of Grafana, lol. I was in particular trying to format a string for padding, in a situation where I have combinations of Kafka groups and topics and wanted to display {{topic}} | {{consumergroup}} nicely aligned since topic names have all sorts of widths.

I guess I just can’t do that without more work than justifiable just for my compulsive behavior