How to Implement an Error Budget Policy in Python
This code implements a mock error budget policy that decides whether to freeze deployments based on a simulated error rate and monthly freeze limits.
Python code
43 linesfrom datetime import datetime, timedelta
class ErrorBudgetPolicy:
FREEZE_WINDOW_HOURS = 24
MAX_FREEZES_PER_MONTH = 3
def __init__(self, budget_percentage=99.9):
self.budget_percentage = budget_percentage
self.freeze_count = 0
self.last_freeze_start = None
self.freeze_enabled = True
def should_freeze(self, deploy_time):
if not self.freeze_enabled:
return False
if self.freeze_count >= self.MAX_FREEZES_PER_MONTH:
return False
if self.last_freeze_start and deploy_time < self.last_freeze_start + timedelta(hours=self.FREEZE_WINDOW_HOURS):
return False
# Mock: freeze if error rate exceeds budget allowance
error_rate = 0.02 # simulated 2% error rate
allowed_error = 1 - (self.budget_percentage / 100)
return error_rate > allowed_error
def apply_freeze(self, deploy_time):
self.freeze_count += 1
self.last_freeze_start = deploy_time
self.freeze_enabled = False
return {"frozen": True, "freeze_expires": deploy_time + timedelta(hours=self.FREEZE_WINDOW_HOURS)}
def simulate_deploy():
policy = ErrorBudgetPolicy(budget_percentage=99.9)
deploy_time = datetime.now()
print("Initial policy:", policy)
if policy.should_freeze(deploy_time):
result = policy.apply_freeze(deploy_time)
print("Deploy frozen -", result)
else:
print("Deploy allowed")
if __name__ == "__main__":
simulate_deploy()
Output
Initial policy: <__main__.ErrorBudgetPolicy object at 0x7f8b1c0a3d30>
Deploy frozen - {'frozen': True, 'freeze_expires': datetime.datetime(2025, 3, 15, 10, 30, 0, 123456)}
How it works
The ErrorBudgetPolicy class tracks a monthly freeze budget (max 3) and a 24-hour cooldown window. should_freeze checks if the freeze budget is exhausted, if the cooldown is active, and compares a mocked 2% error rate against the allowed error derived from the budget percentage (0.1% for 99.9% budget). apply_freeze increments the counter, sets the cooldown, and disables further freezes. This mock lets you test the decision logic without real telemetry.
Common mistakes
- Forgetting that `freeze_enabled` is set to False after a freeze, preventing any future freezes in the same run.
- Hardcoding the error rate instead of passing it as a parameter, making the mock less flexible.
- Not resetting `freeze_count` monthly, so the limit is permanent in long-running processes.
Variations
- Use a real error rate from a monitoring API instead of the constant 0.02.
- Store freeze state in a database or Redis to survive restarts.
Real-world use cases
- Automating a deploy freeze when an SLO error budget is nearly exhausted during a service incident.
- Integrating with CI/CD pipelines to block risky releases when error budgets drop below a threshold.
- Enforcing a monthly cap on emergency releases to balance velocity and reliability.
Sponsored
More from Observability & SRE
- Adding a Correlation ID to Log Context in Python medium
- Calculate Error Rate from Log Stream in Python easy
- Check if a Timestamp Falls in a Daily Maintenance Window in Python easy
- Export Metrics with OTLP Mock in Python medium
- Generate Mock CPU and Memory Metrics in Python easy
- Generate Prometheus Text Exposition Format in Python easy
Keep learning
Related tutorials and quizzes for this topic.