Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Commit ee55025

Browse files
committed
Merge branch 'fix-request-logging' into 'master'
Correctly get the action from request & defensively pop exception from log dictionary, closes mistio#15 Closes mistio#15 See merge request mistio/mist-api-v2!127
2 parents 4bb2caa + da9a429 commit ee55025

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

mist_api_v2/__main__.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535

3636
app = connexion.App(__name__, specification_dir='./openapi/')
3737
app.app.json_encoder = encoder.JSONEncoder
38-
api_blueprint = app.add_api('openapi.yaml',
39-
arguments={'title': 'Mist API'},
40-
pythonic_params=True)
38+
app.add_api('openapi.yaml',
39+
arguments={'title': 'Mist API'},
40+
pythonic_params=True)
4141
application = app.app
4242

4343
logging.basicConfig(level=config.PY_LOG_LEVEL,
@@ -62,18 +62,17 @@ def prepare_log(response):
6262
g.exc_flag is False):
6363
return response
6464

65+
# Find the action from flask's dict mapping endpoints to view functions.
6566
try:
66-
operation = api_blueprint.specification.get_operation(
67-
request.path, request.method.lower())
68-
operation_id = operation['operationId']
67+
action = application.view_functions[request.endpoint].__name__
6968
except KeyError:
70-
log.error('Could not find operation id. Path:%s Method:%s',
69+
log.error('Could not find action. Path:%s Method:%s',
7170
request.path, request.method)
7271
return response
7372

7473
log_dict = {
7574
'event_type': 'request',
76-
'action': operation_id,
75+
'action': action,
7776
'request_path': request.path,
7877
'request_method': request.method,
7978
'request_ip': request.environ['HTTP_X_REAL_IP'],
@@ -194,10 +193,10 @@ def log_request_to_elastic(exception):
194193
# elastic
195194
log.info('Bad exception occured, logging to rabbitmq')
196195
es_dict = log_dict.copy()
197-
es_dict.pop('_exc_type')
196+
es_dict.pop('_exc_type', '')
198197
es_dict['time'] = time.time()
199198
es_dict['traceback'] = es_dict.pop('_traceback', '')
200-
es_dict['exception'] = es_dict.pop('_exc')
199+
es_dict['exception'] = es_dict.pop('_exc', '')
201200
es_dict['type'] = 'exception'
202201
routing_key = '%s.%s' % (es_dict['owner_id'], es_dict['action'])
203202
pickler = jsonpickle.pickler.Pickler()
@@ -208,8 +207,8 @@ def log_request_to_elastic(exception):
208207
# log bad exception to file
209208
log.info('Bad exception occured, logging to file')
210209
lines = []
211-
lines.append('Exception: %s' % log_dict.pop('_exc'))
212-
lines.append('Exception type: %s' % log_dict.pop('_exc_type'))
210+
lines.append('Exception: %s' % log_dict.pop('_exc', ''))
211+
lines.append('Exception type: %s' % log_dict.pop('_exc_type', ''))
213212
lines.append('Time: %s' % time.strftime('%Y-%m-%d %H:%M %Z'))
214213
lines += (
215214
['%s: %s' % (key, value) for key, value in list(log_dict.items())

0 commit comments

Comments
 (0)