feat(ocr): observe ocr_processing_seconds around engine.to_thread calls
Wraps every asyncio.to_thread(engine.extract_*) call with time.monotonic() deltas in /ocr (per document) and in both /ocr/stream generators (per page). Streaming buckets are the useful operational signal; the non-streaming observation is a bonus. Refs #652 (AC5) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -267,3 +267,43 @@ async def test_ocr_words_and_illegible_words_total_sum_across_blocks(fresh_metri
|
||||
|
||||
assert fresh_metrics.ocr_words_total._value.get() == 5.0
|
||||
assert fresh_metrics.ocr_illegible_words_total._value.get() == 2.0
|
||||
|
||||
|
||||
def _histogram_count_sum(histogram, **labels) -> tuple[float, float]:
|
||||
"""Read the per-label-set _count and _sum from a prometheus_client Histogram."""
|
||||
child = histogram.labels(**labels)
|
||||
return child._sum.get(), sum(b.get() for b in child._buckets)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ocr_processing_seconds_histogram_observed_per_page_in_stream(fresh_metrics):
|
||||
"""The streaming generator observes ocr_processing_seconds once per page."""
|
||||
mock_images = [Image.new("RGB", (100, 100)) for _ in range(2)]
|
||||
mock_blocks = [{"pageNumber": 1, "x": 0.0, "y": 0.0, "width": 1.0, "height": 1.0,
|
||||
"polygon": None, "text": "ok", "words": []}]
|
||||
|
||||
with patch("main.kraken_engine.load_models"), \
|
||||
patch("main.load_spell_checker"), \
|
||||
patch("main._download_and_convert_pdf", new_callable=AsyncMock, return_value=mock_images), \
|
||||
patch("main.preprocess_page", side_effect=lambda img: img), \
|
||||
patch("main.surya_engine.extract_page_blocks", return_value=mock_blocks):
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
|
||||
import main as main_module
|
||||
main_module._models_ready = True
|
||||
try:
|
||||
async with client.stream("POST", "/ocr/stream", json={
|
||||
"pdfUrl": "http://minio/doc.pdf",
|
||||
"scriptType": "TYPEWRITER",
|
||||
"language": "de",
|
||||
}) as response:
|
||||
assert response.status_code == 200
|
||||
async for _ in response.aiter_lines():
|
||||
pass
|
||||
finally:
|
||||
main_module._models_ready = False
|
||||
|
||||
sum_seconds, count = _histogram_count_sum(
|
||||
fresh_metrics.ocr_processing_seconds, engine="surya"
|
||||
)
|
||||
assert count == 2.0
|
||||
assert sum_seconds >= 0.0
|
||||
|
||||
Reference in New Issue
Block a user