Query History in Grafana
Introduction
Query History is a powerful feature in Grafana that allows you to track, search, and reuse queries you've previously executed. As you build dashboards and explore data, you'll often write multiple queries - some successful, some not. Instead of rewriting queries from scratch or maintaining a separate record, Query History automatically saves all your queries, making your workflow more efficient.
This feature is especially helpful when:
- You need to recover a query after accidental changes
- You want to reuse complex queries in different panels
- You're iterating on query development and want to compare different versions
- You need to share queries with team members
Understanding Query History Basics
Query History works across all Grafana data sources including Prometheus, InfluxDB, Loki, MySQL, and others. Let's explore how to access and use this feature.
Accessing Query History
To access Query History:
- Open any Grafana dashboard or create a new one
- Click on "Add panel" or edit an existing panel
- In the query editor, look for the clock icon in the top toolbar
- Click on the clock icon to toggle the Query History panel
Query History Interface
The Query History panel is divided into several components:
- Search bar: Filter queries by content or comments
- Data source selector: Filter queries by data source
- Query list: Chronological list of your previous queries
- Query details: Preview of the selected query
- Action buttons: Star, copy, or comment on queries
Working with Query History
Now let's explore the practical ways to use Query History in your Grafana workflow.
Viewing and Searching Queries
When you open Query History, you'll see a list of all your previous queries sorted by time, with the most recent at the top. Each entry shows:
- The data source used (Prometheus, Loki, etc.)
- A snippet of the query text
- When it was last run
- Any comments you've added
- Whether it's starred
To find specific queries:
// Example: Search all Prometheus queries containing "memory"
// In the search field:
dataSource:"Prometheus" memory
Starring Important Queries
If you have queries you use frequently or want to keep for reference:
- Hover over a query in the history list
- Click the star icon to mark it as a favorite
- Use the "Starred" tab to quickly access your favorite queries
Adding Comments
Adding descriptive comments helps you remember what a query does:
- Select a query from the history
- Click the comment icon (speech bubble)
- Enter your comment in the text field
- Click "Save comment"
// Example comment
"This query shows memory usage spikes over the last 24 hours with 1m resolution"
Reusing Queries
To reuse a query from your history:
- Find the query in your history list
- Click on it to view details
- Click "Use query" to apply it to the current panel
- Alternatively, click the copy icon to copy the query to your clipboard
Managing Query History Settings
Grafana allows you to customize how Query History works:
- Go to Configuration → Preferences
- Scroll down to the "Query history" section
- Set your preferred retention period (default is 7 days)
- Choose whether to keep starred queries indefinitely
Practical Examples
Let's look at some real-world examples of how Query History can improve your workflow.
Example 1: Iterative Query Development
When developing complex queries, you often make incremental changes:
# First attempt - basic CPU usage
rate(node_cpu_seconds_total{mode="idle"}[1m])
# Second attempt - filter by instance
rate(node_cpu_seconds_total{mode="idle",instance="server-01:9100"}[1m])
# Final query - calculate CPU usage percentage
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)
With Query History, you can easily:
- See your progression of query development
- Go back to any version if a change doesn't work as expected
- Compare different approaches
Example 2: Cross-Panel Query Reuse
When building dashboards with related panels:
- Create and perfect a query in one panel
- Access Query History to find the query
- Copy the query to use as a starting point for related panels
- Modify as needed for the new context
Example 3: Sharing Queries with Team Members
When collaborating on dashboards:
- Develop a useful query
- Star it and add a detailed comment
- Show your colleague where to find it in Query History
- They can copy and use it in their own panels
Advanced Features
Query History Tabs
The Query History panel includes several tabs to help organize your queries:
- Query history: All queries, newest first
- Starred: Only queries you've marked as favorites
- Comments: Only queries with comments
Bulk Actions
You can perform actions on multiple queries:
- Use the checkboxes to select multiple queries
- Use the bulk action buttons to:
- Delete selected queries
- Star/unstar multiple queries
Persistent Storage
Query History is stored in your browser's local storage by default, but when you're signed in to Grafana, your query history is also saved to the Grafana database and available across devices and browsers.
Common Issues and Troubleshooting
Missing Queries
If you don't see expected queries in your history:
- Check if you've exceeded the retention period (default 7 days)
- Verify you're logged in if using across multiple devices
- Make sure you haven't filtered out the queries with search settings
Browser Storage Limitations
If using local storage only (not logged in):
- Browser storage limits may restrict very large query histories
- Clearing browser data will erase your local query history
Summary
Query History is an essential productivity tool for Grafana users that:
- Automatically saves all your queries for easy reference
- Allows you to search, filter, and organize your query collection
- Makes it simple to reuse and iterate on complex queries
- Helps with collaboration by providing a way to document and share queries
By leveraging Query History effectively, you'll spend less time rewriting queries and more time gaining insights from your data.
Additional Resources
- Try using keyboard shortcuts: Press
Ctrl+K
(orCmd+K
on Mac) in the query editor to quickly access your query history - Practice star and comment features to organize your most useful queries
- Experiment with different search techniques to quickly find specific queries
Exercise: Query History Workflow
- Create three different queries on a Grafana dashboard
- Star one of them and add a comment
- Close the dashboard and open a new one
- Use Query History to find and reuse one of your previous queries
- Try searching for specific terms in your query history
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)