prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Capture Celery Time-in-queue
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/scout_apm/celery.py`
  Content:
  ```
  1 # coding=utf-8
  2 from __future__ import absolute_import, division, print_function, unicode_literals
  3 
  4 from celery.signals import task_postrun, task_prerun
  5 
  6 import scout_apm.core
  7 from scout_apm.core.tracked_request import TrackedRequest
  8 
  9 
  10 def prerun_callback(task=None, **kwargs):
  11     tracked_request = TrackedRequest.instance()
  12     tracked_request.mark_real_request()
  13 
  14     delivery_info = task.request.delivery_info
  15     tracked_request.tag("is_eager", delivery_info.get("is_eager", False))
  16     tracked_request.tag("exchange", delivery_info.get("exchange", "unknown"))
  17     tracked_request.tag("routing_key", delivery_info.get("routing_key", "unknown"))
  18     tracked_request.tag("queue", delivery_info.get("queue", "unknown"))
  19 
  20     tracked_request.start_span(operation=("Job/" + task.name))
  21 
  22 
  23 def postrun_callback(task=None, **kwargs):
  24     tracked_request = TrackedRequest.instance()
  25     tracked_request.stop_span()
  26 
  27 
  28 def install():
  29     installed = scout_apm.core.install()
  30     if not installed:
  31         return
  32 
  33     task_prerun.connect(prerun_callback)
  34     task_postrun.connect(postrun_callback)
  35 
  36 
  37 def uninstall():
  38     task_prerun.disconnect(prerun_callback)
  39     task_postrun.disconnect(postrun_callback)
  40
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/scout_apm/celery.py b/src/scout_apm/celery.py
  --- a/src/scout_apm/celery.py
  +++ b/src/scout_apm/celery.py
  @@ -1,16 +1,34 @@
   # coding=utf-8
   from __future__ import absolute_import, division, print_function, unicode_literals
   
  -from celery.signals import task_postrun, task_prerun
  +import datetime as dt
  +
  +from celery.signals import before_task_publish, task_postrun, task_prerun
   
   import scout_apm.core
  +from scout_apm.compat import datetime_to_timestamp
   from scout_apm.core.tracked_request import TrackedRequest
   
   
  +def before_publish_callback(headers=None, properties=None, **kwargs):
  +    if "scout_task_start" not in headers:
  +        headers["scout_task_start"] = datetime_to_timestamp(dt.datetime.utcnow())
  +
  +
   def prerun_callback(task=None, **kwargs):
       tracked_request = TrackedRequest.instance()
       tracked_request.mark_real_request()
   
  +    start = getattr(task.request, "scout_task_start", None)
  +    if start is not None:
  +        now = datetime_to_timestamp(dt.datetime.utcnow())
  +        try:
  +            queue_time = now - start
  +        except TypeError:
  +            pass
  +        else:
  +            tracked_request.tag("queue_time", queue_time)
  +
       delivery_info = task.request.delivery_info
       tracked_request.tag("is_eager", delivery_info.get("is_eager", False))
       tracked_request.tag("exchange", delivery_info.get("exchange", "unknown"))
  @@ -30,10 +48,12 @@
       if not installed:
           return
   
  +    before_task_publish.connect(before_publish_callback)
       task_prerun.connect(prerun_callback)
       task_postrun.connect(postrun_callback)
   
   
   def uninstall():
  +    before_task_publish.disconnect(before_publish_callback)
       task_prerun.disconnect(prerun_callback)
       task_postrun.disconnect(postrun_callback)
  
verification_info: {"golden_diff": "diff --git a/src/scout_apm/celery.py b/src/scout_apm/celery.py\n--- a/src/scout_apm/celery.py\n+++ b/src/scout_apm/celery.py\n@@ -1,16 +1,34 @@\n # coding=utf-8\n from __future__ import absolute_import, division, print_function, unicode_literals\n \n-from celery.signals import task_postrun, task_prerun\n+import datetime as dt\n+\n+from celery.signals import before_task_publish, task_postrun, task_prerun\n \n import scout_apm.core\n+from scout_apm.compat import datetime_to_timestamp\n from scout_apm.core.tracked_request import TrackedRequest\n \n \n+def before_publish_callback(headers=None, properties=None, **kwargs):\n+    if \"scout_task_start\" not in headers:\n+        headers[\"scout_task_start\"] = datetime_to_timestamp(dt.datetime.utcnow())\n+\n+\n def prerun_callback(task=None, **kwargs):\n     tracked_request = TrackedRequest.instance()\n     tracked_request.mark_real_request()\n \n+    start = getattr(task.request, \"scout_task_start\", None)\n+    if start is not None:\n+        now = datetime_to_timestamp(dt.datetime.utcnow())\n+        try:\n+            queue_time = now - start\n+        except TypeError:\n+            pass\n+        else:\n+            tracked_request.tag(\"queue_time\", queue_time)\n+\n     delivery_info = task.request.delivery_info\n     tracked_request.tag(\"is_eager\", delivery_info.get(\"is_eager\", False))\n     tracked_request.tag(\"exchange\", delivery_info.get(\"exchange\", \"unknown\"))\n@@ -30,10 +48,12 @@\n     if not installed:\n         return\n \n+    before_task_publish.connect(before_publish_callback)\n     task_prerun.connect(prerun_callback)\n     task_postrun.connect(postrun_callback)\n \n \n def uninstall():\n+    before_task_publish.disconnect(before_publish_callback)\n     task_prerun.disconnect(prerun_callback)\n     task_postrun.disconnect(postrun_callback)\n", "issue": "Capture Celery Time-in-queue\n\n", "before_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom celery.signals import task_postrun, task_prerun\n\nimport scout_apm.core\nfrom scout_apm.core.tracked_request import TrackedRequest\n\n\ndef prerun_callback(task=None, **kwargs):\n    tracked_request = TrackedRequest.instance()\n    tracked_request.mark_real_request()\n\n    delivery_info = task.request.delivery_info\n    tracked_request.tag(\"is_eager\", delivery_info.get(\"is_eager\", False))\n    tracked_request.tag(\"exchange\", delivery_info.get(\"exchange\", \"unknown\"))\n    tracked_request.tag(\"routing_key\", delivery_info.get(\"routing_key\", \"unknown\"))\n    tracked_request.tag(\"queue\", delivery_info.get(\"queue\", \"unknown\"))\n\n    tracked_request.start_span(operation=(\"Job/\" + task.name))\n\n\ndef postrun_callback(task=None, **kwargs):\n    tracked_request = TrackedRequest.instance()\n    tracked_request.stop_span()\n\n\ndef install():\n    installed = scout_apm.core.install()\n    if not installed:\n        return\n\n    task_prerun.connect(prerun_callback)\n    task_postrun.connect(postrun_callback)\n\n\ndef uninstall():\n    task_prerun.disconnect(prerun_callback)\n    task_postrun.disconnect(postrun_callback)\n", "path": "src/scout_apm/celery.py"}], "after_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport datetime as dt\n\nfrom celery.signals import before_task_publish, task_postrun, task_prerun\n\nimport scout_apm.core\nfrom scout_apm.compat import datetime_to_timestamp\nfrom scout_apm.core.tracked_request import TrackedRequest\n\n\ndef before_publish_callback(headers=None, properties=None, **kwargs):\n    if \"scout_task_start\" not in headers:\n        headers[\"scout_task_start\"] = datetime_to_timestamp(dt.datetime.utcnow())\n\n\ndef prerun_callback(task=None, **kwargs):\n    tracked_request = TrackedRequest.instance()\n    tracked_request.mark_real_request()\n\n    start = getattr(task.request, \"scout_task_start\", None)\n    if start is not None:\n        now = datetime_to_timestamp(dt.datetime.utcnow())\n        try:\n            queue_time = now - start\n        except TypeError:\n            pass\n        else:\n            tracked_request.tag(\"queue_time\", queue_time)\n\n    delivery_info = task.request.delivery_info\n    tracked_request.tag(\"is_eager\", delivery_info.get(\"is_eager\", False))\n    tracked_request.tag(\"exchange\", delivery_info.get(\"exchange\", \"unknown\"))\n    tracked_request.tag(\"routing_key\", delivery_info.get(\"routing_key\", \"unknown\"))\n    tracked_request.tag(\"queue\", delivery_info.get(\"queue\", \"unknown\"))\n\n    tracked_request.start_span(operation=(\"Job/\" + task.name))\n\n\ndef postrun_callback(task=None, **kwargs):\n    tracked_request = TrackedRequest.instance()\n    tracked_request.stop_span()\n\n\ndef install():\n    installed = scout_apm.core.install()\n    if not installed:\n        return\n\n    before_task_publish.connect(before_publish_callback)\n    task_prerun.connect(prerun_callback)\n    task_postrun.connect(postrun_callback)\n\n\ndef uninstall():\n    before_task_publish.disconnect(before_publish_callback)\n    task_prerun.disconnect(prerun_callback)\n    task_postrun.disconnect(postrun_callback)\n", "path": "src/scout_apm/celery.py"}]}