Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion autodynatrace/wrappers/dbapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
from ..utils import normalize_vendor


class TracedMethodException(Exception):
"""
Custom exception used to indicate an error occurred within a traced method.

This exception is used to wrap and re-raise exceptions that occur during the execution
of a method wrapped by the `_trace_method` function in the `TracedCursor` class. It helps
to maintain the context of the original exception while providing a clear indication that
the error occurred within a traced method and not in the SDK.
"""


class TracedCursor(wrapt.ObjectProxy):
def __init__(self, cursor, db_info):
super(TracedCursor, self).__init__(cursor)
Expand All @@ -24,8 +35,16 @@ def _trace_method(self, method, query, *args, **kwargs):

try:
with sdk.trace_sql_database_request(self.db_info, f"{query}"):
return method(*args, **kwargs)
try:
return method(*args, **kwargs)
except Exception as e:
# If an exception occurs in the `method``,
# raise the original exception from TracedMethodException.
raise e from TracedMethodException
except Exception as e:
if isinstance(e.__cause__, TracedMethodException):
# Re-raise the exception if it was caused by TracedMethodException
raise
logger.warning(f"Error instrumenting database: {e}")
return method(*args, **kwargs)

Expand Down