Sub topic 17
Monitoring crawl health and metrics
This chapter assumes you have read the chapters on the URL frontier, politeness, error handling, and concurrency. It also assumes you can read short Python programs. A crawler runs for hours and fails quietly, so you need a few numbers that show whether it is working.
Why a crawler needs metrics
A crawler rarely crashes with a clear error. More often it slows down, gets blocked by a host, or loops inside a crawl trap while the logs look normal. Counters and rates make these problems visible.
Metrics worth tracking
| Metric | What it tells you |
|---|---|
| Pages fetched per second | Overall throughput. A sudden drop points to blocking, slow hosts, or a stuck worker. |
| Status code counts (2xx, 3xx, 4xx, 5xx) | A rise in 403 or 429 means hosts are rejecting you. A rise in 5xx may mean you are fetching too fast. |
| Timeouts and connection errors | Network trouble or overloaded hosts. |
| Frontier size | A frontier that grows without limit suggests a crawl trap or missing limits. |
| Queue wait time per host | Whether politeness delays are the bottleneck. |
| Duplicate rate | The share of discovered URLs already in the seen set. A rate near zero or near one both deserve a look. |
| Errors per host | Which hosts are failing, so you can slow them down or skip them. |
A small metrics collector
This collector counts outcomes, tracks errors per host, and prints a summary line. You can call record after every fetch and report every few seconds.
import time
from collections import Counter
class CrawlMetrics:
def __init__(self):
self.started = time.monotonic()
self.outcomes = Counter()
self.host_errors = Counter()
def record(self, host, outcome):
# outcome is an HTTP status code, or 'timeout' / 'error' / 'robots'
self.outcomes[outcome] += 1
failed = outcome in ('timeout', 'error') or (
isinstance(outcome, int) and outcome >= 500
)
if failed:
self.host_errors[host] += 1
def report(self, frontier_size):
elapsed = max(time.monotonic() - self.started, 1e-9)
total = sum(self.outcomes.values())
rate = total / elapsed
worst = self.host_errors.most_common(1)
print(f'{total} fetches, {rate:.2f}/s, frontier={frontier_size}, '
f'worst host={worst[0] if worst else None}')
metrics = CrawlMetrics()
metrics.record('example.com', 200)
metrics.record('example.com', 503)
metrics.record('slow.example.org', 'timeout')
metrics.report(frontier_size=42)Where the numbers come from
Every stage of the crawl loop feeds the same collector. The frontier reports its size, and the fetcher reports outcomes and timings.
Turning metrics into alerts
A number only helps if you know what is normal for it. Record a baseline during a healthy run, then alert on changes from it. Useful rules include:
- Throughput falls below half its recent average.
- The 4xx and 5xx share rises above a set fraction, such as 20 percent.
- Frontier size grows for many reports in a row with no matching rise in fetches.
- One host accounts for most errors.
Exercise
A crawl's frontier size has grown steadily for an hour, but pages fetched per second is flat. Which two metrics would you check next, and what do you suspect?
Show answer
Check the duplicate rate and the per-host URL counts. A frontier that grows while throughput stays flat suggests that new URLs are being discovered faster than they are fetched. A crawl trap, such as endless calendar or session URLs on one host, is a likely cause. Per-host counts show which host is producing the URLs, and a depth limit or page budget for that host can then contain it.