Repository: celery/celery

Issue:
task args and kwargs in LOG_RECEIVED and LOG_SUCCESS should use argsrepr and kwargsrepr
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->
- [x] I have verified that the issue exists against the `main` branch of Celery.
- [ ] This has already been asked to the [discussions forum](https://github.com/celery/celery/discussions) first.
- [ ] I have read the relevant section in the
  [contribution guide](https://docs.celeryq.dev/en/main/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [x] I have checked the [commit log](https://github.com/celery/celery/commits/main)
  to find out if the bug was already fixed in the main branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [ ] I have included the output of ``celery -A proj report`` in the issue.
    (if you are not able to do this, then at least specify the Celery
     version affected).
- [ ] I have verified that the issue exists against the `main` branch of Celery.
- [ ] I have included the contents of ``pip freeze`` in the issue.
- [ ] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [ ] I have tried reproducing the issue on more than one Python version
  and/or implementation.
- [ ] I have tried reproducing the issue on more than one message broker and/or
  result backend.
- [ ] I have tried reproducing the issue on more than one version of the message
  broker and/or result backend.
- [ ] I have tried reproducing the issue on more than one operating system.
- [ ] I have tried reproducing the issue on more than one workers pool.
- [ ] I have tried reproducing the issue with autoscaling, retries,
  ETA/Countdown & rate limits disabled.
- [ ] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- #6898 
- #6885 

#### Possible Duplicates

- None

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**:
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:</b></summary>
<p>

```
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
- **Minimal Python Version**: N/A or Unknown
- **Minimal Celery Version**: N/A or Unknown
- **Minimal Kombu Version**: N/A or Unknown
- **Minimal Broker Version**: N/A or Unknown
- **Minimal Result Backend Version**: N/A or Unknown
- **Minimal OS and/or Kernel Version**: N/A or Unknown
- **Minimal Broker Client Version**: N/A or Unknown
- **Minimal Result Backend Client Version**: N/A or Unknown

### Python Packages
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```python
```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->
```python
                                'args': req.argsrepr,
                                'kwargs': req.kwargsrepr,
```

# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->
```python
                                'args': safe_repr(args),
                                'kwargs': safe_repr(kwargs),
```


Discussion:
Hey @zhu :wave:,
Thank you for opening an issue. We will get back to you as soon as we can.
Also, check out our [Open Collective](https://opencollective.com/celery) and consider backing us - every little helps!

We also offer priority support for our sponsors.
If you require immediate assistance please consider sponsoring us.

can you contribute the fix with appropriate test?

Resolving patch:
diff --git a/celery/app/trace.py b/celery/app/trace.py
--- a/celery/app/trace.py
+++ b/celery/app/trace.py
@@ -250,8 +250,8 @@ def _log_error(self, task, req, einfo):
             safe_repr(eobj),
             safe_str(einfo.traceback),
             einfo.exc_info,
-            safe_repr(req.args),
-            safe_repr(req.kwargs),
+            req.get('argsrepr') or safe_repr(req.args),
+            req.get('kwargsrepr') or safe_repr(req.kwargs),
         )
         policy = get_log_policy(task, einfo, eobj)
 
@@ -559,8 +559,8 @@ def trace_task(
                                 'name': get_task_name(task_request, name),
                                 'return_value': Rstr,
                                 'runtime': T,
-                                'args': safe_repr(args),
-                                'kwargs': safe_repr(kwargs),
+                                'args': task_request.get('argsrepr') or safe_repr(args),
+                                'kwargs': task_request.get('kwargsrepr') or safe_repr(kwargs),
                             })
 
                 # -* POST *-
diff --git a/celery/worker/strategy.py b/celery/worker/strategy.py
--- a/celery/worker/strategy.py
+++ b/celery/worker/strategy.py
@@ -2,7 +2,6 @@
 import logging
 
 from kombu.asynchronous.timer import to_timestamp
-from kombu.utils.encoding import safe_repr
 
 from celery import signals
 from celery.app import trace as _app_trace
@@ -155,8 +154,9 @@ def task_message_handler(message, body, ack, reject, callbacks,
             context = {
                 'id': req.id,
                 'name': req.name,
-                'args': safe_repr(req.args),
-                'kwargs': safe_repr(req.kwargs),
+                'args': req.argsrepr,
+                'kwargs': req.kwargsrepr,
+                'eta': req.eta,
             }
             info(_app_trace.LOG_RECEIVED, context, extra={'data': context})
         if (req.expires or req.id in revoked_tasks) and req.revoked():


Test patch:
diff --git a/celery/contrib/testing/worker.py b/celery/contrib/testing/worker.py
--- a/celery/contrib/testing/worker.py
+++ b/celery/contrib/testing/worker.py
@@ -1,4 +1,5 @@
 """Embedded workers for integration tests."""
+import logging
 import os
 import threading
 from contextlib import contextmanager
@@ -29,11 +30,48 @@
 class TestWorkController(worker.WorkController):
     """Worker that can synchronize on being fully started."""
 
+    logger_queue = None
+
     def __init__(self, *args, **kwargs):
         # type: (*Any, **Any) -> None
         self._on_started = threading.Event()
+
         super().__init__(*args, **kwargs)
 
+        if self.pool_cls.__module__.split('.')[-1] == 'prefork':
+            from billiard import Queue
+            self.logger_queue = Queue()
+            self.pid = os.getpid()
+
+            try:
+                from tblib import pickling_support
+                pickling_support.install()
+            except ImportError:
+                pass
+
+            # collect logs from forked process.
+            # XXX: those logs will appear twice in the live log
+            self.queue_listener = logging.handlers.QueueListener(self.logger_queue, logging.getLogger())
+            self.queue_listener.start()
+
+    class QueueHandler(logging.handlers.QueueHandler):
+        def prepare(self, record):
+            record.from_queue = True
+            # Keep origin record.
+            return record
+
+        def handleError(self, record):
+            if logging.raiseExceptions:
+                raise
+
+    def start(self):
+        if self.logger_queue:
+            handler = self.QueueHandler(self.logger_queue)
+            handler.addFilter(lambda r: r.process != self.pid and not getattr(r, 'from_queue', False))
+            logger = logging.getLogger()
+            logger.addHandler(handler)
+        return super().start()
+
     def on_consumer_ready(self, consumer):
         # type: (celery.worker.consumer.Consumer) -> None
         """Callback called when the Consumer blueprint is fully started."""
diff --git a/t/integration/test_tasks.py b/t/integration/test_tasks.py
--- a/t/integration/test_tasks.py
+++ b/t/integration/test_tasks.py
@@ -1,3 +1,4 @@
+import logging
 import time
 from datetime import datetime, timedelta
 from time import perf_counter, sleep
@@ -465,6 +466,57 @@ def test_properties(self, celery_session_worker):
         assert res.get(timeout=TIMEOUT)["app_id"] == "1234"
 
 
+class test_trace_log_arguments:
+    args = "CUSTOM ARGS"
+    kwargs = "CUSTOM KWARGS"
+
+    def assert_trace_log(self, caplog, result, expected):
+        # wait for logs from worker
+        sleep(.01)
+
+        records = [(r.name, r.levelno, r.msg, r.data["args"], r.data["kwargs"])
+                   for r in caplog.records
+                   if r.name in {'celery.worker.strategy', 'celery.app.trace'}
+                   if r.data["id"] == result.task_id
+                   ]
+        assert records == [(*e, self.args, self.kwargs) for e in expected]
+
+    def call_task_with_reprs(self, task):
+        return task.set(argsrepr=self.args, kwargsrepr=self.kwargs).delay()
+
+    @flaky
+    def test_task_success(self, caplog):
+        result = self.call_task_with_reprs(add.s(2, 2))
+        value = result.get()
+        assert value == 4
+        assert result.successful() is True
+
+        self.assert_trace_log(caplog, result, [
+            ('celery.worker.strategy', logging.INFO,
+             celery.app.trace.LOG_RECEIVED,
+             ),
+            ('celery.app.trace', logging.INFO,
+             celery.app.trace.LOG_SUCCESS,
+             ),
+        ])
+
+    @flaky
+    def test_task_failed(self, caplog):
+        result = self.call_task_with_reprs(fail.s(2, 2))
+        with pytest.raises(ExpectedException):
+            result.get(timeout=5)
+        assert result.failed() is True
+
+        self.assert_trace_log(caplog, result, [
+            ('celery.worker.strategy', logging.INFO,
+             celery.app.trace.LOG_RECEIVED,
+             ),
+            ('celery.app.trace', logging.ERROR,
+             celery.app.trace.LOG_FAILURE,
+             ),
+        ])
+
+
 class test_task_redis_result_backend:
     @pytest.fixture()
     def manager(self, manager):
diff --git a/t/unit/worker/test_strategy.py b/t/unit/worker/test_strategy.py
--- a/t/unit/worker/test_strategy.py
+++ b/t/unit/worker/test_strategy.py
@@ -117,7 +117,7 @@ def get_request(self):
             if self.was_rate_limited():
                 return self.consumer._limit_task.call_args[0][0]
             if self.was_scheduled():
-                return self.consumer.timer.call_at.call_args[0][0]
+                return self.consumer.timer.call_at.call_args[0][2][0]
             raise ValueError('request not handled')
 
     @contextmanager
@@ -176,10 +176,23 @@ def test_log_task_received(self, caplog):
         for record in caplog.records:
             if record.msg == LOG_RECEIVED:
                 assert record.levelno == logging.INFO
+                assert record.args['eta'] is None
                 break
         else:
             raise ValueError("Expected message not in captured log records")
 
+    def test_log_eta_task_received(self, caplog):
+        caplog.set_level(logging.INFO, logger="celery.worker.strategy")
+        with self._context(self.add.s(2, 2).set(countdown=10)) as C:
+            C()
+            req = C.get_request()
+            for record in caplog.records:
+                if record.msg == LOG_RECEIVED:
+                    assert record.args['eta'] == req.eta
+                    break
+            else:
+                raise ValueError("Expected message not in captured log records")
+
     def test_log_task_received_custom(self, caplog):
         caplog.set_level(logging.INFO, logger="celery.worker.strategy")
         custom_fmt = "CUSTOM MESSAGE"
@@ -191,7 +204,23 @@ def test_log_task_received_custom(self, caplog):
             C()
         for record in caplog.records:
             if record.msg == custom_fmt:
-                assert set(record.args) == {"id", "name", "kwargs", "args"}
+                assert set(record.args) == {"id", "name", "kwargs", "args", "eta"}
+                break
+        else:
+            raise ValueError("Expected message not in captured log records")
+
+    def test_log_task_arguments(self, caplog):
+        caplog.set_level(logging.INFO, logger="celery.worker.strategy")
+        args = "CUSTOM ARGS"
+        kwargs = "CUSTOM KWARGS"
+        with self._context(
+            self.add.s(2, 2).set(argsrepr=args, kwargsrepr=kwargs)
+        ) as C:
+            C()
+        for record in caplog.records:
+            if record.msg == LOG_RECEIVED:
+                assert record.args["args"] == args
+                assert record.args["kwargs"] == kwargs
                 break
         else:
             raise ValueError("Expected message not in captured log records")