Write per-crawl stats: HTTP status counts + sitemap accounting for auditing#9
Open
MirjamOdile wants to merge 1 commit into
Open
Write per-crawl stats: HTTP status counts + sitemap accounting for auditing#9MirjamOdile wants to merge 1 commit into
MirjamOdile wants to merge 1 commit into
Conversation
Every completed crawl now persists its own numbers to data/<project>/_audit/crawl_stats/<spider>.json: - BaseDBSpiderMixin.closed() dumps item count, request count, and the HTTP status histogram Scrapy already tracks — giving an audit EXACT page liveness for free instead of a slow sampled re-fetch pass. Only on reason=="finished", so partial/test (--limit) runs can't overwrite a real crawl's numbers. - Sitemap spiders additionally count, while parsing, the sitemap's page URLs (sitemap_total) and those that survive date/deny filters AND match an allow rule (eligible) — the coverage denominator, recorded from the real crawl so nothing re-fetches the sitemap. <sitemapindex> entries (sub-sitemap refs) are never counted as pages. See docs/requests/06-per-crawl-stats.md (ships with the quality tool PR) for the full write-up.
9c290a8 to
6e07c21
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Assessing a fleet's health needs each crawl's page liveness (HTTP status mix)
and a coverage denominator (how many URLs the sitemap declared, how many were
rule-eligible). Sampling that after the fact is slow (re-fetching ~1000 URLs
per spider) and drifts — the sitemap you fetch today isn't the one the crawl
saw. Scrapy already counts every response status during the crawl; the data
just isn't persisted anywhere.
Change
Every completed production crawl persists its own numbers to
data/<project>/_audit/crawl_stats/<spider>.json:BaseDBSpiderMixin.closed()dumps item count, request count, and the HTTPstatus histogram Scrapy already tracks (filtered on the
downloader/response_status_count/prefix so nothing else leaks in).(
sitemap_total) and those that survive date/deny filters AND match anallow rule (
eligible) — the coverage denominator, recorded from the realcrawl at the crawl's own point in time.
<sitemapindex>entries (sub-sitemaprefs) are never counted as pages.
Guards, so a wrong number can never masquerade as a right one:
CLOSESPIDER_ITEMCOUNTsetting, not the close reason: a
--limit/health run that finishes UNDERits cap still closes with
reason == "finished"and must not overwrite areal crawl's stats.
the request queue from disk but every in-memory counter restarts at zero.
Detection reads the scheduler's own persisted state
(
JOBDIR/requests.queue/active.json— non-empty exactly when a runcontinues an interrupted crawl); the writer then withholds
sitemap_total/eligibleand stamps"resumed": true, keeping leg-onlyitems/status.
Verification
17 unit tests: writer happy-path and negative paths (non-finished reasons,
item-capped runs, resumed runs, write failure), sitemap counter fidelity
(match-all fallback, sitemapindex exclusion, relative-loc resolution), and
resume detection state matrix. Two additional round-trip tests (auto-skipped
in trees without a consumer) prove the file parses back into liveness/coverage
readings. Full unit suite green.
Known limitations (documented, deliberate)
items/status(marked"resumed": true).The full fix — accumulating counters across legs in the checkpoint dir — is
a worthwhile follow-up, but must handle the dupefilter-clearing corruption
recovery (which makes a resumed leg re-parse sitemaps → double-count risk).
only newly fetched URLs.