You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, when a Celery job runs in the application, it does not generate AppMap recordings. This behavior results in a lack of visibility into the execution and performance of tasks managed by Celery, which impedes the ability to trace issues or optimize task handling.
Analysis
To enable AppMap recording for Celery jobs, we need to integrate the AppMap recorder into the task execution lifecycle of Celery. Celery tasks go through a specific sequence of states: pending, started, and success (or failure). By hooking into these states, we can start a recording before the task executes and stop the recording once the task completes, successfully or otherwise.
We will extend the Celery task class to include AppMap recording functionality. This involves importing the AppMap recorder, starting the recording upon task initiation, and stopping it upon task completion or failure. The recorded AppMap data should be saved in a specified directory, similarly to how request recordings are handled.
Proposed Changes
appmap/labeling/jobs.yml: Ensure Celery task classes are included in the job creation configuration:
Add an entry for celery.app.task.Task.apply_async under job.create.
_appmap/recorder.py: Create helper functions if necessary to standardize the start and stop of recording processes. Confirm that the AppMap recorder follows the lifecycle of the task correctly.
Ensure methods like start_recording() and stop_recording() are suitable for re-use in the context of celery tasks.
Create a new file or update an existing integration module (e.g., _appmap/integration/celery.py): Augment celery.Task to include AppMap recording steps:
Patch the celery.Task class such that the task lifecycle includes AppMap recording steps.
_appmap/configuration.py: Ensure that the configuration for celery is correctly captured and can be enabled via the AppMap configuration.
_appmap/test/test_celery.py: Add tests to validate that Celery tasks are correctly generating AppMap recordings:
Create and run mock Celery tasks, then verify the existence and correctness of the generated AppMaps.
Detailed File Changes
appmap/labeling/jobs.yml:
Add the Celery task class to the job creation configuration.
job.create:
- celery.app.task.Task.apply_async
_appmap/recorder.py:
Make sure existing methods support celery tasks recording; if not, adjust the existing methods to support additional context if necessary.
Create or update an integration file _appmap/integration/celery.py:
_appmap/configuration.py:
Ensure the configuration loader recognizes celery-related configurations:
class_ConfigLoader(SafeLoader):
# existing methods ...defconstruct_mapping(self, node, deep=False):
mapping=super().construct_mapping(node, deep=deep)
# Add or update relevant config parameters related to celeryif"enable_celery"inmapping:
val=mapping["enable_celery"]
ifisinstance(val, str):
mapping["enable_celery"] =val.lower() =="true"returnmapping
_appmap/test/test_celery.py:
Add tests ensuring that AppMaps are generated when Celery tasks execute.
importceleryimportpytestfrom_appmap.recorderimportRecorder@pytest.fixturedefcelery_app():
app=celery.Celery('tasks', broker='memory://')
returnappdeftest_celery_task_appmap_generation(celery_app):
@celery_app.task(bind=True, base=AppMapTask)defsample_task(self):
return"Task completed"result=sample_task.apply_async()
assertresult.get() =="Task completed"# Add assertions to check the appmap file is generated# This might involve checking the tmp/appmap directory
By implementing these changes, Celery tasks will each generate their own AppMap recording, capturing execution details and improving observability for Celery-managed tasks.
Activity
github-actions commentedon Aug 8, 2024
Title
Enable AppMap Recording for Celery Jobs
Problem
Currently, when a Celery job runs in the application, it does not generate AppMap recordings. This behavior results in a lack of visibility into the execution and performance of tasks managed by Celery, which impedes the ability to trace issues or optimize task handling.
Analysis
To enable AppMap recording for Celery jobs, we need to integrate the AppMap recorder into the task execution lifecycle of Celery. Celery tasks go through a specific sequence of states: pending, started, and success (or failure). By hooking into these states, we can start a recording before the task executes and stop the recording once the task completes, successfully or otherwise.
We will extend the Celery task class to include AppMap recording functionality. This involves importing the AppMap recorder, starting the recording upon task initiation, and stopping it upon task completion or failure. The recorded AppMap data should be saved in a specified directory, similarly to how request recordings are handled.
Proposed Changes
appmap/labeling/jobs.yml: Ensure Celery task classes are included in the job creation configuration:celery.app.task.Task.apply_asyncunderjob.create._appmap/recorder.py: Create helper functions if necessary to standardize the start and stop of recording processes. Confirm that the AppMap recorder follows the lifecycle of the task correctly.start_recording()andstop_recording()are suitable for re-use in the context of celery tasks.Create a new file or update an existing integration module (e.g.,
_appmap/integration/celery.py): Augmentcelery.Taskto include AppMap recording steps:celery.Taskclass such that the task lifecycle includesAppMaprecording steps._appmap/configuration.py: Ensure that the configuration for celery is correctly captured and can be enabled via the AppMap configuration._appmap/test/test_celery.py: Add tests to validate that Celery tasks are correctly generating AppMap recordings:Detailed File Changes
appmap/labeling/jobs.yml:Add the Celery task class to the job creation configuration.
_appmap/recorder.py:Make sure existing methods support celery tasks recording; if not, adjust the existing methods to support additional context if necessary.
Create or update an integration file
_appmap/integration/celery.py:_appmap/configuration.py:Ensure the configuration loader recognizes celery-related configurations:
_appmap/test/test_celery.py:Add tests ensuring that AppMaps are generated when Celery tasks execute.
By implementing these changes, Celery tasks will each generate their own AppMap recording, capturing execution details and improving observability for Celery-managed tasks.