22
33import sentry_sdk
44from sentry_sdk ._types import MYPY
5- from sentry_sdk .consts import OP
5+ from sentry_sdk .consts import OP , SPANDATA
66from sentry_sdk .integrations import Integration
7+ from sentry_sdk .tracing_utils import has_span_streaming_enabled
78
89if MYPY :
910 from socket import AddressFamily , SocketKind
@@ -50,22 +51,39 @@ def create_connection(
5051 timeout : "Optional[float]" = socket ._GLOBAL_DEFAULT_TIMEOUT , # type: ignore
5152 source_address : "Optional[Tuple[Union[bytearray, bytes, str], int]]" = None ,
5253 ) -> "socket.socket" :
53- integration = sentry_sdk .get_client ().get_integration (SocketIntegration )
54+ client = sentry_sdk .get_client ()
55+ integration = client .get_integration (SocketIntegration )
5456 if integration is None :
5557 return real_create_connection (address , timeout , source_address )
5658
57- with sentry_sdk .start_span (
58- op = OP .SOCKET_CONNECTION ,
59- name = _get_span_description (address [0 ], address [1 ]),
60- origin = SocketIntegration .origin ,
61- ) as span :
62- span .set_data ("address" , address )
63- span .set_data ("timeout" , timeout )
64- span .set_data ("source_address" , source_address )
65-
66- return real_create_connection (
67- address = address , timeout = timeout , source_address = source_address
68- )
59+ if has_span_streaming_enabled (client .options ):
60+ with sentry_sdk .traces .start_span (
61+ name = _get_span_description (address [0 ], address [1 ]),
62+ attributes = {
63+ "sentry.op" : OP .SOCKET_CONNECTION ,
64+ "sentry.origin" : SocketIntegration .origin ,
65+ },
66+ ) as span :
67+ if address [0 ] is not None :
68+ span .set_attribute (SPANDATA .SERVER_ADDRESS , address [0 ])
69+ span .set_attribute (SPANDATA .SERVER_PORT , address [1 ])
70+
71+ return real_create_connection (
72+ address = address , timeout = timeout , source_address = source_address
73+ )
74+ else :
75+ with sentry_sdk .start_span (
76+ op = OP .SOCKET_CONNECTION ,
77+ name = _get_span_description (address [0 ], address [1 ]),
78+ origin = SocketIntegration .origin ,
79+ ) as span :
80+ span .set_data ("address" , address )
81+ span .set_data ("timeout" , timeout )
82+ span .set_data ("source_address" , source_address )
83+
84+ return real_create_connection (
85+ address = address , timeout = timeout , source_address = source_address
86+ )
6987
7088 socket .create_connection = create_connection # type: ignore
7189
@@ -81,18 +99,44 @@ def getaddrinfo(
8199 proto : int = 0 ,
82100 flags : int = 0 ,
83101 ) -> "List[Tuple[AddressFamily, SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int], Tuple[int, bytes]]]]" :
84- integration = sentry_sdk .get_client ().get_integration (SocketIntegration )
102+ client = sentry_sdk .get_client ()
103+ integration = client .get_integration (SocketIntegration )
85104 if integration is None :
86105 return real_getaddrinfo (host , port , family , type , proto , flags )
87106
88- with sentry_sdk .start_span (
89- op = OP .SOCKET_DNS ,
90- name = _get_span_description (host , port ),
91- origin = SocketIntegration .origin ,
92- ) as span :
93- span .set_data ("host" , host )
94- span .set_data ("port" , port )
95-
96- return real_getaddrinfo (host , port , family , type , proto , flags )
107+ if has_span_streaming_enabled (client .options ):
108+ with sentry_sdk .traces .start_span (
109+ name = _get_span_description (host , port ),
110+ attributes = {
111+ "sentry.op" : OP .SOCKET_DNS ,
112+ "sentry.origin" : SocketIntegration .origin ,
113+ },
114+ ) as span :
115+ if isinstance (host , str ):
116+ span .set_attribute (SPANDATA .SERVER_ADDRESS , host )
117+ elif isinstance (host , bytes ):
118+ span .set_attribute (
119+ SPANDATA .SERVER_ADDRESS , host .decode (errors = "replace" )
120+ )
121+
122+ if isinstance (port , int ):
123+ span .set_attribute (SPANDATA .SERVER_PORT , port )
124+ elif port is not None :
125+ try :
126+ span .set_attribute (SPANDATA .SERVER_PORT , int (port ))
127+ except (ValueError , TypeError ):
128+ pass
129+
130+ return real_getaddrinfo (host , port , family , type , proto , flags )
131+ else :
132+ with sentry_sdk .start_span (
133+ op = OP .SOCKET_DNS ,
134+ name = _get_span_description (host , port ),
135+ origin = SocketIntegration .origin ,
136+ ) as span :
137+ span .set_data ("host" , host )
138+ span .set_data ("port" , port )
139+
140+ return real_getaddrinfo (host , port , family , type , proto , flags )
97141
98142 socket .getaddrinfo = getaddrinfo
0 commit comments