@@ -94,53 +94,63 @@ def __init__(self, options):
9494 self ._retry = urllib3 .util .Retry ()
9595 self .options = options
9696
97+ from sentry_sdk import Hub
98+
99+ self .hub_cls = Hub
100+
97101 def _send_event (self , event ):
98102 if self ._disabled_until is not None :
99103 if datetime .utcnow () < self ._disabled_until :
100104 return
101105 self ._disabled_until = None
102106
103- with capture_internal_exceptions ():
104- body = io .BytesIO ()
105- with gzip .GzipFile (fileobj = body , mode = "w" ) as f :
106- f .write (json .dumps (event ).encode ("utf-8" ))
107-
108- logger .debug (
109- "Sending %s event [%s] to %s project:%s"
110- % (
111- event .get ("level" ) or "error" ,
112- event ["event_id" ],
113- self .parsed_dsn .host ,
114- self .parsed_dsn .project_id ,
115- )
116- )
117- response = self ._pool .request (
118- "POST" ,
119- str (self ._auth .store_api_url ),
120- body = body .getvalue (),
121- headers = {
122- "X-Sentry-Auth" : str (self ._auth .to_header ()),
123- "Content-Type" : "application/json" ,
124- "Content-Encoding" : "gzip" ,
125- },
107+ body = io .BytesIO ()
108+ with gzip .GzipFile (fileobj = body , mode = "w" ) as f :
109+ f .write (json .dumps (event ).encode ("utf-8" ))
110+
111+ logger .debug (
112+ "Sending %s event [%s] to %s project:%s"
113+ % (
114+ event .get ("level" ) or "error" ,
115+ event ["event_id" ],
116+ self .parsed_dsn .host ,
117+ self .parsed_dsn .project_id ,
126118 )
119+ )
120+ response = self ._pool .request (
121+ "POST" ,
122+ str (self ._auth .store_api_url ),
123+ body = body .getvalue (),
124+ headers = {
125+ "X-Sentry-Auth" : str (self ._auth .to_header ()),
126+ "Content-Type" : "application/json" ,
127+ "Content-Encoding" : "gzip" ,
128+ },
129+ )
127130
128- try :
129- if response .status == 429 :
130- self ._disabled_until = datetime .utcnow () + timedelta (
131- seconds = self ._retry .get_retry_after (response )
132- )
133- return
131+ try :
132+ if response .status == 429 :
133+ self ._disabled_until = datetime .utcnow () + timedelta (
134+ seconds = self ._retry .get_retry_after (response )
135+ )
136+ return
134137
135- elif response .status >= 300 or response .status < 200 :
136- raise ValueError ("Unexpected status code: %s" % response .status )
137- finally :
138- response .close ()
138+ elif response .status >= 300 or response .status < 200 :
139+ raise ValueError ("Unexpected status code: %s" % response .status )
140+ finally :
141+ response .close ()
139142
140- self ._disabled_until = None
143+ self ._disabled_until = None
141144
142145 def capture_event (self , event ):
143- self ._worker .submit (lambda : self ._send_event (event ))
146+ hub = self .hub_cls .current
147+
148+ def send_event_wrapper ():
149+ with hub :
150+ with capture_internal_exceptions ():
151+ self ._send_event (event )
152+
153+ self ._worker .submit (send_event_wrapper )
144154
145155 def shutdown (self , timeout , callback = None ):
146156 logger .debug ("Shutting down HTTP transport orderly" )
0 commit comments