Error Budget Remaining for an SLO
Problem Statement
Given an availability SLO and observed request counts, compute how much of the error budget remains.
Background
For a target availability slo (e.g. 0.999) over total requests, the allowed failures (error budget) is (1 - slo) * total. With bad observed failures, the fraction of budget remaining is (budget - bad) / budget, clamped to [−∞, 1] but reported down to negative if exhausted. If the budget is 0 (slo == 1), remaining is 1.0 when bad == 0 else 0.0.
Your Task
def error_budget_remaining(slo, total, bad):
Return the fraction of error budget remaining, rounded to 4 decimals (may be negative).
Input Format
- slo (float in (0,1]), total (int), bad (int).
Output Format
- A float rounded to 4 decimals.
Sample
print(error_budget_remaining(0.99, 1000, 4))
Output:
0.6
Example
print(error_budget_remaining(0.99, 1000, 4))
0.6
- Calculate the total error budget allowed by the SLO: with a target availability of and total requests, the allowed failures are .
- Determine the remaining budget by subtracting the observed failures from the total budget: .
- Compute the fraction of the budget remaining by dividing the remaining budget by the total budget: .
- Round the result to 4 decimal places as required: remains .
- The final output is 0.6
Constraints
- budget =
(1 - slo) * total. - remaining =
(budget - bad) / budget; if budget == 0, return 1.0 (bad==0) else 0.0. - Round to 4 decimals; may be negative if overspent.