Skip to content

Commit dfe8598

Browse files
committed
feat(database): add support for synchronous methods in DatabaseController
Enhance the DatabaseController to handle both asynchronous and synchronous methods. Previously, only asynchronous methods were wrapped with Sentry spans for monitoring. By checking if the original method is asynchronous, the code now conditionally wraps it with either an async or sync wrapper. This change increases the flexibility of the DatabaseController, allowing it to support a wider range of method types while maintaining error tracking and performance monitoring capabilities.
1 parent fd82d0a commit dfe8598

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

tux/database/controllers/__init__.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,56 @@ def _create_wrapped_method(self, instance: Any, method_name: str, original_metho
101101
original_method : Any
102102
The original method to wrap
103103
"""
104-
105-
@functools.wraps(original_method)
106-
async def wrapped_method(*args: Any, **kwargs: Any) -> Any:
107-
controller_name = instance.__class__.__name__
108-
with sentry_sdk.start_span(
109-
op=f"db.controller.{method_name}",
110-
description=f"{controller_name}.{method_name}",
111-
) as span:
112-
span.set_tag("db.controller", controller_name)
113-
span.set_tag("db.operation", method_name)
114-
try:
115-
result = await original_method(*args, **kwargs)
116-
except Exception as e:
117-
span.set_status("internal_error")
118-
span.set_data("error", str(e))
119-
raise
120-
else:
121-
span.set_status("ok")
122-
return result
123-
124-
setattr(instance, method_name, wrapped_method)
104+
import inspect
105+
106+
# Check if the original method is async
107+
is_async = inspect.iscoroutinefunction(original_method)
108+
109+
if is_async:
110+
111+
@functools.wraps(original_method)
112+
async def async_wrapped_method(*args: Any, **kwargs: Any) -> Any:
113+
controller_name = instance.__class__.__name__
114+
with sentry_sdk.start_span(
115+
op=f"db.controller.{method_name}",
116+
description=f"{controller_name}.{method_name}",
117+
) as span:
118+
span.set_tag("db.controller", controller_name)
119+
span.set_tag("db.operation", method_name)
120+
try:
121+
result = await original_method(*args, **kwargs)
122+
except Exception as e:
123+
span.set_status("internal_error")
124+
span.set_data("error", str(e))
125+
raise
126+
else:
127+
span.set_status("ok")
128+
return result
129+
130+
setattr(instance, method_name, async_wrapped_method)
131+
132+
else:
133+
134+
@functools.wraps(original_method)
135+
def sync_wrapped_method(*args: Any, **kwargs: Any) -> Any:
136+
controller_name = instance.__class__.__name__
137+
with sentry_sdk.start_span(
138+
op=f"db.controller.{method_name}",
139+
description=f"{controller_name}.{method_name}",
140+
) as span:
141+
span.set_tag("db.controller", controller_name)
142+
span.set_tag("db.operation", method_name)
143+
try:
144+
result = original_method(*args, **kwargs)
145+
except Exception as e:
146+
span.set_status("internal_error")
147+
span.set_data("error", str(e))
148+
raise
149+
else:
150+
span.set_status("ok")
151+
return result
152+
153+
setattr(instance, method_name, sync_wrapped_method)
125154

126155
_controller_mapping: ClassVar[dict[str, type]] = {
127156
"afk": AfkController,

0 commit comments

Comments
 (0)