Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zawi99 committed Nov 20, 2024
1 parent 7c16cd6 commit 45dbb3c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion autodynatrace/wrappers/dbapi/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)

0 comments on commit 45dbb3c

Please sign in to comment.