diff --git a/awx/api/generics.py b/awx/api/generics.py index 90c555c8d3..a71deda2af 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -119,36 +119,7 @@ class APIView(views.APIView): response['X-API-Query-Count'] = len(q_times) response['X-API-Query-Time'] = '%0.3fs' % sum(q_times) - def copy_items_try_to_numberify(d, keys): - new_d = {} - for k in keys: - val = d[k] - if val.endswith('s'): - val = val[:-1] - try: - new_d[k] = int(val) - except ValueError: - try: - new_d[k] = float(val) - except ValueError: - new_d[k] = val - - return new_d - - log = copy_items_try_to_numberify(response, ['X-API-Time', 'X-API-Query-Count', 'X-API-Query-Time', 'X-API-Node',]) - req = { - 'method': request.method, - 'path': request.path, - 'path_info': request.path_info, - 'query_string': request.META['QUERY_STRING'], - - } - if request.method == "POST": - req['post_data'] = request.POST - elif request.method == "GET": - req['get_data'] = request.GET - - analytics_logger.info("api response", extra=dict(request=req, x_api=log)) + analytics_logger.info("api response", extra=dict(python_objects=dict(request=request, response=response))) return response def get_authenticate_header(self, request): diff --git a/awx/main/utils/formatters.py b/awx/main/utils/formatters.py index 4bece1b74f..da5df45726 100644 --- a/awx/main/utils/formatters.py +++ b/awx/main/utils/formatters.py @@ -56,6 +56,21 @@ class LogstashFormatter(LogstashFormatterVersion1): adict[name] = subdict return adict + def convert_to_type(t, val): + if t is float: + val = val[:-1] if val.endswith('s') else val + try: + return float(val) + except ValueError: + return val + elif t is int: + try: + return int(val) + except ValueError: + return val + elif t is str: + return val + if kind == 'job_events': data.update(data.get('event_data', {})) for fd in data: @@ -82,7 +97,25 @@ class LogstashFormatter(LogstashFormatterVersion1): data_for_log['facts'] = data data_for_log['module_name'] = module_name elif kind == 'performance': - return raw_data + request = raw_data['python_objects']['request'] + response = raw_data['python_objects']['response'] + + headers = [ + (float, 'X-API-Time'), # may end with an 's' "0.33s" + (int, 'X-API-Query-Count'), + (float, 'X-API-Query-Time'), # may also end with an 's' + (str, 'X-API-Node'), + ] + data_for_log['x_api'] = { k: convert_to_type(t, response[k]) for (t, k) in headers } + + data_for_log['request'] = { + 'method': request.method, + 'path': request.path, + 'path_info': request.path_info, + 'query_string': request.META['QUERY_STRING'], + 'data': request.data, + } + return data_for_log def get_extra_fields(self, record):