1
+ from __future__ import annotations
1
2
import os
2
3
import random
3
4
import threading
4
5
from datetime import datetime , timezone
5
- from typing import Optional , List , Callable , TYPE_CHECKING , Any
6
6
7
7
from sentry_sdk .utils import format_timestamp , safe_repr
8
8
from sentry_sdk .envelope import Envelope , Item , PayloadRef
9
9
10
+ from typing import TYPE_CHECKING
11
+
10
12
if TYPE_CHECKING :
11
13
from sentry_sdk ._types import Log
14
+ from typing import Optional , List , Callable , Any
12
15
13
16
14
17
class LogBatcher :
15
18
MAX_LOGS_BEFORE_FLUSH = 100
16
19
FLUSH_WAIT_TIME = 5.0
17
20
18
- def __init__ (
19
- self ,
20
- capture_func , # type: Callable[[Envelope], None]
21
- ):
22
- # type: (...) -> None
23
- self ._log_buffer = [] # type: List[Log]
21
+ def __init__ (self , capture_func : Callable [[Envelope ], None ]) -> None :
22
+ self ._log_buffer : List [Log ] = []
24
23
self ._capture_func = capture_func
25
24
self ._running = True
26
25
self ._lock = threading .Lock ()
27
26
28
- self ._flush_event = threading .Event () # type: threading.Event
27
+ self ._flush_event : threading .Event = threading .Event ()
29
28
30
- self ._flusher = None # type : Optional[threading.Thread]
31
- self ._flusher_pid = None # type : Optional[int]
29
+ self ._flusher : Optional [threading .Thread ] = None
30
+ self ._flusher_pid : Optional [int ] = None
32
31
33
- def _ensure_thread (self ):
34
- # type: (...) -> bool
32
+ def _ensure_thread (self ) -> bool :
35
33
"""For forking processes we might need to restart this thread.
36
34
This ensures that our process actually has that thread running.
37
35
"""
@@ -63,18 +61,13 @@ def _ensure_thread(self):
63
61
64
62
return True
65
63
66
- def _flush_loop (self ):
67
- # type: (...) -> None
64
+ def _flush_loop (self ) -> None :
68
65
while self ._running :
69
66
self ._flush_event .wait (self .FLUSH_WAIT_TIME + random .random ())
70
67
self ._flush_event .clear ()
71
68
self ._flush ()
72
69
73
- def add (
74
- self ,
75
- log , # type: Log
76
- ):
77
- # type: (...) -> None
70
+ def add (self , log : Log ) -> None :
78
71
if not self ._ensure_thread () or self ._flusher is None :
79
72
return None
80
73
@@ -83,24 +76,20 @@ def add(
83
76
if len (self ._log_buffer ) >= self .MAX_LOGS_BEFORE_FLUSH :
84
77
self ._flush_event .set ()
85
78
86
- def kill (self ):
87
- # type: (...) -> None
79
+ def kill (self ) -> None :
88
80
if self ._flusher is None :
89
81
return
90
82
91
83
self ._running = False
92
84
self ._flush_event .set ()
93
85
self ._flusher = None
94
86
95
- def flush (self ):
96
- # type: (...) -> None
87
+ def flush (self ) -> None :
97
88
self ._flush ()
98
89
99
90
@staticmethod
100
- def _log_to_transport_format (log ):
101
- # type: (Log) -> Any
102
- def format_attribute (val ):
103
- # type: (int | float | str | bool) -> Any
91
+ def _log_to_transport_format (log : Log ) -> Any :
92
+ def format_attribute (val : int | float | str | bool ) -> Any :
104
93
if isinstance (val , bool ):
105
94
return {"value" : val , "type" : "boolean" }
106
95
if isinstance (val , int ):
@@ -128,8 +117,7 @@ def format_attribute(val):
128
117
129
118
return res
130
119
131
- def _flush (self ):
132
- # type: (...) -> Optional[Envelope]
120
+ def _flush (self ) -> Optional [Envelope ]:
133
121
134
122
envelope = Envelope (
135
123
headers = {"sent_at" : format_timestamp (datetime .now (timezone .utc ))}
0 commit comments