Skip to content

Commit

Permalink
[Sentry] Added catch to ratelimiting header missing error (#818)
Browse files Browse the repository at this point in the history
# Description

What - Added catching of missing headers in requests to the Sentry API
Why - Some requests seem not to have the header requested to make
ratelimiting optimizations
How - Added try and catch in the request method

## Type of change

Please leave one option from the following and delete the rest:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
  • Loading branch information
matan84 authored Jul 15, 2024
1 parent 3c2a4c7 commit 71670fe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
8 changes: 8 additions & 0 deletions integrations/sentry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

# Port_Ocean 0.1.58 (2024-07-10)

### Bugfix

- Added try and catch to missing ratelimiting headers in response to the Sentry API (#1)
- Added proper logging of missng ratelimiting headers (#1)
- Added raise for http status errors from the request (#1)

# Port_Ocean 0.1.57 (2024-07-10)

### Improvements
Expand Down
41 changes: 27 additions & 14 deletions integrations/sentry/clients/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,35 @@ async def _fetch_with_rate_limit_handling(
while True:
async with semaphore:
response = await self.client.get(url, params=params)
rate_limit_remaining = int(
response.headers["X-Sentry-Rate-Limit-Remaining"]
)
if rate_limit_remaining <= MINIMUM_ISSUES_LIMIT_REMAINING:
rate_limit_reset = int(response.headers["X-Sentry-Rate-Limit-Reset"])
current_time = int(time.time())
wait_time = (
rate_limit_reset - current_time
if rate_limit_reset > current_time
else DEFAULT_SLEEP_TIME
try:
response.raise_for_status()
rate_limit_remaining = int(
response.headers["X-Sentry-Rate-Limit-Remaining"]
)
logger.debug(
f"Approaching rate limit. Waiting for {wait_time} seconds before retrying. "
f"URL: {url}, Remaining: {rate_limit_remaining} "
if rate_limit_remaining <= MINIMUM_ISSUES_LIMIT_REMAINING:
current_time = int(time.time())
rate_limit_reset = int(
response.headers["X-Sentry-Rate-Limit-Reset"]
)
wait_time = (
rate_limit_reset - current_time
if rate_limit_reset > current_time
else DEFAULT_SLEEP_TIME
)
logger.debug(
f"Approaching rate limit. Waiting for {wait_time} seconds before retrying. "
f"URL: {url}, Remaining: {rate_limit_remaining} "
)
await asyncio.sleep(wait_time)
except KeyError as e:
logger.warning(
f"Rate limit headers not found in response: {str(e)} for url {url}"
)
await asyncio.sleep(wait_time)
except httpx.HTTPStatusError as e:
logger.error(
f"Got HTTP error to url: {url} with status code: {e.response.status_code} and response text: {e.response.text}"
)
raise
return response

def get_next_link(self, link_header: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion integrations/sentry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sentry"
version = "0.1.57"
version = "0.1.58"
description = "Sentry Integration"
authors = ["Dvir Segev <dvir@getport.io>","Matan Geva <matang@getport.io>"]

Expand Down

0 comments on commit 71670fe

Please sign in to comment.