Error when I use regex in Grafana Loki tenant ID when creating a data source

Hi. I’ve been working with multi-tenant setups in Grafana Loki and have successfully used regex to filter tenants in log queries, for example:

{service_name="loki", __tenant_id__=~"m.+"} |= ``

The tenant in the Datasource has been statically defined example: my_org_1, or_org_2, …

I would like to make it more flexible with regex because of the dynamic environment.
The idea would be to use Regex not only in the query, but also in the Datasource creation. Think as an admin view.

However, when I try to create a data source in Grafana using a similar regex pattern for the tenant ID, like m.+, I receive an error:

Error: tenant ID ‘m.+’ contains unsupported character ‘+’

I expected to use regex in the tenant ID field to handle dynamic or multiple tenants, but it seems the data source configuration doesn’t support this.

  • Is there a workaround or a different approach to achieve the same result?
  • Why is regex allowed in queries but restricted when configuring the data source?

Any guidance or insights would be appreciated!

I don’t do tenant this way, so I could be wrong, but you may need to enable multi_tenant_queries_enabled to use regex for tenant_id. See Multi-tenancy | Grafana Loki documentation

Hi. multi_tenant_queries_enabled is already enabled and functioning properly in my setup. The challenge happens because my environment is dynamic, with tenants being added and removed frequently. As a result, I have to constantly update the regex (e.g., org1|org2|org3...) to match the active tenants.

It would be ideal if there were a method to capture all tenants automatically—something like using a wildcard (*) to cover everything.

And since the wildcard already works for queries I was wondering if it was possible to work for datasources as well.

Then I am not sure, it’s possible that it’s not do-able.

I personally try to keep a static tenants, unfortunately i havent’ had this use case and I am not quite sure how to deal with it. Perhaps you might consider whether keeping a static tenant might be possible? For the most part I’d argue that if your tenant changes all the time, perhaps they can be made into labels instead?

1 Like

I understand your point.

The issue with labels is that I would also like to provide a restricted datasource for the tenants
And another admin like datasource for operations.

But if its not possible its not a problem, the eventual maintenance on the static datasource as you mentioned is something we work with.

I appreciate your considerations.