-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TDL-26714: Fix ratelimit error and add retry for 5xx errors #155
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8e9fddf
Fix 502 and 429 error
prijendev c311aff
Fix pylint issue
prijendev f9e77c6
Replace batch size with concurrency_limit
prijendev 7dfda05
Fix rate limit issue
prijendev e205234
Remove 404 log
prijendev a1d299e
Increase buffer wait time
prijendev 116497c
Skip deleted tickets
prijendev 56c7afd
Move forward inf condition
prijendev ecf4bf0
Add unit tests for deleted tickets
prijendev 8a366ea
Address review comments
prijendev 4e114c9
Fix pylint
prijendev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||
import json | ||||||||||||||||||||||
import datetime | ||||||||||||||||||||||
import asyncio | ||||||||||||||||||||||
import time | ||||||||||||||||||||||
import pytz | ||||||||||||||||||||||
from zenpy.lib.exception import APIException | ||||||||||||||||||||||
from aiohttp import ClientSession | ||||||||||||||||||||||
|
@@ -18,7 +19,9 @@ | |||||||||||||||||||||
|
||||||||||||||||||||||
DEFAULT_PAGE_SIZE = 100 | ||||||||||||||||||||||
REQUEST_TIMEOUT = 300 | ||||||||||||||||||||||
DEFAULT_BATCH_SIZE = 700 | ||||||||||||||||||||||
CONCURRENCY_LIMIT = 20 | ||||||||||||||||||||||
# Reference: https://developer.zendesk.com/api-reference/introduction/rate-limits/#endpoint-rate-limits:~:text=List%20Audits%20for,requests%20per%20minute | ||||||||||||||||||||||
AUDITS_REQUEST_PER_MINUTE=450 | ||||||||||||||||||||||
START_DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ" | ||||||||||||||||||||||
HEADERS = { | ||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||
|
@@ -266,7 +269,6 @@ class Tickets(CursorBasedExportStream): | |||||||||||||||||||||
replication_key = "generated_timestamp" | ||||||||||||||||||||||
item_key = "tickets" | ||||||||||||||||||||||
endpoint = "https://{}.zendesk.com/api/v2/incremental/tickets/cursor.json" | ||||||||||||||||||||||
batch_size = DEFAULT_BATCH_SIZE | ||||||||||||||||||||||
|
||||||||||||||||||||||
def sync(self, state): #pylint: disable=too-many-statements | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -276,9 +278,6 @@ def sync(self, state): #pylint: disable=too-many-statements | |||||||||||||||||||||
# https://developer.zendesk.com/documentation/ticketing/using-the-zendesk-api/side_loading/#supported-endpoints | ||||||||||||||||||||||
tickets = self.get_objects(bookmark, side_load='metric_sets') | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Run this method to set batch size to fetch ticket audits and comments records in async way | ||||||||||||||||||||||
self.check_access() | ||||||||||||||||||||||
|
||||||||||||||||||||||
audits_stream = TicketAudits(self.client, self.config) | ||||||||||||||||||||||
metrics_stream = TicketMetrics(self.client, self.config) | ||||||||||||||||||||||
comments_stream = TicketComments(self.client, self.config) | ||||||||||||||||||||||
|
@@ -295,6 +294,8 @@ def emit_sub_stream_metrics(sub_stream): | |||||||||||||||||||||
LOGGER.info("Syncing ticket_audits per ticket...") | ||||||||||||||||||||||
|
||||||||||||||||||||||
ticket_ids = [] | ||||||||||||||||||||||
counter = 0 | ||||||||||||||||||||||
start_time = time.time() | ||||||||||||||||||||||
for ticket in tickets: | ||||||||||||||||||||||
zendesk_metrics.capture('ticket') | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -306,6 +307,9 @@ def emit_sub_stream_metrics(sub_stream): | |||||||||||||||||||||
# yielding stream name with record in a tuple as it is used for obtaining only the parent records while sync | ||||||||||||||||||||||
yield (self.stream, ticket) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Skip deleted tickets because they don't have audits or comments | ||||||||||||||||||||||
if ticket.get('status') == 'deleted': | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
|
||||||||||||||||||||||
if metrics_stream.is_selected() and ticket.get('metric_set'): | ||||||||||||||||||||||
zendesk_metrics.capture('ticket_metric') | ||||||||||||||||||||||
|
@@ -314,7 +318,7 @@ def emit_sub_stream_metrics(sub_stream): | |||||||||||||||||||||
|
||||||||||||||||||||||
# Check if the number of ticket IDs has reached the batch size. | ||||||||||||||||||||||
ticket_ids.append(ticket["id"]) | ||||||||||||||||||||||
if len(ticket_ids) >= self.batch_size: | ||||||||||||||||||||||
if len(ticket_ids) >= CONCURRENCY_LIMIT: | ||||||||||||||||||||||
# Process audits and comments in batches | ||||||||||||||||||||||
records = self.sync_ticket_audits_and_comments( | ||||||||||||||||||||||
comments_stream, audits_stream, ticket_ids) | ||||||||||||||||||||||
|
@@ -327,6 +331,15 @@ def emit_sub_stream_metrics(sub_stream): | |||||||||||||||||||||
ticket_ids = [] | ||||||||||||||||||||||
# Write state after processing the batch. | ||||||||||||||||||||||
singer.write_state(state) | ||||||||||||||||||||||
counter+=CONCURRENCY_LIMIT | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
# Check if the number of records processed in a minute has reached the limit. | ||||||||||||||||||||||
if counter >= AUDITS_REQUEST_PER_MINUTE: | ||||||||||||||||||||||
# Processed max number of records in a minute. Sleep for few seconds. | ||||||||||||||||||||||
# Add 2 seconds of buffer time | ||||||||||||||||||||||
time.sleep(max(0, 60 - (time.time() - start_time)+2)) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though original code logic is correct, suggested code may offer better readability, clarity and code maintenance.
Suggested change
|
||||||||||||||||||||||
start_time = time.time() | ||||||||||||||||||||||
counter = 0 | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Check if there are any remaining ticket IDs after the loop. | ||||||||||||||||||||||
if ticket_ids: | ||||||||||||||||||||||
|
@@ -357,11 +370,7 @@ def check_access(self): | |||||||||||||||||||||
start_time = datetime.datetime.strptime(self.config['start_date'], START_DATE_FORMAT).timestamp() | ||||||||||||||||||||||
HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) | ||||||||||||||||||||||
|
||||||||||||||||||||||
response = http.call_api(url, self.request_timeout, params={'start_time': start_time, 'per_page': 1}, headers=HEADERS) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Rate limit are varies according to the zendesk account. So, we need to set the batch size dynamically. | ||||||||||||||||||||||
# https://developer.zendesk.com/api-reference/introduction/rate-limits/ | ||||||||||||||||||||||
self.batch_size = int(response.headers.get('x-rate-limit', DEFAULT_BATCH_SIZE)) | ||||||||||||||||||||||
http.call_api(url, self.request_timeout, params={'start_time': start_time, 'per_page': 1}, headers=HEADERS) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
class TicketAudits(Stream): | ||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For every non-200 error, API does not return a valid JSON response. Showing, logger with stack trace make it very ugly.