Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
121 changes: 121 additions & 0 deletions tests/test_sentry_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,126 @@ def test_requests_connection_error_also_sampled(self, mock_random):
)


@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()
12 changes: 11 additions & 1 deletion webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
# 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 +312,16 @@
): # Drop 95% of security API retry errors
return None

# Sample blog/WordPress API retry errors
if "admin.insights.ubuntu.com" 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
14 changes: 12 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,17 @@ 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
)

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