Skip to content

perf: metrics_recorded overcounts non-numeric samples; legacy _store_metric lacks try/finally #1352

Description

@groupthinking

Two pre-existing defects in the performance-metrics path, both surfaced during review of #1341 and deliberately left out of that diff to keep a cleared PR narrow. Neither is a regression from #1341; both predate it.

1. metrics_recorded overcounts

ingest_performance_report_v1 (src/youtube_extension/backend/api/v1/router.py) returns:

return {"status": "ok", "metrics_recorded": len(metrics)}

len(metrics) counts keys submitted, but the ingest loop only appends a sample when the value is numeric:

value = stats.get("current") if isinstance(stats, dict) else None
if isinstance(value, (int, float)):
    samples.append({...})

A report mixing numeric and non-numeric values reports more written than were written. A client sending {"lcp": {"current": 1200}, "note": {"current": "n/a"}} is told 2; one row is persisted.

Suggested fix: return len(samples).

Worth noting this also makes the endpoint's own contract untestable from the response alone — the count cannot distinguish "batched correctly" from "wrote nothing", which is why #1341 added an explicit await_count assertion on the mock instead of relying on this field.

2. Legacy _store_metric lacks try/finally

_store_metric in src/youtube_extension/backend/services/performance_monitor.py opens a SQLite connection without a try/finally, so an exception between open and close leaks the handle. Its batched sibling added in #1341 does this correctly.

Suggested fix: mirror the batched path, or route the singular call through it with a one-element list.

Context

Both were raised in review of #1341 and confirmed as correctly out of scope for it. Filing here so they are tracked rather than lost.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions