1414from tux .utils .config import CONFIG
1515from tux .utils .sentry import safe_set_name , span , start_span , transaction
1616
17+ IS_INITIALIZED : bool = sentry_sdk .is_initialized ()
18+
1719
1820class CogLoadError (Exception ):
1921 """Raised when a cog fails to load."""
@@ -90,7 +92,7 @@ async def _load_single_cog(self, path: Path) -> None:
9092 cog_name = path .stem
9193
9294 # 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 ()):
9496 current_span .set_tag ("cog.name" , cog_name )
9597 current_span .set_tag ("cog.path" , str (path ))
9698
@@ -101,7 +103,7 @@ async def _load_single_cog(self, path: Path) -> None:
101103 # Convert path to module format (e.g., tux.cogs.admin.dev)
102104 module = f"tux.{ str (relative_path ).replace ('/' , '.' ).replace ('\\ ' , '.' )[:- 3 ]} "
103105
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 ()):
105107 current_span .set_tag ("cog.module" , module )
106108
107109 # Check if this module or any parent module is already loaded
@@ -112,7 +114,7 @@ async def _load_single_cog(self, path: Path) -> None:
112114 check_module = "." .join (module_parts [:i ])
113115 if check_module in self .bot .extensions :
114116 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 ()):
116118 current_span .set_tag ("cog.status" , "skipped" )
117119 current_span .set_tag ("cog.skip_reason" , "already_loaded" )
118120 current_span .set_data ("already_loaded_module" , check_module )
@@ -124,15 +126,15 @@ async def _load_single_cog(self, path: Path) -> None:
124126 self .load_times [module ] = load_time
125127
126128 # 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 ()):
128130 current_span .set_tag ("cog.status" , "loaded" )
129131 current_span .set_data ("load_time_ms" , load_time * 1000 )
130132 current_span .set_data ("load_time_s" , load_time )
131133
132134 logger .debug (f"Successfully loaded cog { module } in { load_time * 1000 :.0f} ms" )
133135
134136 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 ()):
136138 current_span .set_status ("internal_error" )
137139 current_span .set_tag ("cog.status" , "failed" )
138140 current_span .set_data ("error" , str (e ))
@@ -173,7 +175,7 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None:
173175 return
174176
175177 # 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 ()):
177179 current_span .set_data ("cog_count" , len (cogs ))
178180
179181 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:
188190 success_count = len ([r for r in results if not isinstance (r , Exception )])
189191 failure_count = len (results ) - success_count
190192
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 ()):
192194 current_span .set_data ("load_time_s" , end_time - start_time )
193195 current_span .set_data ("success_count" , success_count )
194196 current_span .set_data ("failure_count" , failure_count )
@@ -200,14 +202,14 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None:
200202
201203 async def _process_single_file (self , path : Path ) -> None :
202204 """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 ()):
204206 current_span .set_tag ("path.is_dir" , False )
205207 if await self .is_cog_eligible (path ):
206208 await self ._load_single_cog (path )
207209
208210 async def _process_directory (self , path : Path ) -> None :
209211 """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 ()):
211213 current_span .set_tag ("path.is_dir" , True )
212214
213215 # Collect and sort eligible cogs by priority
@@ -216,7 +218,7 @@ async def _process_directory(self, path: Path) -> None:
216218 ]
217219 cog_paths .sort (key = lambda x : x [0 ], reverse = True )
218220
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 ()):
220222 current_span .set_data ("eligible_cog_count" , len (cog_paths ))
221223
222224 # Priority groups info for observability
@@ -254,7 +256,7 @@ async def load_cogs(self, path: Path) -> None:
254256 The path to the directory containing cogs.
255257 """
256258 # 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 ()):
258260 current_span .set_tag ("cog.path" , str (path ))
259261
260262 try :
@@ -268,7 +270,7 @@ async def load_cogs(self, path: Path) -> None:
268270 path_str = path .as_posix ()
269271 logger .error (f"An error occurred while processing { path_str } : { e } " )
270272
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 ()):
272274 current_span .set_status ("internal_error" )
273275 current_span .set_data ("error" , str (e ))
274276 current_span .set_data ("traceback" , traceback .format_exc ())
@@ -286,22 +288,22 @@ async def load_cogs_from_folder(self, folder_name: str) -> None:
286288 The name of the folder containing the cogs.
287289 """
288290 # 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 ()):
290292 current_span .set_tag ("cog.folder" , folder_name )
291293 # Use safe_set_name instead of direct set_name call
292294 safe_set_name (current_span , f"Load Cogs: { folder_name } " )
293295
294296 start_time = time .perf_counter ()
295297 cog_path : Path = Path (__file__ ).parent / folder_name
296298
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 ()):
298300 current_span .set_data ("full_path" , str (cog_path ))
299301
300302 try :
301303 await self .load_cogs (path = cog_path )
302304 load_time = time .perf_counter () - start_time
303305
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 ()):
305307 current_span .set_data ("load_time_s" , load_time )
306308 current_span .set_data ("load_time_ms" , load_time * 1000 )
307309
@@ -311,12 +313,12 @@ async def load_cogs_from_folder(self, folder_name: str) -> None:
311313 # Log individual cog load times for performance monitoring
312314 slow_threshold = 1.0 # seconds
313315 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 ()):
315317 current_span .set_data ("slow_cogs" , slow_cogs )
316318 logger .warning (f"Slow loading cogs (>{ slow_threshold * 1000 :.0f} ms): { slow_cogs } " )
317319
318320 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 ()):
320322 current_span .set_status ("internal_error" )
321323 current_span .set_data ("error" , str (e ))
322324 current_span .set_data ("traceback" , traceback .format_exc ())
@@ -335,12 +337,12 @@ async def setup(cls, bot: commands.Bot) -> None:
335337 bot : commands.Bot
336338 The bot instance.
337339 """
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-
341340 start_time = time .perf_counter ()
342341 cog_loader = cls (bot )
343342
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+
344346 try :
345347 # Load handlers first (they have highest priority)
346348 with start_span ("cog.load_handlers" , "Load handler cogs" ):
@@ -356,7 +358,7 @@ async def setup(cls, bot: commands.Bot) -> None:
356358
357359 total_time = time .perf_counter () - start_time
358360
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 ()):
360362 current_span .set_data ("total_load_time_s" , total_time )
361363 current_span .set_data ("total_load_time_ms" , total_time * 1000 )
362364
@@ -367,7 +369,7 @@ async def setup(cls, bot: commands.Bot) -> None:
367369 logger .info (f"Total cog loading time: { total_time * 1000 :.0f} ms" )
368370
369371 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 ()):
371373 current_span .set_status ("internal_error" )
372374 current_span .set_data ("error" , str (e ))
373375 current_span .set_data ("traceback" , traceback .format_exc ())
0 commit comments