Replace strings in Grafana® dashboards#
Sometimes, it is useful to replace all occurrences of a string in Grafana® metric expressions.
This page describes how to do that using the
aiven-string-replacer-for-grafana tool, which is on GitHub.
The approach described will work with your own Grafana® cluster or with an Aiven for Grafana® cluster
Building the tool requires a Go environment. Follow the Go installation instructions.
To build the tool from its source repository, run the following at the terminal prompt:
go install github.com/aiven/aiven-string-replacer-for-grafana
Values you will need#
The API key for accessing Grafana
The URL for the Grafana dashboard
The UID that identifies the Grafana dashboard
The old string, that you want to replace
The new string, that you want to use instead
Get the Grafana API key#
The key needs the Grafana API key to be able to edit the Grafana dashboards.
To get your API key (
Go to your Grafana® UI. Select the Configuration icon and then select the API keys tab
If you already have an appropriate API key available, save a copy of it.
Otherwise, select Add API key and fill in the Key name, Role and Time to live. Click Add and then save the new API key.
Role must be either Editor or Admin.
To get the Grafana dashboard URL and UID#
To get the dashboard URL and UID (
Go to the dashboard on which you want to change metric expression strings.
Save the dashboard URL (
Select the Dashboard settings icon, then select JSON Model. Save the dashboard UID from the JSON Model page (
Perform the replacement#
Use a command like the following to perform the replacement, changing the placeholder variable names to the values you collected above:
aiven-string-replacer-for-grafana \ -apikey GRAFANA_API_KEY \ -url GRAFANA_DASHBOARD_URL \ -from OLD_STRING \ -to NEW_STRING \ -uid GRAFANA_DASHBOARD_UID
For instance, use the following command to change all metric expressions that start with
elasticsearch_ to instead start with
aiven-string-replacer-for-grafana \ -apikey GRAFANA_API_KEY \ -url YOUR_DASHBOARD_URL \ -from elasticsearch_ \ -to opensearch_ \ -uid YOUR_DASHBOARD_UID
The change will be visible in the Query panel:
Before running the command, metrics start with
After running the command, metrics start with
Use the version history to revert#
If necessary, the Dashboard changelog page can be used to revert a change to a specific version.
Go to the dashboard that you modified.
Select the Dashboard settings icon, then select Versions.
This will show your Dashboard changelog, and you can use this to revert to an earlier version of the dashboard.