-
Notifications
You must be signed in to change notification settings - Fork 281
Remove retry circular dependency #1969
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
Changes from 5 commits
401205c
5b8fa9f
ed4ecc3
2a6c921
153fbe9
30245e3
05fee4a
ebaa911
584e096
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
# limitations under the License. | ||
"""Set up for logging.""" | ||
from enum import Enum | ||
|
||
import logging | ||
import os | ||
import sys | ||
import time | ||
import traceback | ||
|
||
import google.cloud.logging | ||
|
@@ -25,8 +27,8 @@ | |
# Disable this check since we have a bunch of non-constant globals in this file. | ||
# pylint: disable=invalid-name | ||
|
||
from common import retry | ||
from common import utils | ||
from common import retry | ||
|
||
_default_logger = None | ||
_log_client = None | ||
|
@@ -37,6 +39,7 @@ | |
|
||
NUM_RETRIES = 5 | ||
RETRY_DELAY = 1 | ||
BACKOFF = 2 | ||
|
||
|
||
def _initialize_cloud_clients(): | ||
|
@@ -153,47 +156,61 @@ class LogSeverity(Enum): | |
DEBUG = logging.DEBUG | ||
|
||
|
||
@retry.wrap(NUM_RETRIES, RETRY_DELAY, 'common.logs.log', log_retries=False) | ||
def log(logger, severity, message, *args, extras=None): | ||
"""Log a message with severity |severity|. If using stack driver logging | ||
then |extras| is also logged (in addition to default extras).""" | ||
message = str(message) | ||
if args: | ||
message = message % args | ||
|
||
if utils.is_local(): | ||
if extras: | ||
message += ' Extras: ' + str(extras) | ||
logging.log(severity, message) | ||
return | ||
|
||
if logger is None: | ||
logger = _default_logger | ||
assert logger | ||
|
||
struct_message = { | ||
'message': message, | ||
} | ||
all_extras = _default_extras.copy() | ||
extras = extras or {} | ||
all_extras.update(extras) | ||
struct_message.update(all_extras) | ||
severity = LogSeverity(severity).name | ||
logger.log_struct(struct_message, severity=severity) | ||
# Custom retry logic to avoid circular dependency | ||
# as retry from retry.py uses log | ||
for num_try in range(1, NUM_RETRIES + 1): | ||
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. Can you do this from 0? Or change NUM_RETRIES to NUM_ATTEMPTS? 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. Nice, I decided to change the nomenclature to NUM_ATTEMPTS, as the get_retry function already was 1-index based |
||
try: | ||
message = str(message) | ||
if args: | ||
message = message % args | ||
|
||
if utils.is_local(): | ||
if extras: | ||
message += ' Extras: ' + str(extras) | ||
logging.log(severity, message) | ||
return | ||
|
||
if logger is None: | ||
logger = _default_logger | ||
assert logger | ||
|
||
struct_message = { | ||
'message': message, | ||
} | ||
all_extras = _default_extras.copy() | ||
extras = extras or {} | ||
all_extras.update(extras) | ||
struct_message.update(all_extras) | ||
severity = LogSeverity(severity).name | ||
logger.log_struct(struct_message, severity=severity) | ||
break | ||
except Exception: # pylint: disable=broad-except | ||
# We really dont want do to do anything here except sleep here, | ||
# since we cant log it out as log itself is already failing | ||
time.sleep(retry.get_delay(num_try, RETRY_DELAY, BACKOFF)) | ||
|
||
|
||
def error(message, *args, extras=None, logger=None): | ||
"""Logs |message| to stackdriver logging and error reporting (including | ||
exception if there was one.""" | ||
|
||
@retry.wrap(NUM_RETRIES, | ||
RETRY_DELAY, | ||
'common.logs.error._report_error_with_retries', | ||
log_retries=False) | ||
def _report_error_with_retries(message): | ||
if utils.is_local(): | ||
return | ||
_error_reporting_client.report(message) | ||
|
||
# Custom retry logic to avoid circular dependency | ||
# as retry from retry.py uses log | ||
for num_try in range(1, NUM_RETRIES + 1): | ||
try: | ||
_error_reporting_client.report(message) | ||
break | ||
except Exception: # pylint: disable=broad-except | ||
# We really dont want do to do anything here except sleep here, | ||
# since we cant log it out as log itself is already failing | ||
time.sleep(retry.get_delay(num_try, RETRY_DELAY, BACKOFF)) | ||
|
||
if not any(sys.exc_info()): | ||
_report_error_with_retries(message % args) | ||
|
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.
Annoying google nit, can you move this line up so the imports are alphabetical order