From 977fc6a8b49866de7093a6b2bb40f84b0ff3a61d Mon Sep 17 00:00:00 2001 From: Keith Walsh Date: Thu, 2 Apr 2020 14:36:45 -0400 Subject: [PATCH] Disable uses_queues There's currently an issue where logs in the gunicorn app server process don't get pushed to CW. This resolves that, in order to make way for formatting RBAC logs as JSON. Here are some related issues which presented the solution: https://github.com/kislyuk/watchtower/issues/54 https://github.com/kislyuk/watchtower/issues/89 --- rbac/rbac/middleware.py | 22 +++++++++++++--------- rbac/rbac/settings.py | 10 ++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/rbac/rbac/middleware.py b/rbac/rbac/middleware.py index 247e30386..8bce36d59 100644 --- a/rbac/rbac/middleware.py +++ b/rbac/rbac/middleware.py @@ -277,7 +277,6 @@ def process_response(self, request, response): # pylint: disable=no-self-use request (object): The request object response (object): The response object """ - context = '' query_string = '' is_admin = False account = None @@ -293,15 +292,20 @@ def process_response(self, request, response): # pylint: disable=no-self-use is_system = f'System: {request.user.system}' req_id = request.user.req_id - logger.info(dict(request_id=req_id, - account=account, - username=username, - is_admin=request.user.admin, - is_system=request.user.system)) + log_object = { + 'method': request.method, + 'path': request.path + query_string, + 'status': response.status_code, + } + if account: - context = f' -- {req_id} {account} {username} {is_admin} {is_system}' - logger.info(f'{request.method} {request.path}{query_string}' # pylint: disable=W1203 - f' {response.status_code}{context}') + log_object['req_id'] = req_id + log_object['account'] = account + log_object['username'] = username + log_object['is_admin'] = request.user.admin + log_object['is_system'] = request.user.system + + logger.info(log_object) return response diff --git a/rbac/rbac/settings.py b/rbac/rbac/settings.py index 72ca57659..259898846 100644 --- a/rbac/rbac/settings.py +++ b/rbac/rbac/settings.py @@ -287,16 +287,10 @@ 'handlers': LOGGING_HANDLERS, 'level': RBAC_LOGGING_LEVEL, }, - 'gunicorn.access': { - 'handlers': ['console'], - 'level': RBAC_LOGGING_LEVEL, - 'propagate': False, - }, }, } if CW_AWS_ACCESS_KEY_ID: - print('setting up CW handler') NAMESPACE = ENVIRONMENT.get_value('APP_NAMESPACE', default='unknown') BOTO3_SESSION = Session(aws_access_key_id=CW_AWS_ACCESS_KEY_ID, aws_secret_access_key=CW_AWS_SECRET_ACCESS_KEY, @@ -306,8 +300,9 @@ 'class': 'watchtower.CloudWatchLogHandler', 'boto3_session': BOTO3_SESSION, 'log_group': CW_LOG_GROUP, - 'stream_name': 'rbac-test', + 'stream_name': NAMESPACE, 'formatter': LOGGING_FORMATTER, + 'use_queues': False, } LOGGING['handlers']['watchtower'] = WATCHTOWER_HANDLER @@ -335,7 +330,6 @@ # Role Seeding Setup ROLE_SEEDING_ENABLED = ENVIRONMENT.bool('ROLE_SEEDING_ENABLED', default=True) -logconfig_dict = LOGGING # disable log messages less than CRITICAL when running unit tests. if len(sys.argv) > 1 and sys.argv[1] == 'test':