Query Inspector
Introduction
The Query Inspector is one of Grafana's most powerful troubleshooting tools. It lets you peek under the hood of your dashboards to examine the exact queries being sent to your data sources, view the raw response data, and analyze query performance metrics. Whether you're debugging problematic panels, optimizing slow dashboards, or simply wanting to understand how Grafana interacts with your data sources, the Query Inspector is your go-to tool.
What is the Query Inspector?
The Query Inspector is a built-in Grafana tool that allows you to:
- Examine the raw queries sent to your data sources
- View the complete response data returned (before Grafana transforms it)
- Review query performance statistics
- Modify and re-run queries for testing
- Copy queries for external use or sharing
Think of it as a window into the communication between Grafana and your data sources, giving you transparency into this otherwise hidden process.
Accessing the Query Inspector
There are two ways to access the Query Inspector:
-
From a panel: Click on the panel title to open the panel menu, then select "Inspect" → "Query".
-
From edit mode: While editing a panel, click the "Query Inspector" button in the query editor toolbar.
Query Inspector Interface
Once opened, the Query Inspector provides several tabs:
- Query: Shows the actual queries being sent to the data source
- Response: Displays the raw data returned from the data source
- Stats: Shows performance metrics for query execution
- Query History: Lists recently run queries (when enabled)
Understanding the Query Tab
The Query tab shows you the exact query that Grafana sends to your data source. Depending on your data source, this might be:
- SQL for MySQL, PostgreSQL, or SQL Server
- Prometheus query language
- InfluxDB query language
- Elasticsearch query DSL
- And many others
Here's an example of what you might see with a Prometheus data source:
{
"expr": "sum(rate(http_requests_total{job=\"api-server\"}[5m])) by (method, path)",
"range": true,
"instant": false,
"datasource": {
"type": "prometheus",
"uid": "abc123"
},
"format": "time_series"
}
This view is particularly useful when:
- Troubleshooting why a query isn't returning expected data
- Understanding how Grafana translates your visual query builder selections into actual queries
- Sharing query details with colleagues or in support tickets
Working with the Response Tab
The Response tab shows the raw data returned from your data source before Grafana applies any transformations or visualizations. This can help you:
- Verify that the data source is returning the expected data
- Understand the structure of the data Grafana is working with
- Identify issues with the data that might affect visualization
For example, with a Prometheus data source, you might see:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"method": "GET",
"path": "/api/users"
},
"values": [
[1629456000, "23.5"],
[1629456060, "25.1"],
[1629456120, "24.8"]
]
},
{
"metric": {
"method": "POST",
"path": "/api/users"
},
"values": [
[1629456000, "3.2"],
[1629456060, "2.9"],
[1629456120, "3.5"]
]
}
]
}
}
Analyzing Query Performance with the Stats Tab
The Stats tab provides valuable performance metrics about your queries:
- Query Time: How long the query took to execute
- Request Time: Total time including network overhead
- Data Processing Time: How long Grafana took to process the response
- Rows: Number of data points or records returned
This information is crucial for optimizing dashboard performance. A slow dashboard often has queries that:
- Return too much data
- Have inefficient filtering
- Perform complex calculations
- Cover very large time ranges
Example Stats output might look like:
Query: Prometheus
Time range: Last 6 hours
Query Time: 1.235s
Request Time: 1.312s
Data Processing Time: 0.077s
Rows: 5,432
Practical Use Cases
Use Case 1: Debugging Missing Data
If a panel shows "No data" or unexpected results:
- Open the Query Inspector
- Check the Query tab to verify the query looks correct
- Examine the Response tab to see if any data is being returned
- Look for errors or empty result sets
Common issues you might find:
- Incorrect time range selection
- Filter conditions eliminating all data
- Syntax errors in queries
- Permissions issues with the data source
Use Case 2: Optimizing Slow Dashboards
For dashboards that load slowly:
- Open Query Inspector for problematic panels
- Use the Stats tab to identify slow queries
- Check how many rows are being returned
- Look for ways to optimize by:
- Narrowing time ranges
- Adding more specific filters
- Using aggregations to reduce data points
- Using time buckets appropriate for the selected time range
Use Case 3: Learning Query Languages
The Query Inspector is an excellent learning tool:
- Create a panel using the visual query editor
- Open the Query Inspector to see how your visual selections translate to actual query code
- Make changes in the visual editor and observe how the query changes
- Experiment with modifying the raw query in the Query Inspector
Advanced Techniques
Modifying and Re-running Queries
The Query Inspector allows you to modify queries and re-run them without changing your panel:
- Edit the query in the Query tab
- Click "Run Query" to execute it
- View the results in the Response tab
This is useful for:
- Testing potential fixes for issues
- Experimenting with query optimizations
- Learning query syntax through trial and error
Query Exports
You can export queries from the Query Inspector for:
- Sharing with team members
- Documentation purposes
- Using in external tools
- Creating support tickets
Simply click the "Copy to clipboard" button next to the query.
Comparing Multiple Panels
If you're troubleshooting why two similar panels show different results:
- Open Query Inspector for the first panel
- Copy the query and response
- Repeat for the second panel
- Compare the queries to identify differences
- Compare the responses to understand data variations
Troubleshooting Common Issues with Query Inspector
Issue: Empty Response
If your Response tab shows an empty result:
- Check time range selection
- Verify filter conditions aren't too restrictive
- Ensure the query syntax is correct
- Confirm the data source has data for the specified period
Issue: Error Messages
Common error messages and solutions:
- Timeout errors: Your query is too complex or returning too much data. Try narrowing the time range or adding filters.
- Access denied: Check permissions for the data source.
- Syntax errors: Verify query format is correct for the data source.
- Data source unavailable: Check connection settings and data source health.
Issue: Unexpected Results
If you're getting unexpected data:
- Examine the raw response to understand what's being returned
- Check for data transformations applied in the panel settings
- Verify field mappings if using an unfamiliar data source
- Look for hidden null values or unexpected data types
Summary
The Query Inspector is an essential tool in every Grafana user's toolkit. It provides visibility into the query execution process, helps identify and resolve issues, and is invaluable for optimizing dashboard performance.
Key takeaways:
- Use Query Inspector to see exactly what queries are being executed
- Examine raw responses to understand the data structure
- Use performance stats to identify bottlenecks
- Modify and re-run queries to test potential improvements
- Export queries for documentation or sharing
By mastering the Query Inspector, you'll become more proficient at:
- Troubleshooting problematic dashboards
- Optimizing performance
- Understanding how Grafana interacts with your data
- Learning query languages for various data sources
Additional Resources
- Explore Grafana's Query Optimization documentation
- Learn about Grafana Alerting to be notified of issues
- Practice with the Grafana Play site
Practice Exercises
- Open a dashboard and use Query Inspector to examine the queries for three different panels. How do they differ?
- Take a slow-loading panel and use Query Inspector stats to identify performance bottlenecks. Can you optimize it?
- Use Query Inspector to export a complex query, then try to recreate the same query using the visual editor.
- Compare the raw response data with what's displayed in a visualization. How is Grafana transforming the data?
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)