Templating regex exclude not working

I tried to exclude some hostname from the templating output.

Template query to get the hostname from InfluxDB,
SHOW TAG VALUES FROM system WITH KEY=host

As SHOW TAG VALUES doesn’t support time in WHERE clause, I tried to exclude some down hosts using template regex option.

Can you help me , how to exclude below hosts on templating regex option

ansys001.example.local
net001.example.local

Regex tried : /^(?!ansys.example.local$).*$/

You can use regex in the influxdb query as well to filter, or the regex option in grafana.

Not sure what the regex should look like.

Negative lookaheads are hard. Have you tried use the !~ operator (negative regex) in the the WHERE clause instead?

Something like: WHERE host !~ /^ansys*.example.local$/

I was just tackling this issue today.

My dashboard template variable is for a customer list from InfluxDB data source. The initial query looks like:
SHOW TAG VALUES FROM "series_name" WITH KEY = "customer"

Of course, I don’t want to look at any of my internal, test, or demo accounts, so I needed to use the regex to exclude those. I’m using the negative lookahead as daniellee mentioned, but since this is in the templating section and not the metrics, it’s a little different. My regex line has:
/^(?!abc|.*demo|.*test)/

This is working for me at this time. I hope it helps you out.

2 Likes

#Exclude all until remain what you want
/^(?!.postgres.|.pgsql-service.|kubelet.|haproxy-ingress.|.mssql-service.|kube-state-metrics.|billing-service.|default-http-backend.*|.jenkins.|.multideploy-service.|.nginx-service.|.openedge-service.|.project-db-service.|.project-db-test-service.|.project.|.source-service.|.mysql.|.rabbitmq.)/

#Exclude + Include feature (this is the best)
/(?=dev.|uat.|demo.|prod.|pre.|poc.)^(?!.postgres.|pgsql-service.*)/

#Include-only
/dev.|uat.|demo.|prod.|pre.|poc./

2 Likes

Does regex supports excluding value derived from other variable ? I want something like this for Prometheus,

Var 1 : Prometheus query returns Stack1, Stack2, Stack3
Var 2 : If I select Stack1, all services of Stack1

Something like chained variable but now in templating !
Or any other way to achieve this.

This worked for me. Thanks!