Skip to content

Commit eb1314d

Browse files
Pigbibicodex
andcommitted
fix: harden cloud run runtime guard
Co-Authored-By: Codex <noreply@openai.com>
1 parent e5d8cdc commit eb1314d

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

scripts/cloud_run_runtime_guard.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ def _load_services() -> list[str]:
8787
return unique
8888

8989

90+
def _cloud_run_log_filter(service: str, since_text: str, region: str = "") -> str:
91+
parts = [
92+
'resource.type="cloud_run_revision"',
93+
f'resource.labels.service_name="{service}"',
94+
f'timestamp >= "{since_text}"',
95+
]
96+
if region:
97+
parts.append(f'resource.labels.location="{region}"')
98+
return " AND ".join(parts)
99+
100+
90101
def _service_job_aliases(service: str) -> list[str]:
91102
service_name = str(service or "").strip()
92103
if not service_name:
@@ -345,7 +356,26 @@ def _entry_text(entry: dict[str, Any]) -> str:
345356
return " ".join(chunks)
346357

347358

359+
def _request_path(entry: dict[str, Any]) -> str:
360+
request_url = str((entry.get("httpRequest") or {}).get("requestUrl") or "").strip()
361+
if not request_url:
362+
return ""
363+
return urllib.parse.urlparse(request_url).path
364+
365+
366+
def _is_ignorable_monitor_dispatch_capacity_warning(entry: dict[str, Any]) -> bool:
367+
if not _env_bool("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", True):
368+
return False
369+
return (
370+
_status(entry) == 429
371+
and _request_path(entry) == "/monitor-dispatch"
372+
and "NO AVAILABLE INSTANCE" in _entry_text(entry).upper()
373+
)
374+
375+
348376
def _is_failure(entry: dict[str, Any]) -> bool:
377+
if _is_ignorable_monitor_dispatch_capacity_warning(entry):
378+
return False
349379
severity = str(entry.get("severity") or "").upper()
350380
status = _status(entry)
351381
text = _entry_text(entry).upper()
@@ -491,11 +521,7 @@ def main() -> int:
491521
service_since = _cloud_run_log_since(project, service, since) if ignore_pre_ready_logs else since
492522
service_since_by_name[service] = service_since
493523
service_since_text = _format_timestamp(service_since)
494-
log_filter = (
495-
'resource.type="cloud_run_revision" '
496-
f'AND resource.labels.service_name="{service}" '
497-
f'AND timestamp >= "{service_since_text}"'
498-
)
524+
log_filter = _cloud_run_log_filter(service, service_since_text, _region_for_service(service))
499525
try:
500526
entries = _run_gcloud_logging(project, log_filter, limit)
501527
except RuntimeError as exc:

tests/test_cloud_run_runtime_guard.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
from scripts import cloud_run_runtime_guard as guard
99

1010

11+
def test_cloud_run_log_filter_includes_region_when_available():
12+
log_filter = guard._cloud_run_log_filter(
13+
"firstrade-service",
14+
"2026-07-01T12:00:00Z",
15+
"asia-east1",
16+
)
17+
18+
assert 'resource.labels.service_name="firstrade-service"' in log_filter
19+
assert 'resource.labels.location="asia-east1"' in log_filter
20+
assert 'timestamp >= "2026-07-01T12:00:00Z"' in log_filter
21+
22+
1123
def test_scheduler_job_pattern_includes_service_alias():
1224
pattern = guard._scheduler_job_pattern_for_services(["firstrade-service"])
1325

@@ -125,3 +137,45 @@ def test_scheduler_entry_since_uses_matching_service_revision_window():
125137
guard._scheduler_entry_since(entry, {"other-service": service_since}, fallback)
126138
== fallback
127139
)
140+
141+
142+
def test_monitor_dispatch_capacity_warning_is_not_failure_by_default(monkeypatch):
143+
monkeypatch.delenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", raising=False)
144+
entry = {
145+
"severity": "WARNING",
146+
"httpRequest": {
147+
"status": 429,
148+
"requestUrl": "https://example.run.app/monitor-dispatch",
149+
},
150+
"textPayload": "The request was aborted because there was no available instance.",
151+
}
152+
153+
assert guard._is_failure(entry) is False
154+
155+
156+
def test_monitor_dispatch_capacity_warning_can_be_counted(monkeypatch):
157+
monkeypatch.setenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", "false")
158+
entry = {
159+
"severity": "WARNING",
160+
"httpRequest": {
161+
"status": 429,
162+
"requestUrl": "https://example.run.app/monitor-dispatch",
163+
},
164+
"textPayload": "The request was aborted because there was no available instance.",
165+
}
166+
167+
assert guard._is_failure(entry) is True
168+
169+
170+
def test_strategy_request_capacity_warning_still_fails(monkeypatch):
171+
monkeypatch.delenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", raising=False)
172+
entry = {
173+
"severity": "WARNING",
174+
"httpRequest": {
175+
"status": 429,
176+
"requestUrl": "https://example.run.app/dry-run",
177+
},
178+
"textPayload": "The request was aborted because there was no available instance.",
179+
}
180+
181+
assert guard._is_failure(entry) is True

0 commit comments

Comments
 (0)