Skip to content

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

Make thumbnail alerts after threshold binned by configurable amount (#2146)

Madison Swain-Bowdenfb0d1908f8c6
diff --git a/api/api/utils/photon.py b/api/api/utils/photon.py
index 3b135e6da..79416eac1 100644
--- a/api/api/utils/photon.py
+++ b/api/api/utils/photon.py
@@ -164,7 +164,10 @@ def get(
errors = cache.incr(key)
except ValueError: # Key does not exist.
cache.set(key, 1)
- if errors >= settings.THUMBNAIL_HTTP_ERROR_THRESHOLD_TO_NOTIFY:
+ if errors >= settings.THUMBNAIL_HTTP_ERROR_THRESHOLD_TO_NOTIFY and (
+ errors % settings.THUMBNAIL_HTTP_ERROR_THRESHOLD_FOR_REPEATED_NOTIFICATIONS
+ == 0
+ ):
sentry_sdk.capture_exception(exc)
raise UpstreamThumbnailException(f"Failed to render thumbnail. {exc}")
except requests.RequestException as exc:
diff --git a/api/conf/settings/thumbnails.py b/api/conf/settings/thumbnails.py
index 15a4e5948..e4818033d 100644
--- a/api/conf/settings/thumbnails.py
+++ b/api/conf/settings/thumbnails.py
@@ -27,3 +27,7 @@ THUMBNAIL_HTTP_ERROR_PREFIX = config(
THUMBNAIL_HTTP_ERROR_THRESHOLD_TO_NOTIFY = config(
"THUMBNAIL_HTTP_ERROR_THRESHOLD_TO_NOTIFY", default=1000, cast=int
)
+
+THUMBNAIL_HTTP_ERROR_THRESHOLD_FOR_REPEATED_NOTIFICATIONS = config(
+ "THUMBNAIL_HTTP_ERROR_THRESHOLD_FOR_REPEATED_NOTIFICATIONS", default=1000, cast=int
+)
diff --git a/api/test/unit/utils/test_photon.py b/api/test/unit/utils/test_photon.py
index 531175a32..e0a572a3f 100644
--- a/api/test/unit/utils/test_photon.py
+++ b/api/test/unit/utils/test_photon.py
@@ -255,14 +255,16 @@ def test_get_http_error_pass_threshold(
key = f"{settings.THUMBNAIL_HTTP_ERROR_PREFIX}subdomain.example.com"
redis.set(key, settings.THUMBNAIL_HTTP_ERROR_THRESHOLD_TO_NOTIFY - 1)
- with pytest.raises(
- UpstreamThumbnailException, match=r"Failed to render thumbnail."
- ):
- photon_get(TEST_IMAGE_URL)
+ # Call several times
+ for _ in range(3):
+ with pytest.raises(
+ UpstreamThumbnailException, match=r"Failed to render thumbnail."
+ ):
+ photon_get(TEST_IMAGE_URL)
capture_exception.assert_called_once_with(exc)
- assert redis.get(key) == b"1000"
+ assert redis.get(key) == b"1002"
@pytest.mark.parametrize(