1+ from __future__ import annotations
12import os
23import random
34import threading
@@ -15,23 +16,18 @@ class LogBatcher:
1516 MAX_LOGS_BEFORE_FLUSH = 100
1617 FLUSH_WAIT_TIME = 5.0
1718
18- def __init__ (
19- self ,
20- capture_func , # type: Callable[[Envelope], None]
21- ):
22- # type: (...) -> None
23- self ._log_buffer = [] # type: List[Log]
19+ def __init__ (self , capture_func : Callable [[Envelope ], None ]) -> None :
20+ self ._log_buffer : List [Log ] = []
2421 self ._capture_func = capture_func
2522 self ._running = True
2623 self ._lock = threading .Lock ()
2724
28- self ._flush_event = threading .Event () # type: threading.Event
25+ self ._flush_event : threading .Event = threading .Event ()
2926
30- self ._flusher = None # type : Optional[threading.Thread]
31- self ._flusher_pid = None # type : Optional[int]
27+ self ._flusher : Optional [threading .Thread ] = None
28+ self ._flusher_pid : Optional [int ] = None
3229
33- def _ensure_thread (self ):
34- # type: (...) -> bool
30+ def _ensure_thread (self ) -> bool :
3531 """For forking processes we might need to restart this thread.
3632 This ensures that our process actually has that thread running.
3733 """
@@ -63,18 +59,13 @@ def _ensure_thread(self):
6359
6460 return True
6561
66- def _flush_loop (self ):
67- # type: (...) -> None
62+ def _flush_loop (self ) -> None :
6863 while self ._running :
6964 self ._flush_event .wait (self .FLUSH_WAIT_TIME + random .random ())
7065 self ._flush_event .clear ()
7166 self ._flush ()
7267
73- def add (
74- self ,
75- log , # type: Log
76- ):
77- # type: (...) -> None
68+ def add (self , log : Log ) -> None :
7869 if not self ._ensure_thread () or self ._flusher is None :
7970 return None
8071
@@ -83,24 +74,20 @@ def add(
8374 if len (self ._log_buffer ) >= self .MAX_LOGS_BEFORE_FLUSH :
8475 self ._flush_event .set ()
8576
86- def kill (self ):
87- # type: (...) -> None
77+ def kill (self ) -> None :
8878 if self ._flusher is None :
8979 return
9080
9181 self ._running = False
9282 self ._flush_event .set ()
9383 self ._flusher = None
9484
95- def flush (self ):
96- # type: (...) -> None
85+ def flush (self ) -> None :
9786 self ._flush ()
9887
9988 @staticmethod
100- def _log_to_transport_format (log ):
101- # type: (Log) -> Any
102- def format_attribute (val ):
103- # type: (int | float | str | bool) -> Any
89+ def _log_to_transport_format (log : Log ) -> Any :
90+ def format_attribute (val : int | float | str | bool ) -> Any :
10491 if isinstance (val , bool ):
10592 return {"value" : val , "type" : "boolean" }
10693 if isinstance (val , int ):
@@ -128,8 +115,7 @@ def format_attribute(val):
128115
129116 return res
130117
131- def _flush (self ):
132- # type: (...) -> Optional[Envelope]
118+ def _flush (self ) -> Optional [Envelope ]:
133119
134120 envelope = Envelope (
135121 headers = {"sent_at" : format_timestamp (datetime .now (timezone .utc ))}
0 commit comments