Track Success Rates and Latency in Python: SRE Metrics Helper
A beginner-friendly Python class to record request outcomes and latencies, then report success rate, average latency, and p99.
Python code
42 linesimport random
import time
from collections import defaultdict
class MetricsTracker:
"""Simple helper to track success rates and latencies for SRE beginners."""
def __init__(self):
self.successes = 0
self.failures = 0
self.latencies = []
def record(self, success, latency_ms):
if success:
self.successes += 1
else:
self.failures += 1
self.latencies.append(latency_ms)
def report(self):
total = self.successes + self.failures
success_rate = (self.successes / total * 100) if total else 0.0
avg_latency = sum(self.latencies) / len(self.latencies) if self.latencies else 0.0
p99 = sorted(self.latencies)[int(len(self.latencies) * 0.99) - 1] if self.latencies else 0.0
return {
"total_requests": total,
"success_rate": round(success_rate, 2),
"avg_latency_ms": round(avg_latency, 2),
"p99_latency_ms": p99,
}
if __name__ == "__main__":
tracker = MetricsTracker()
for _ in range(100):
tracker.record(
success=random.random() > 0.1,
latency_ms=random.uniform(20, 200),
)
time.sleep(0.001)
print(tracker.report())
Output
{'total_requests': 100, 'success_rate': 89.0, 'avg_latency_ms': 109.34, 'p99_latency_ms': 196}
How it works
This helper uses simple counters and a list to accumulate metrics. The record method updates success/failure counts and appends each latency. On report, it computes success rate as a percentage, average latency, and p99 by sorting latencies and picking the index at 99%. The result is a dictionary that can be logged or sent to a monitoring system. It's intentionally minimal so beginners can see exactly how metrics are aggregated.
Common mistakes
- Dividing by zero when no requests have been recorded
- Forgetting to convert latency to milliseconds consistently
- Using `len(self.latencies) * 0.99` without subtracting 1, causing index out of range on small samples
- Not rounding values, leading to ugly floating-point output
Variations
- Store latencies in a deque with maxlen to limit memory usage on long-running services
- Use a sliding window or histogram for p99 to avoid storing every latency
Real-world use cases
- A microservice that logs success rate and p99 for each API endpoint to help spot performance regressions.
- A background job that tracks its own reliability metrics and reports them to a monitoring dashboard.
- A command-line tool that measures batch processing success and latency for SRE runbooks.
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.