Przeglądaj źródła

Ability to configure maxlen of result/args/kwargs repr. Closes #2540

Ask Solem 9 lat temu
rodzic
commit
2cc25f53b2
3 zmienionych plików z 15 dodań i 3 usunięć
  1. 9 2
      celery/app/amqp.py
  2. 3 0
      celery/app/task.py
  3. 3 1
      celery/app/trace.py

+ 9 - 2
celery/app/amqp.py

@@ -235,6 +235,13 @@ class AMQP(object):
     # and instead send directly to the queue named in the routing key.
     autoexchange = None
 
+    #: Max size of positional argument representation used for
+    #: logging purposes.
+    argsrepr_maxsize = 1024
+
+    #: Max size of keyword argument representation used for logging purposes.
+    kwargsrepr_maxsize = 1024
+
     def __init__(self, app):
         self.app = app
         self.task_protocols = {
@@ -318,8 +325,8 @@ class AMQP(object):
         eta = eta and eta.isoformat()
         expires = expires and expires.isoformat()
 
-        argsrepr = saferepr(args)
-        kwargsrepr = saferepr(kwargs)
+        argsrepr = saferepr(args, self.argsrepr_maxsize)
+        kwargsrepr = saferepr(kwargs, self.kwargsrepr_maxsize)
 
         if JSON_NEEDS_UNICODE_KEYS:  # pragma: no cover
             if callbacks:

+ 3 - 0
celery/app/task.py

@@ -244,6 +244,9 @@ class Task(object):
     #: Default task expiry time.
     expires = None
 
+    #: Max length of result representation used in logs and events.
+    resultrepr_maxsize = 1024
+
     #: Task request stack, the current request will be the topmost.
     request_stack = None
 

+ 3 - 1
celery/app/trace.py

@@ -37,6 +37,7 @@ from celery.exceptions import Ignore, Reject, Retry, InvalidTaskError
 from celery.five import monotonic
 from celery.utils.log import get_logger
 from celery.utils.objects import mro_lookup
+from celery.utils.saferepr import saferepr
 from celery.utils.serialization import (
     get_pickleable_exception, get_pickled_exception, get_pickleable_etype,
 )
@@ -292,6 +293,7 @@ def build_tracer(name, task, loader=None, hostname=None, store_errors=True,
     push_task = _task_stack.push
     pop_task = _task_stack.pop
     _does_info = logger.isEnabledFor(logging.INFO)
+    resultrepr_maxsize = task.resultrepr_maxsize
 
     prerun_receivers = signals.task_prerun.receivers
     postrun_receivers = signals.task_postrun.receivers
@@ -423,7 +425,7 @@ def build_tracer(name, task, loader=None, hostname=None, store_errors=True,
                             send_success(sender=task, result=retval)
                         if _does_info:
                             T = monotonic() - time_start
-                            Rstr = truncate(safe_repr(R), 256)
+                            Rstr = saferepr(R, resultrepr_maxsize)
                             info(LOG_SUCCESS, {
                                 'id': uuid, 'name': name,
                                 'return_value': Rstr, 'runtime': T,