Skip to content

Commit

Permalink
get data size in memory for better logs (#7090)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachliu authored Aug 1, 2024
1 parent 660d04b commit 86b75db
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion redash/tasks/queries/execution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import signal
import sys
import time
from collections import deque

import redis
from rq import get_current_job
Expand Down Expand Up @@ -145,6 +147,30 @@ def _resolve_user(user_id, is_api_key, query_id):
return None


def _get_size_iterative(dict_obj):
"""Iteratively finds size of objects in bytes"""
seen = set()
size = 0
objects = deque([dict_obj])

while objects:
current = objects.popleft()
if id(current) in seen:
continue
seen.add(id(current))
size += sys.getsizeof(current)

if isinstance(current, dict):
objects.extend(current.keys())
objects.extend(current.values())
elif hasattr(current, "__dict__"):
objects.append(current.__dict__)
elif hasattr(current, "__iter__") and not isinstance(current, (str, bytes, bytearray)):
objects.extend(current)

return size


class QueryExecutor:
def __init__(self, query, data_source_id, user_id, is_api_key, metadata, is_scheduled_query):
self.job = get_current_job()
Expand Down Expand Up @@ -195,7 +221,7 @@ def run(self):
"job=execute_query query_hash=%s ds_id=%d data_length=%s error=[%s]",
self.query_hash,
self.data_source_id,
data and len(data),
data and _get_size_iterative(data),
error,
)

Expand Down

0 comments on commit 86b75db

Please sign in to comment.