Grafana Error: "Failed to evaluate queries and expressions: failed to execute conditions: input data must be a wide series but got type long" - Explained and Solved
The Problem:
This Grafana error message pops up when you try to use a panel that requires data in a specific format, but your data source is providing a different type. It's like trying to fit a square peg in a round hole.
Scenario & Original Code:
Imagine you're trying to create a Grafana dashboard that shows the number of successful logins per hour. You're using a Prometheus data source, and your query looks like this:
sum(rate(login_success{status="success"}[1h]))
This query calculates the rate of successful logins per hour. However, when you try to display this data in a panel that expects a "wide series" (multiple data points per time interval), you encounter the "input data must be a wide series but got type long" error.
Analysis and Clarification:
The error occurs because the sum
function in Prometheus returns a single value (a long
) for each time interval. This is different from a "wide series," which would have multiple data points for each time interval.
Solution:
To fix this, you need to modify your query to return a "wide series." Here are two common approaches:
1. Use the label_values
Function:
Instead of summing the rate directly, you can use the label_values
function to get a list of unique values for a specific label. In this case, you might want to use the user
label to distinguish between different users.
label_values(login_success{status="success"}, user)
This query will return a list of user names, which can be used to create a wide series in your Grafana panel.
2. Use the histogram_quantile
Function:
If your data source already provides a histogram, you can use the histogram_quantile
function to extract specific quantiles from the histogram, creating a wide series with multiple data points.
histogram_quantile(0.9, rate(login_success{status="success"}[1h]))
This query extracts the 90th percentile of the rate of successful logins per hour.
Additional Value:
- Understand the difference between "wide series" and "long" data types: A "wide series" has multiple data points for each time interval, while a "long" data type has a single value for each time interval.
- Use the
label_values
andhistogram_quantile
functions strategically: These functions can help you transform your data into the format required for your Grafana panel. - Experiment with different queries: Don't be afraid to try different queries and experiment until you find the one that works best for your specific data and panel.
References and Resources:
Conclusion:
By understanding the root cause of the "input data must be a wide series but got type long" error and learning how to manipulate data with Prometheus functions, you can easily resolve this issue and create informative Grafana dashboards that display your data effectively.