Metadata-Version: 2.4
Name: scrapy-spider-monitor
Version: 0.1.1
Summary: Scrapy extension that records per-field fill rates in job stats for Spider Monitor.
License-Expression: MIT
Keywords: scrapy,monitoring,data quality,fill rate
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Scrapy
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: scrapy>=2.11
Requires-Dist: itemadapter>=0.9
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: attrs; extra == "test"
Dynamic: license-file

# scrapy-spider-monitor

A small Scrapy extension that records **per-field fill rates** in your job stats, so
Spider Monitor can flag a spider that "finishes fine" while a field has quietly gone
empty after a site layout change.

It reads only field names, whether values are empty, and response body sizes, and keeps
numeric counters in memory. Item values are never stored, logged or sent anywhere.
The only output is the job's own stats.

## Install

```bash
pip install scrapy-spider-monitor   # on Scrapy Cloud, add scrapy-spider-monitor to requirements.txt
# settings.py:
EXTENSIONS = {"spidermonitor_scrapy.FillRateExtension": 500}
```

Requires Python 3.10+ and Scrapy 2.11+.

## What it writes

When the spider closes, for each top-level field:

| Stat | Meaning |
| --- | --- |
| `fill_rate/<field>/count` | Items where the field was present and non-empty |
| `fill_rate/<field>/rate` | `count / item_scraped_count`, from 0 to 1 (omitted when there are 0 items) |
| `item_scraped_count` | The denominator. Scrapy normally sets it; the extension sets it if it is missing |

For responses that produced at least one item (each counted once, however many items it
yielded; items not tied to a response are skipped):

| Stat | Meaning |
| --- | --- |
| `item_responses/count` | Responses that produced items |
| `item_responses/body_length/min` | Smallest body, in bytes (omitted when the count is 0) |
| `item_responses/body_length/avg` | Mean body, in bytes (omitted when the count is 0) |

Per-status response counts are already in every job's stats as
`downloader/response_status_count/<code>` (Scrapy's built-in `DownloaderStats`), so this
extension does not duplicate them.

`None`, whitespace-only strings (`""`, `" "`, `"\n  "`) and empty containers (`[]`, `{}`,
`()`, `b""`) count as empty. Whitespace counts as empty because a broken selector often
returns it instead of `None`. `0` and `False` count as values. A missing key counts as empty.

Works with dicts, `scrapy.Item`, dataclass and attrs items (via `itemadapter`). Fields
declared on an Item, dataclass or attrs class are reported even if they were never set,
so a field that is empty in every item shows up as `count 0`. For plain dicts, a field is
reported once it has appeared as a key in at least one item.

## Optional settings

```python
# Track only these fields. Listed fields are always reported, even if no item has them.
SPIDERMONITOR_FIELDS = ["title", "price", "sku"]

# Turn the extension off without removing it from EXTENSIONS.
SPIDERMONITOR_FILL_RATE_ENABLED = False
```

## Cost

One dictionary update per field per item, one set lookup per item for the response, and
no I/O during the crawl. Responses are tracked by weak reference, so they are not kept
in memory. Stats are written once, in `spider_closed`. Only top-level fields are counted;
nested fields are not counted in this version.

## Troubleshooting on Scrapy Cloud

**Every job fails at startup with `ImportError: cannot import name 'request_fingerprint'`.**
This comes from the Scrapy Cloud stack, not from this extension: a recent Scrapy (pulled
in by, for example, `scrapy-zyte-api`) together with `scrapinghub-entrypoint-scrapy`
older than 0.18. Add this to `requirements.txt` and deploy again:

```text
scrapinghub-entrypoint-scrapy>=0.18
```

## Development

```bash
python -m venv .venv && . .venv/bin/activate
pip install -e '.[test]'
pytest
```

## License

MIT
