14
14
from tux .utils .config import CONFIG
15
15
from tux .utils .sentry import safe_set_name , span , start_span , transaction
16
16
17
+ IS_INITIALIZED : bool = sentry_sdk .is_initialized ()
18
+
17
19
18
20
class CogLoadError (Exception ):
19
21
"""Raised when a cog fails to load."""
@@ -90,7 +92,7 @@ async def _load_single_cog(self, path: Path) -> None:
90
92
cog_name = path .stem
91
93
92
94
# Add span tags for the current cog
93
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
95
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
94
96
current_span .set_tag ("cog.name" , cog_name )
95
97
current_span .set_tag ("cog.path" , str (path ))
96
98
@@ -101,7 +103,7 @@ async def _load_single_cog(self, path: Path) -> None:
101
103
# Convert path to module format (e.g., tux.cogs.admin.dev)
102
104
module = f"tux.{ str (relative_path ).replace ('/' , '.' ).replace ('\\ ' , '.' )[:- 3 ]} "
103
105
104
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
106
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
105
107
current_span .set_tag ("cog.module" , module )
106
108
107
109
# Check if this module or any parent module is already loaded
@@ -112,7 +114,7 @@ async def _load_single_cog(self, path: Path) -> None:
112
114
check_module = "." .join (module_parts [:i ])
113
115
if check_module in self .bot .extensions :
114
116
logger .warning (f"Skipping { module } as { check_module } is already loaded" )
115
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
117
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
116
118
current_span .set_tag ("cog.status" , "skipped" )
117
119
current_span .set_tag ("cog.skip_reason" , "already_loaded" )
118
120
current_span .set_data ("already_loaded_module" , check_module )
@@ -124,15 +126,15 @@ async def _load_single_cog(self, path: Path) -> None:
124
126
self .load_times [module ] = load_time
125
127
126
128
# Add telemetry data to span
127
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
129
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
128
130
current_span .set_tag ("cog.status" , "loaded" )
129
131
current_span .set_data ("load_time_ms" , load_time * 1000 )
130
132
current_span .set_data ("load_time_s" , load_time )
131
133
132
134
logger .debug (f"Successfully loaded cog { module } in { load_time * 1000 :.0f} ms" )
133
135
134
136
except Exception as e :
135
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
137
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
136
138
current_span .set_status ("internal_error" )
137
139
current_span .set_tag ("cog.status" , "failed" )
138
140
current_span .set_data ("error" , str (e ))
@@ -173,7 +175,7 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None:
173
175
return
174
176
175
177
# Add basic info for the group
176
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
178
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
177
179
current_span .set_data ("cog_count" , len (cogs ))
178
180
179
181
if categories := {cog .parent .name for cog in cogs if cog .parent }:
@@ -188,7 +190,7 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None:
188
190
success_count = len ([r for r in results if not isinstance (r , Exception )])
189
191
failure_count = len (results ) - success_count
190
192
191
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
193
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
192
194
current_span .set_data ("load_time_s" , end_time - start_time )
193
195
current_span .set_data ("success_count" , success_count )
194
196
current_span .set_data ("failure_count" , failure_count )
@@ -200,14 +202,14 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None:
200
202
201
203
async def _process_single_file (self , path : Path ) -> None :
202
204
"""Process a single file path."""
203
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
205
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
204
206
current_span .set_tag ("path.is_dir" , False )
205
207
if await self .is_cog_eligible (path ):
206
208
await self ._load_single_cog (path )
207
209
208
210
async def _process_directory (self , path : Path ) -> None :
209
211
"""Process a directory of cogs."""
210
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
212
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
211
213
current_span .set_tag ("path.is_dir" , True )
212
214
213
215
# Collect and sort eligible cogs by priority
@@ -216,7 +218,7 @@ async def _process_directory(self, path: Path) -> None:
216
218
]
217
219
cog_paths .sort (key = lambda x : x [0 ], reverse = True )
218
220
219
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
221
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
220
222
current_span .set_data ("eligible_cog_count" , len (cog_paths ))
221
223
222
224
# Priority groups info for observability
@@ -254,7 +256,7 @@ async def load_cogs(self, path: Path) -> None:
254
256
The path to the directory containing cogs.
255
257
"""
256
258
# Add span context
257
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
259
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
258
260
current_span .set_tag ("cog.path" , str (path ))
259
261
260
262
try :
@@ -268,7 +270,7 @@ async def load_cogs(self, path: Path) -> None:
268
270
path_str = path .as_posix ()
269
271
logger .error (f"An error occurred while processing { path_str } : { e } " )
270
272
271
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
273
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
272
274
current_span .set_status ("internal_error" )
273
275
current_span .set_data ("error" , str (e ))
274
276
current_span .set_data ("traceback" , traceback .format_exc ())
@@ -286,22 +288,22 @@ async def load_cogs_from_folder(self, folder_name: str) -> None:
286
288
The name of the folder containing the cogs.
287
289
"""
288
290
# Add span info
289
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
291
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
290
292
current_span .set_tag ("cog.folder" , folder_name )
291
293
# Use safe_set_name instead of direct set_name call
292
294
safe_set_name (current_span , f"Load Cogs: { folder_name } " )
293
295
294
296
start_time = time .perf_counter ()
295
297
cog_path : Path = Path (__file__ ).parent / folder_name
296
298
297
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
299
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
298
300
current_span .set_data ("full_path" , str (cog_path ))
299
301
300
302
try :
301
303
await self .load_cogs (path = cog_path )
302
304
load_time = time .perf_counter () - start_time
303
305
304
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
306
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
305
307
current_span .set_data ("load_time_s" , load_time )
306
308
current_span .set_data ("load_time_ms" , load_time * 1000 )
307
309
@@ -311,12 +313,12 @@ async def load_cogs_from_folder(self, folder_name: str) -> None:
311
313
# Log individual cog load times for performance monitoring
312
314
slow_threshold = 1.0 # seconds
313
315
if slow_cogs := {k : v for k , v in self .load_times .items () if v > slow_threshold }:
314
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
316
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
315
317
current_span .set_data ("slow_cogs" , slow_cogs )
316
318
logger .warning (f"Slow loading cogs (>{ slow_threshold * 1000 :.0f} ms): { slow_cogs } " )
317
319
318
320
except Exception as e :
319
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
321
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
320
322
current_span .set_status ("internal_error" )
321
323
current_span .set_data ("error" , str (e ))
322
324
current_span .set_data ("traceback" , traceback .format_exc ())
@@ -335,12 +337,12 @@ async def setup(cls, bot: commands.Bot) -> None:
335
337
bot : commands.Bot
336
338
The bot instance.
337
339
"""
338
- if sentry_sdk .is_initialized () and (current_span := sentry_sdk .get_current_span ()):
339
- current_span .set_tag ("bot.id" , bot .user .id if bot .user else "unknown" )
340
-
341
340
start_time = time .perf_counter ()
342
341
cog_loader = cls (bot )
343
342
343
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
344
+ current_span .set_tag ("bot.id" , bot .user .id if bot .user else "unknown" )
345
+
344
346
try :
345
347
# Load handlers first (they have highest priority)
346
348
with start_span ("cog.load_handlers" , "Load handler cogs" ):
@@ -356,7 +358,7 @@ async def setup(cls, bot: commands.Bot) -> None:
356
358
357
359
total_time = time .perf_counter () - start_time
358
360
359
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
361
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
360
362
current_span .set_data ("total_load_time_s" , total_time )
361
363
current_span .set_data ("total_load_time_ms" , total_time * 1000 )
362
364
@@ -367,7 +369,7 @@ async def setup(cls, bot: commands.Bot) -> None:
367
369
logger .info (f"Total cog loading time: { total_time * 1000 :.0f} ms" )
368
370
369
371
except Exception as e :
370
- if sentry_sdk . is_initialized () and (current_span := sentry_sdk .get_current_span ()):
372
+ if IS_INITIALIZED and (current_span := sentry_sdk .get_current_span ()):
371
373
current_span .set_status ("internal_error" )
372
374
current_span .set_data ("error" , str (e ))
373
375
current_span .set_data ("traceback" , traceback .format_exc ())
0 commit comments