Ruan Bekker's Blog

From a Curious mind to Posts on Github

Generate Grafana Loki Log Links From Metric Label Values

In this tutorial we will generate Loki Log links from selected dropdown template variables in a Grafana Dashboard.

Context

To give more context, we have a Grafana Dashboard with all our services, and when you select that service you see all the metrics of that service, now if you want to see the logs of that service, the selected label values will be parsed to a log link which you can click and it will take you to the Loki Explorer and parse the label values to the log link, so your logql will already be generated for you.

In order to achieve this, our metrics and logs need to share the same labels and label values (environment, container_name) etc.

Dashboard Variables

First we have our environment variable:

image

And here we have our service variable:

image

Then for our container_name we have:

image

Notice the /^(.*?)-[0-9]/ thats just to strip the end, if we remove it it will be:

image

Grafana Dashboard

Now when we select the environment, service, we get presented with a Loki LogURL:

image

If we look at our dashboard links, under the dashboard settings:

image

The Logs Uri is:

1
https://grafana.mydomain.com/explore?orgId=1&left=%5B%22now-1h%22,%22now%22,%22Loki%22,%7B%22expr%22:%22%7Bcontainer_name%3D~%5C%22.*$container_name.*%5C%22%7D%22%7D,%7B%22mode%22:%22Logs%22%7D,%7B%22ui%22:%5Btrue,true,true,%22none%22%5D%7D%5D

Now when we select our label values from the dropdown for our service and we follow the link we will get:

image