Files
karaoke-gen/backend/services/error_monitor/known_issues.py
T
Andrew Beveridge ☄️andClaude Opus 4.8 d6361ca7ad fix(error-monitor): stop transient-capacity + bot crashes from paging (#897)
## Why

Two alert-channel noise sources surfaced during the flacfetch download
incident. Neither was a real production problem, but both page — so we
fix the **monitor's signal quality** rather than mute them.

### 1. Transient encoding-worker capacity retries
`[job:…] Encoding worker start failed, aborting retries: VM … 503 —
SERVICE UNAVAILABLE` is a transient GCP zonal-capacity event.
`render_video_worker` catches `EncodingWorkerStartError` and **parks the
job for auto-retry** (`_park_job_for_capacity_retry`) — the job is *not*
lost, and a genuine give-up fails the job via a separate ERROR. Added a
`known_issues` suppression **scoped to the transient signal only** (503
/ `SERVICE UNAVAILABLE` or a capacity code:
`ZONE_RESOURCE_POOL_EXHAUSTED` / `STOCKOUT` / `QUOTA_EXCEEDED`). A
non-transient `EncodingWorkerStartError` (other/unknown code) shares the
prefix but **still pages**.

### 2. One incident → N alerts (normalizer gap)
The same failure produced *two* alerts (same job, two preview-render
IDs). `preview_<jobid>_<renderid>` is underscore-joined, and the generic
hex normalizer is `\b`-anchored (`_` is a word char), so every preview
render hashed to a distinct "new pattern." Added a normalizer rule that
collapses `preview_<hex>_<hex>` → `preview_<ID>`.

### 3. Bot/crawler browser crashes
`NotFoundError: removeChild` on `/en/r/` came from **bingbot** running
headless Chrome — a React DOM-reconciliation error no real user hits (0
real-user occurrences in 7d). Reports from bot/crawler user-agents are
now dropped at the `/client-errors` ingestion edge (still `202` so the
client won't retry), instead of being persisted/alerted.

## Tests
- `known_issues`: transient 503 **and** capacity-code variants
suppressed; non-transient start failure + genuine job failure still
alert.
- `normalizer`: preview-ID collapse; two renders of one job → same hash.
- `is_bot_user_agent`: crawler matrix true, real-browser/empty/None
false.
- API: a bingbot report is accepted (`202`, `is_new=false`) but **not
upserted**.

`133 passed`. Local CodeRabbit review completed and its two findings
(scope the capacity regex; sanitize the logged bot URL) were addressed;
a confirming re-review was blocked by the free-tier rate limit, so
leaving the bot review on.

## Notes
The actual outage in this incident (daily E2E download failure) was a
**flacfetch** bug, fixed + deployed separately (flacfetch #36). This PR
only removes the alert noise those investigations surfaced.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
- Bot and crawler error reports are acknowledged without being persisted
or triggering alerts.
- Transient encoding-worker capacity failures are filtered as retryable
infrastructure noise.
- Preview render job identifiers are normalized consistently for
improved error grouping.

- **Maintenance**
  - Updated the application version to 0.192.2.
- Expanded validation for bot detection, error filtering, and preview
error normalization.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-07 10:20:47 +02:00

134 lines
5.1 KiB
Python

"""Known infrastructure noise patterns for the production error monitoring service.
Filters out transient, expected log messages so the error monitor is not flooded
with Cloud Run cold-start probes, deploy events, spot VM preemptions, and similar
operational noise.
Usage:
from backend.services.error_monitor.known_issues import should_ignore
reason = should_ignore(service, message)
if reason:
# skip this log entry — it's known infrastructure noise
...
"""
import re
from dataclasses import dataclass
@dataclass
class IgnoreReason:
"""Describes why a log entry should be suppressed.
Attributes:
pattern_name: Machine-readable name of the matching ignore pattern.
reason: Human-readable explanation of why this is infrastructure noise.
"""
pattern_name: str
reason: str
# ---------------------------------------------------------------------------
# Compiled ignore patterns (applied in order)
# All patterns use re.IGNORECASE for case-insensitive matching.
# ---------------------------------------------------------------------------
_IGNORE_PATTERNS: list[tuple[str, re.Pattern, str]] = [
(
"startup_probe",
re.compile(r"startup probe failed", re.IGNORECASE),
"Cloud Run cold start — startup probe failure is transient and expected",
),
(
"ready_condition",
re.compile(r"ready condition status changed", re.IGNORECASE),
"Cloud Run deploy event — readiness condition change is expected on deploy",
),
(
"spot_preemption",
re.compile(r"instance was preempted", re.IGNORECASE),
"Spot/preemptible VM preemption — expected behaviour for spot instances",
),
(
"idle_shutdown",
re.compile(r"stopping idle encoding worker", re.IGNORECASE),
"Encoding worker idle shutdown — expected behaviour when no jobs are queued",
),
(
"encoding_worker_capacity_retry",
# Only the TRANSIENT start failures: GCE 503/SERVICE UNAVAILABLE or a
# capacity code (ZONE_RESOURCE_POOL_EXHAUSTED / STOCKOUT / QUOTA_EXCEEDED
# — see encoding_errors.CAPACITY_ERROR_CODES). The render worker parks
# these for auto-retry. A non-transient EncodingWorkerStartError (other /
# unknown code) shares the "aborting retries" prefix but must still page,
# so we require the transient signal, not just the prefix.
# Bind the transient signal to the GCE error CODE field — which appears
# immediately after "start failed in <zone>: " / "could not be started in
# <zone>: " (see encoding_errors.classify_gce_error). Matching the code in
# position (not anywhere in the line) prevents a non-transient failure
# whose message text merely contains "503" from being suppressed.
re.compile(
r"encoding worker start failed, aborting retries:.*?"
r"(?:start failed|could not be started) in [\w.-]+:\s*"
r"(?:503\b|service unavailable|zone_resource_pool_exhausted"
r"|stockout|quota_exceeded)",
re.IGNORECASE,
),
(
"GCE encoding worker VM start hit a transient GCP condition (zone "
"capacity exhaustion or 503 SERVICE UNAVAILABLE) — the render worker "
"parks the job for auto-retry rather than failing it. Non-transient "
"start failures still alert."
),
),
(
"health_check_404",
re.compile(
r"(?:GET|HEAD)\b[^\n]*?/health[^\n]*?\b(?:404|not found)\b",
re.IGNORECASE,
),
"Load balancer health check returning 404 — transient during cold start",
),
(
"scheduler_retry",
re.compile(r"cloud scheduler.*retry", re.IGNORECASE),
"Cloud Scheduler transient retry — expected on intermittent job failures",
),
(
"runner_startup",
re.compile(r"github\.runner\..*(?:starting|stopping|idle)", re.IGNORECASE),
"GitHub Actions runner lifecycle event — expected during runner start/stop",
),
(
"container_shutdown",
re.compile(r"container called exit\(0\)", re.IGNORECASE),
"Container clean exit — exit code 0 indicates a graceful, expected shutdown",
),
]
def should_ignore(service: str, message: str) -> IgnoreReason | None:
"""Check whether a log entry matches a known infrastructure noise pattern.
The check is purely message-based; ``service`` is accepted for API symmetry
and future service-specific overrides but does not gate any pattern today.
Args:
service: The originating service name (e.g. ``"karaoke-backend"``).
message: The raw log message string to evaluate.
Returns:
An :class:`IgnoreReason` if the message matches a known noise pattern,
or ``None`` if the message should be processed normally.
"""
if not message:
return None
for pattern_name, pattern, reason in _IGNORE_PATTERNS:
if pattern.search(message):
return IgnoreReason(pattern_name=pattern_name, reason=reason)
return None