- 1 year ago
Prometheus not able to divide 2 values
If you're encountering issues when trying to divide two values in Prometheus, there are a few potential causes and solutions to consider:
Ensure Metric Types: Check that the metrics you're trying to divide are of the appropriate metric type. Prometheus has specific rules for division based on metric types. For example, you can divide two
counter
orgauge
metrics, but you cannot divide acounter
by ahistogram
.Convert to Scalar: If you're attempting to divide two metrics that have multiple time series, you need to convert them to scalar values using an aggregation function like
sum()
,min()
, ormax()
. For example:scssmy_metric / sum(another_metric)
This ensures that you have single scalar values for division.
Handle Division by Zero: If the denominator metric might have zero values, you should handle this scenario to avoid division by zero errors. One way to handle this is by using the
rate()
function or a conditional expression with theif()
function to prevent division when the denominator is zero.Example using
if()
function:scssmy_metric / if(another_metric > 0, another_metric, 1)
This will divide
my_metric
byanother_metric
ifanother_metric
is greater than zero; otherwise, it will divide by1
to prevent division by zero.Debug Expression: If the issue persists, try debugging the expression by evaluating the individual metrics separately to ensure they are returning the expected values. You can use the Prometheus expression browser or Grafana to verify the values of your metrics.
By considering these potential issues and applying the appropriate solutions, you should be able to perform division operations between metrics in Prometheus successfully.