Skip to content

Commit 7b3b7cf

Browse files
committed
Still record circuit breaker hits on 503+
These errors indicate failures from downstream loadbalancers that indicate that the downstream application is gone/timing out.
1 parent 11f39cc commit 7b3b7cf

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/sentry/hybridcloud/apigateway/proxy.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,22 @@ def proxy_cell_request(request: HttpRequest, cell: Cell, url_name: str) -> HttpR
193193
)
194194
except Timeout:
195195
metrics.incr("apigateway.proxy.request_timeout", tags=metric_tags)
196-
try:
197-
if circuit_breaker is not None:
198-
circuit_breaker.record_error()
199-
except Exception:
200-
logger.exception("Failed to record circuitbreaker failure")
196+
if circuit_breaker is not None:
197+
circuit_breaker.record_error()
201198

202199
# remote silo timeout. Use DRF timeout instead
203200
raise RequestTimeout()
204201
except ConnectionError:
205202
metrics.incr("apigateway.proxy.connection_error", tags=metric_tags)
206-
try:
207-
if circuit_breaker is not None:
208-
circuit_breaker.record_error()
209-
except Exception:
210-
logger.exception("Failed to record circuitbreaker failure")
203+
if circuit_breaker is not None:
204+
circuit_breaker.record_error()
211205

212206
raise
213207

214-
if resp.status_code >= 500:
208+
if resp.status_code >= 502:
215209
metrics.incr("apigateway.proxy.request_failed", tags=metric_tags)
210+
if circuit_breaker is not None:
211+
circuit_breaker.record_error()
216212

217213
new_headers = clean_outbound_headers(resp.headers)
218214
resp.headers.clear()

tests/sentry/hybridcloud/apigateway/test_proxy.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_timeout_records_error(self) -> None:
355355
def test_connection_error_records_error(self) -> None:
356356
responses.add(
357357
responses.GET,
358-
f"{self.REGION.address}/connect-error",
358+
f"{self.CELL.address}/connect-error",
359359
body=ConnectionError(),
360360
)
361361
with patch("sentry.hybridcloud.apigateway.proxy.CircuitBreaker") as mock_breaker_class:
@@ -371,7 +371,7 @@ def test_connection_error_records_error(self) -> None:
371371
def test_connection_error_records_metric(self) -> None:
372372
responses.add(
373373
responses.GET,
374-
f"{self.REGION.address}/connect-error",
374+
f"{self.CELL.address}/connect-error",
375375
body=ConnectionError(),
376376
)
377377
with patch("sentry.hybridcloud.apigateway.proxy.metrics") as mock_metrics:
@@ -380,12 +380,30 @@ def test_connection_error_records_metric(self) -> None:
380380
proxy_request(request, self.organization.slug, url_name)
381381
mock_metrics.incr.assert_any_call(
382382
"apigateway.proxy.connection_error",
383-
tags={"region": self.REGION.name, "url_name": url_name},
383+
tags={"region": self.CELL.name, "url_name": url_name},
384384
)
385385

386386
@responses.activate
387387
@override_options(CB_ENABLED)
388-
def test_5xx_response_does_not_record_error(self) -> None:
388+
def test_504_response_does_not_record_error(self) -> None:
389+
responses.add(
390+
responses.GET,
391+
f"{self.CELL.address}/server-error",
392+
status=504,
393+
body=json.dumps({"detail": "gateway timeout"}),
394+
content_type="application/json",
395+
)
396+
with patch("sentry.hybridcloud.apigateway.proxy.CircuitBreaker") as mock_breaker_class:
397+
mock_breaker = self._make_breaker_mock(allow_request=True)
398+
mock_breaker_class.return_value = mock_breaker
399+
request = RequestFactory().get("http://sentry.io/server-error")
400+
resp = proxy_request(request, self.organization.slug, url_name)
401+
assert resp.status_code == 504
402+
mock_breaker.record_error.assert_called_once()
403+
404+
@responses.activate
405+
@override_options(CB_ENABLED)
406+
def test_500_response_does_not_record_error(self) -> None:
389407
responses.add(
390408
responses.GET,
391409
f"{self.CELL.address}/server-error",

0 commit comments

Comments
 (0)