diff --git a/ddtrace/contrib/internal/celery/utils.py b/ddtrace/contrib/internal/celery/utils.py index 80a4046f191..ee803439629 100644 --- a/ddtrace/contrib/internal/celery/utils.py +++ b/ddtrace/contrib/internal/celery/utils.py @@ -128,9 +128,12 @@ def retrieve_task_id(context): """ headers = context.get("headers") body = context.get("body") - if headers: + # Check if the id is in the headers, then check the body for it. + # If we don't check the id first, we could wrongly assume no task_id + # when the task_id is in the body. + if headers and 'id' in headers: # Protocol Version 2 (default from Celery 4.0) return headers.get("id") - else: + elif body and 'id' in body: # Protocol Version 1 return body.get("id") diff --git a/releasenotes/notes/fix-protocol-check-62ad7d096b1f36a9.yaml b/releasenotes/notes/fix-protocol-check-62ad7d096b1f36a9.yaml new file mode 100644 index 00000000000..db0850e2f81 --- /dev/null +++ b/releasenotes/notes/fix-protocol-check-62ad7d096b1f36a9.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + tracing(celery): Fixes an issue where ``celery.apply`` spans using protocol 1 didn't close by improving the check for the task id in the body. \ No newline at end of file