Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions tests/test_sentry_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,122 @@ def test_requests_connection_error_also_sampled(self, mock_random):
"requests.exceptions.ConnectionError should also be sampled",
)

@patch("webapp.app.random.random")
def test_sample_blog_api_retry_error_drops_95_percent(self, mock_random):
"""
Test that RetryError from blog API
(admin.insights.ubuntu.com) is sampled at 5% (95% dropped).
"""
mock_random.return_value = 0.96

mock_error = RetryError()
mock_error.args = (
"HTTPSConnectionPool(host='admin.insights.ubuntu.com', port=443):"
" Max retries exceeded with url: /wp-json/wp/v2/posts?slug=test"
" (Caused by ResponseError('too many 503 error responses'))",
)

hint = {"exc_info": (None, mock_error, None)}
event = {"level": "error"}
result = sentry_before_send(event, hint)

self.assertIsNone(
result, "95% of blog API RetryErrors should be dropped"
)

@patch("webapp.app.random.random")
def test_sample_blog_api_retry_error_keeps_5_percent(self, mock_random):
"""
Test that RetryError from blog API keeps 5% of errors.
"""
mock_random.return_value = 0.04

mock_error = RetryError()
mock_error.args = (
"HTTPSConnectionPool(host='admin.insights.ubuntu.com', port=443):"
" Max retries exceeded with url: /wp-json/wp/v2/posts?slug=test"
" (Caused by ResponseError('too many 503 error responses'))",
)

hint = {"exc_info": (None, mock_error, None)}
event = {"level": "error"}
result = sentry_before_send(event, hint)

self.assertEqual(
result, event, "5% of blog API RetryErrors should be kept"
)

@patch("webapp.app.random.random")
def test_blog_api_connection_error_also_sampled(self, mock_random):
"""
Test that ConnectionError from blog API is also sampled.
"""
mock_random.return_value = 0.96

mock_error = RequestsConnectionError()
mock_error.args = (
"HTTPSConnectionPool(host='admin.insights.ubuntu.com', port=443):"
" Max retries exceeded with url: /wp-json/wp/v2/posts?slug=test"
" (Caused by ResponseError('too many 503 error responses'))",
)

hint = {"exc_info": (None, mock_error, None)}
event = {"level": "error"}
result = sentry_before_send(event, hint)

self.assertIsNone(
result,
"ConnectionError from blog API should also be sampled",
)

@patch("webapp.app.random.random")
def test_blog_api_max_retry_error_also_sampled(self, mock_random):
"""
Test that MaxRetryError from blog API is also sampled.
"""
mock_random.return_value = 0.96

mock_error = MaxRetryError(
pool=None,
url=("/wp-json/wp/v2/posts?slug=test" "&tags_exclude=3184"),
reason=(
"ResponseError('too many 503 error responses')"
" admin.insights.ubuntu.com"
),
)

hint = {"exc_info": (None, mock_error, None)}
event = {"level": "error"}
result = sentry_before_send(event, hint)

self.assertIsNone(
result,
"MaxRetryError from blog API should also be sampled",
)

def test_blog_api_connection_timeout_not_filtered(self):
"""
Test that blog API errors without 500/502/503/504 status codes
(e.g. connection timeouts) are NOT filtered.
"""
mock_error = RequestsConnectionError()
mock_error.args = (
"HTTPSConnectionPool(host='admin.insights.ubuntu.com', port=443):"
" Max retries exceeded with url: /wp-json/wp/v2/posts?slug=test"
" (Caused by ConnectTimeoutError('connection timed out'))",
)

hint = {"exc_info": (None, mock_error, None)}
event = {"level": "error"}
result = sentry_before_send(event, hint)

self.assertEqual(
result,
event,
"Blog API errors without 5xx status codes "
"should not be filtered",
)


if __name__ == "__main__":
unittest.main()
13 changes: 11 additions & 2 deletions webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def sentry_before_send(event, hint):
"""
Filter Sentry events.
Excludes all 4xx errors.
Samples MaxRetryError from security API calls to reduce quota usage.
Samples retry errors from security and
blog/WordPress API calls to reduce quota usage.
"""
if "exc_info" in hint:
_, exc_value, _ = hint["exc_info"]
Expand All @@ -297,7 +298,7 @@ def sentry_before_send(event, hint):
# return None to discard the event
return None

# Sample MaxRetryError from security API calls
# Sample MaxRetryError from security and blog API calls
if isinstance(
exc_value, (MaxRetryError, RetryError, RequestsConnectionError)
):
Expand All @@ -312,6 +313,14 @@ def sentry_before_send(event, hint):
): # Drop 95% of security API retry errors
return None

# Sample blog/WordPress API retry errors
if "/wp-json/wp/v2" in error_msg and any(
f"{code} error" in error_msg
for code in ["500", "502", "503", "504"]
):
if random.random() > 0.05: # Drop 95% of blog API retry errors
return None

return event


Expand Down
12 changes: 10 additions & 2 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from canonicalwebteam.directory_parser import generate_sitemap
from geolite2 import geolite2
from requests import Session
from requests.exceptions import HTTPError
from requests.exceptions import HTTPError, ConnectionError, Timeout
from ubuntu_release_info.data import Data
from werkzeug.exceptions import BadRequest
from canonicalwebteam.flask_base.env import get_flask_env
Expand Down Expand Up @@ -748,7 +748,15 @@ def __init__(self, blog_views):
class BlogRedirects(BlogView):
def dispatch_request(self, slug):
slug = quote(slug, safe="/:?&")
context = self.blog_views.get_article(slug)

try:
context = self.blog_views.get_article(slug)
except (
ConnectionError,
Timeout,
HTTPError,
):
return flask.make_response(flask.render_template("500.html"), 502)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code indicates it's a server downtimeand not a server code error


if "article" not in context:
return flask.abort(404)
Expand Down
Loading