1
1
import base64
2
2
from queue import Queue
3
- from typing import Optional , TypeVar , cast
3
+ from typing import Optional , cast
4
4
from typing_extensions import TypeAlias
5
5
6
6
from confidence .telemetry_pb2 import (
15
15
ProtoTraceId : TypeAlias = ProtoLibraryTraces .ProtoTraceId
16
16
ProtoStatus : TypeAlias = ProtoLibraryTraces .ProtoTrace .ProtoRequestTrace .ProtoStatus
17
17
18
- T = TypeVar ("T" )
19
- QueueType = Queue
20
-
21
18
22
19
class Telemetry :
23
20
_instance : Optional ["Telemetry" ] = None
24
21
_initialized : bool = False
25
22
version : str
26
- _traces_queue : "QueueType [ProtoTrace]"
23
+ _traces_queue : Queue [ProtoTrace ]
27
24
_disabled : bool
28
25
29
26
def __new__ (cls , version : str , disabled : bool = False ) -> "Telemetry" :
@@ -36,7 +33,7 @@ def __new__(cls, version: str, disabled: bool = False) -> "Telemetry":
36
33
def __init__ (self , version : str , disabled : bool = False ) -> None :
37
34
if not self ._initialized :
38
35
self .version = version
39
- self ._traces_queue = cast ( "QueueType[ProtoTrace]" , Queue () )
36
+ self ._traces_queue = Queue ()
40
37
self ._disabled = disabled
41
38
self ._initialized = True
42
39
@@ -56,23 +53,19 @@ def add_trace(
56
53
def get_monitoring_header (self ) -> str :
57
54
if self ._disabled :
58
55
return ""
59
- # Get all current traces atomically
60
56
current_traces = []
61
57
while not self ._traces_queue .empty ():
62
58
try :
63
59
current_traces .append (self ._traces_queue .get_nowait ())
64
- except Exception : # Specify the exception type
60
+ except Exception :
65
61
break
66
62
67
- # Create monitoring data with the captured traces
68
63
monitoring = ProtoMonitoring ()
69
64
library_traces = monitoring .library_traces .add ()
70
65
library_traces .library = ProtoLibrary .PROTO_LIBRARY_CONFIDENCE
71
66
library_traces .library_version = self .version
72
67
library_traces .traces .extend (current_traces )
73
68
monitoring .platform = ProtoPlatform .PROTO_PLATFORM_PYTHON
74
-
75
- # Serialize to protobuf and base64 encode
76
69
serialized = monitoring .SerializeToString ()
77
70
encoded = base64 .b64encode (serialized ).decode ()
78
71
return encoded
0 commit comments