diff --git a/opsgenie_sdk/api_client.py b/opsgenie_sdk/api_client.py index 8d2c835..fa112eb 100644 --- a/opsgenie_sdk/api_client.py +++ b/opsgenie_sdk/api_client.py @@ -77,13 +77,11 @@ def __init__(self, configuration=None, header_name=None, header_value=None, self.configuration = configuration self.pool_threads = pool_threads - self.retrying = tenacity.Retrying(stop=tenacity.stop_after_attempt(configuration.retry_count), + self.retrying = tenacity.Retrying(stop=self.should_retry_stop, wait=tenacity.wait_random_exponential(multiplier=configuration.back_off, - max=configuration.retry_max_delay, - min=configuration.retry_delay), - retry=(tenacity.retry_if_result(self.is_retry_enabled) and - ((tenacity.retry_if_exception_type(RetryableException)) | - (tenacity.retry_if_exception_type(HTTPError))))) + max=configuration.retry_delay), + retry=(tenacity.retry_if_exception_type(RetryableException) | + (tenacity.retry_if_exception_type(HTTPError)))) self.rest_client = rest.RESTClientObject(configuration, retrying=self.retrying) self.default_headers = {} @@ -104,8 +102,11 @@ def __del__(self): self._pool.join() self._pool = None - def is_retry_enabled(self): - return self.configuration.retry_enabled + def should_retry_stop(self, retry_state): + if self.configuration.retry_enabled and retry_state.attempt_number <= self.configuration.retry_count and retry_state.seconds_since_start <= self.configuration.retry_max_delay: + return False + + return True @property def pool(self): @@ -192,13 +193,30 @@ def __call_api( url = _host + resource_path # perform request and return response - response_data = self.retrying.call(fn=self.request, method=method, url=url, - query_params=query_params, - headers=header_params, - post_params=post_params, - body=body, - _preload_content=_preload_content, - _request_timeout=_request_timeout) + try: + response_data = self.retrying.call(fn=self.request, method=method, url=url, + query_params=query_params, + headers=header_params, + post_params=post_params, + body=body, + _preload_content=_preload_content, + _request_timeout=_request_timeout) + except Exception as exception: + self._sdk_request_details = { + "query_params": query_params, + "headers": header_params, + "post_params": post_params, + "body": body, + "_preload_content": _preload_content, + "_request_timeout": _request_timeout + } + self.sdk_metric_publisher.build_metric(transaction_id=config.metrics_transaction_id, + duration=datetime.datetime.now() - self._request_start_time, + resource_path=url, error_type=type(exception), + error_message=str(exception), + sdk_request_details=self._sdk_request_details, + sdk_result_details="An Exception Was Thrown!") + raise exception self.last_response = response_data diff --git a/opsgenie_sdk/configuration.py b/opsgenie_sdk/configuration.py index 8792800..a07ccc9 100644 --- a/opsgenie_sdk/configuration.py +++ b/opsgenie_sdk/configuration.py @@ -80,7 +80,7 @@ def __init__(self): # Retry count self.retry_count = 5 # Delay time between attempts - self.retry_delay = 0 + self.retry_delay = 30 # Maximum amount of delay self.retry_max_delay = 60 # Multiplier applied to delay between attempts @@ -116,8 +116,6 @@ def __init__(self): self.proxy_headers = None # Safe chars for path_param self.safe_chars_for_path_param = '' - # Adding retries to override urllib3 default value 3 - self.retries = None self.metrics_transaction_id = None diff --git a/opsgenie_sdk/rest.py b/opsgenie_sdk/rest.py index 574595b..efbc759 100644 --- a/opsgenie_sdk/rest.py +++ b/opsgenie_sdk/rest.py @@ -238,6 +238,8 @@ def request(self, method, url, query_params=None, headers=None, logger.debug("response body: %s", r.data) data = self.decodeResponse(r.data) + http_metrics_request = [('method', method), ('url', url), ('query_params', query_params), ('headers', headers), + ('body', body), ('post_params', post_params)] if "message" not in data: self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id, duration=data["took"], @@ -246,7 +248,7 @@ def request(self, method, url, query_params=None, headers=None, error=False, status=r.status, status_code=r.status, - request=query_params) + request=http_metrics_request) else: self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id, duration=data["took"], @@ -255,7 +257,7 @@ def request(self, method, url, query_params=None, headers=None, error=True, status=r.status, status_code=r.status, - request=query_params) + request=http_metrics_request) should_retry = self.__checkHttpCode__(r.status) if should_retry: diff --git a/templates/api_client.mustache b/templates/api_client.mustache index 56573b6..5b9013d 100755 --- a/templates/api_client.mustache +++ b/templates/api_client.mustache @@ -71,13 +71,11 @@ class ApiClient(object): self.configuration = configuration self.pool_threads = pool_threads - self.retrying = tenacity.Retrying(stop=tenacity.stop_after_attempt(configuration.retry_count), + self.retrying = tenacity.Retrying(stop=self.should_retry_stop, wait=tenacity.wait_random_exponential(multiplier=configuration.back_off, - max=configuration.retry_max_delay, - min=configuration.retry_delay), - retry=(tenacity.retry_if_result(self.is_retry_enabled) and - ((tenacity.retry_if_exception_type(RetryableException)) | - (tenacity.retry_if_exception_type(HTTPError))))) + max=configuration.retry_delay), + retry=(tenacity.retry_if_exception_type(RetryableException) | + (tenacity.retry_if_exception_type(HTTPError)))) self.rest_client = rest.RESTClientObject(configuration, retrying=self.retrying) self.default_headers = {} @@ -98,8 +96,11 @@ class ApiClient(object): self._pool.join() self._pool = None - def is_retry_enabled(self): - return self.configuration.retry_enabled + def should_retry_stop(self, retry_state): + if self.configuration.retry_enabled and retry_state.attempt_number <= self.configuration.retry_count and retry_state.seconds_since_start <= self.configuration.retry_max_delay: + return False + + return True @property def pool(self): @@ -189,13 +190,30 @@ class ApiClient(object): url = _host + resource_path # perform request and return response - response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.retrying.call(fn=self.request, method=method, url=url, - query_params=query_params, - headers=header_params, - post_params=post_params, - body=body, - _preload_content=_preload_content, - _request_timeout=_request_timeout) + try: + response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.retrying.call(fn=self.request, method=method, url=url, + query_params=query_params, + headers=header_params, + post_params=post_params, + body=body, + _preload_content=_preload_content, + _request_timeout=_request_timeout) + except Exception as exception: + self._sdk_request_details = { + "query_params": query_params, + "headers": header_params, + "post_params": post_params, + "body": body, + "_preload_content": _preload_content, + "_request_timeout": _request_timeout + } + self.sdk_metric_publisher.build_metric(transaction_id=config.metrics_transaction_id, + duration=datetime.datetime.now() - self._request_start_time, + resource_path=url, error_type=type(exception), + error_message=str(exception), + sdk_request_details=self._sdk_request_details, + sdk_result_details="An Exception Was Thrown!") + raise exception self.last_response = response_data diff --git a/templates/configuration.mustache b/templates/configuration.mustache index 4b16f8c..c559bb6 100755 --- a/templates/configuration.mustache +++ b/templates/configuration.mustache @@ -81,7 +81,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): # Retry count self.retry_count = 5 # Delay time between attempts - self.retry_delay = 0 + self.retry_delay = 30 # Maximum amount of delay self.retry_max_delay = 60 # Multiplier applied to delay between attempts @@ -117,8 +117,6 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): self.proxy_headers = None # Safe chars for path_param self.safe_chars_for_path_param = '' - # Adding retries to override urllib3 default value 3 - self.retries = None self.metrics_transaction_id = None diff --git a/templates/rest.mustache b/templates/rest.mustache index ed4d282..3b2b805 100755 --- a/templates/rest.mustache +++ b/templates/rest.mustache @@ -229,6 +229,8 @@ class RESTClientObject(object): logger.debug("response body: %s", r.data) data = self.decodeResponse(r.data) + http_metrics_request = [('method', method), ('url', url), ('query_params', query_params), ('headers', headers), + ('body', body), ('post_params', post_params)] if "message" not in data: self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id, duration=data["took"], @@ -237,7 +239,7 @@ class RESTClientObject(object): error=False, status=r.status, status_code=r.status, - request=query_params) + request=http_metrics_request) else: self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id, duration=data["took"], @@ -246,7 +248,7 @@ class RESTClientObject(object): error=True, status=r.status, status_code=r.status, - request=query_params) + request=http_metrics_request) should_retry = self.__checkHttpCode__(r.status) if should_retry: