Skip to content

Commit a0f8d11

Browse files
committed
Unwrap ExceptionInfo and ExceptionWithTraceback
Instead of reporting the `ExceptionInfo` and `ExceptionWithTraceback` wrappers raised by Celery, report the exceptions that they wrap. This ensures that the exception in the OpenTelemetry span has a type and traceback that are meaningful and relevant to the developer.
1 parent a5ed4da commit a0f8d11

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- `opentelemetry-instrumentation-celery` Unwrap Celery's `ExceptionInfo` errors and report the actual exception that was raised. ([#1863](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1863))
1011
- Instrument all httpx versions >= 0.18. ([#1748](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1748))
1112

1213
## Version 1.18.0/0.39b0 (2023-05-10)

instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def add(x, y):
6363
from typing import Collection, Iterable
6464

6565
from celery import signals # pylint: disable=no-name-in-module
66+
from billiard.einfo import ExceptionInfo, ExceptionWithTraceback # pylint: disable=no-name-in-module
6667

6768
from opentelemetry import trace
6869
from opentelemetry.instrumentation.celery import utils
@@ -256,6 +257,19 @@ def _trace_failure(*args, **kwargs):
256257
return
257258

258259
if ex is not None:
260+
# Unwrap the actual exception wrapped by billiard's
261+
# `ExceptionInfo` and `ExceptionWithTraceback`.
262+
if (
263+
isinstance(ex, ExceptionInfo)
264+
and ex.exception is not None
265+
):
266+
ex = ex.exception
267+
elif (
268+
isinstance(ex, ExceptionWithTraceback)
269+
and ex.exc is not None
270+
):
271+
ex = ex.exc
272+
259273
status_kwargs["description"] = str(ex)
260274
span.record_exception(ex)
261275
span.set_status(Status(**status_kwargs))

tests/opentelemetry-docker-tests/tests/celery/test_celery_functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def fn_exception():
279279
assert len(span.events) == 1
280280
event = span.events[0]
281281
assert event.name == "exception"
282-
assert event.attributes[SpanAttributes.EXCEPTION_TYPE] == "ExceptionInfo"
282+
assert event.attributes[SpanAttributes.EXCEPTION_TYPE] == "Exception"
283283
assert SpanAttributes.EXCEPTION_MESSAGE in event.attributes
284284
assert (
285285
span.attributes.get(SpanAttributes.MESSAGING_MESSAGE_ID)

0 commit comments

Comments
 (0)