Skip to content

Commit

Permalink
Unwrap ExceptionInfo and ExceptionWithTraceback
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
unflxw committed Jun 15, 2023
1 parent a5ed4da commit a0f8d11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `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))
- Instrument all httpx versions >= 0.18. ([#1748](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1748))

## Version 1.18.0/0.39b0 (2023-05-10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def add(x, y):
from typing import Collection, Iterable

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

from opentelemetry import trace
from opentelemetry.instrumentation.celery import utils
Expand Down Expand Up @@ -256,6 +257,19 @@ def _trace_failure(*args, **kwargs):
return

if ex is not None:
# Unwrap the actual exception wrapped by billiard's
# `ExceptionInfo` and `ExceptionWithTraceback`.
if (
isinstance(ex, ExceptionInfo)
and ex.exception is not None
):
ex = ex.exception
elif (
isinstance(ex, ExceptionWithTraceback)
and ex.exc is not None
):
ex = ex.exc

status_kwargs["description"] = str(ex)
span.record_exception(ex)
span.set_status(Status(**status_kwargs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def fn_exception():
assert len(span.events) == 1
event = span.events[0]
assert event.name == "exception"
assert event.attributes[SpanAttributes.EXCEPTION_TYPE] == "ExceptionInfo"
assert event.attributes[SpanAttributes.EXCEPTION_TYPE] == "Exception"
assert SpanAttributes.EXCEPTION_MESSAGE in event.attributes
assert (
span.attributes.get(SpanAttributes.MESSAGING_MESSAGE_ID)
Expand Down

0 comments on commit a0f8d11

Please sign in to comment.