Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Log the right URL and the index in dead link validation messages (#5850)

Shaswat Raje55f89fc5f88
diff --git a/api/api/utils/check_dead_links/__init__.py b/api/api/utils/check_dead_links/__init__.py
index 5db716d50..4c76291d5 100644
--- a/api/api/utils/check_dead_links/__init__.py
+++ b/api/api/utils/check_dead_links/__init__.py
@@ -135,7 +135,7 @@ async def _make_head_requests(
def check_dead_links(query_hash: str, start_slice: int, results: list[Hit]) -> None:
"""
- Make sure images exist before we display them.
+ Make sure media files exist before we display them.
Treat redirects as broken links since most of the time the redirect leads to a
generic "not found" placeholder.
@@ -205,17 +205,22 @@ def check_dead_links(query_hash: str, start_slice: int, results: list[Hit]) -> N
provider = results[del_idx]["provider"]
status_mapping = provider_status_mappings[provider]
+ # The results of every media type are checked here, so name the index
+ # the result came from, to tell which table it is in.
+ index = results[del_idx].meta.index
if status in status_mapping.unknown:
logger.warning(
- "Image validation failed due to rate limiting or blocking. "
- f"url={urls[idx]} "
+ "Link validation failed due to rate limiting or blocking. "
+ f"index={index} "
+ f"url={urls[del_idx]} "
f"status={status} "
f"provider={provider} "
)
elif status not in status_mapping.live:
logger.info(
- "Deleting broken image from results "
+ "Deleting broken result from results "
+ f"index={index} "
f"id={results[del_idx]['identifier']} "
f"status={status} "
f"provider={provider} "
diff --git a/api/test/unit/utils/test_check_dead_links.py b/api/test/unit/utils/test_check_dead_links.py
index 827abe98b..083aeec14 100644
--- a/api/test/unit/utils/test_check_dead_links.py
+++ b/api/test/unit/utils/test_check_dead_links.py
@@ -148,3 +148,50 @@ def test_mset_and_expire_for_responses(is_cache_reachable, cache_name, request):
"Redis connect failed, cannot cache link liveness.",
]
)
+
+
+def test_unknown_status_log_names_the_checked_url():
+ query_hash = "test_unknown_status_log_names_the_checked_url"
+ results = [
+ Hit(create_mock_es_http_image_hit("live", "image", live=True)),
+ Hit(create_mock_es_http_image_hit("blocked", "image", live=True)),
+ ]
+ live_url, blocked_url = (result.url for result in results)
+ pook.head(live_url).reply(200)
+ pook.head(blocked_url).reply(429)
+
+ with capture_logs() as cap_logs:
+ check_dead_links(query_hash, 0, results)
+
+ # A 429 is not a dead link, so both results are kept
+ assert len(results) == 2
+ warnings = [log["event"] for log in cap_logs if log["log_level"] == "warning"]
+ assert len(warnings) == 1
+ assert f"url={blocked_url} " in warnings[0]
+ assert f"url={live_url} " not in warnings[0]
+
+
[email protected]("index", ("image", "audio"))
+def test_dead_link_logs_name_the_index(index):
+ query_hash = f"test_dead_link_logs_name_the_index_{index}"
+ results = [
+ Hit(create_mock_es_http_image_hit("blocked", index, live=True)),
+ Hit(create_mock_es_http_image_hit("dead", index, live=False)),
+ ]
+ blocked, dead = results
+ pook.head(blocked.url).reply(429)
+ pook.head(dead.url).reply(404)
+
+ with capture_logs() as cap_logs:
+ check_dead_links(query_hash, 0, results)
+
+ assert results == [blocked]
+ events = {log["log_level"]: log["event"] for log in cap_logs}
+ # The messages are not specific to images, and name the index of the result
+ # so that it can be found in the matching table.
+ for level in ("warning", "info"):
+ assert "image" not in events[level].split(" index=")[0].lower()
+ assert f"index={index} " in events[level]
+ assert f"id={dead['identifier']} " in events["info"]