@@ -173,7 +173,7 @@ def __enter__(self):
173173 def __exit__ (self , ty , value , tb ):
174174 # type: (Optional[Any], Optional[Any], Optional[Any]) -> None
175175 if value is not None :
176- self .set_failure ( )
176+ self ._tags . setdefault ( "status" , "internal_error" )
177177
178178 hub , scope , old_span = self ._context_manager_state
179179 del self ._context_manager_state
@@ -259,17 +259,44 @@ def set_data(self, key, value):
259259 # type: (str, Any) -> None
260260 self ._data [key ] = value
261261
262- def set_failure (self ):
263- # type: () -> None
264- self .set_tag ("status" , "failure" )
262+ def set_status (self , value ):
263+ # type: (str ) -> None
264+ self .set_tag ("status" , value )
265265
266- def set_success (self ):
267- # type: () -> None
268- self .set_tag ("status" , "success" )
266+ def set_http_status (self , http_status ):
267+ # type: (int) -> None
268+ self .set_tag ("http.status_code" , http_status )
269+
270+ if http_status < 400 :
271+ self .set_status ("ok" )
272+ elif 400 <= http_status < 500 :
273+ if http_status == 403 :
274+ self .set_status ("permission_denied" )
275+ elif http_status == 429 :
276+ self .set_status ("resource_exhausted" )
277+ elif http_status == 413 :
278+ self .set_status ("failed_precondition" )
279+ elif http_status == 401 :
280+ self .set_status ("unauthenticated" )
281+ elif http_status == 409 :
282+ self .set_status ("already_exists" )
283+ else :
284+ self .set_status ("invalid_argument" )
285+ elif 500 <= http_status < 600 :
286+ if http_status == 504 :
287+ self .set_status ("deadline_exceeded" )
288+ elif http_status == 501 :
289+ self .set_status ("unimplemented" )
290+ elif http_status == 503 :
291+ self .set_status ("unavailable" )
292+ else :
293+ self .set_status ("internal_error" )
294+ else :
295+ self .set_status ("unknown_error" )
269296
270297 def is_success (self ):
271298 # type: () -> bool
272- return self ._tags .get ("status" ) in ( None , "success" )
299+ return self ._tags .get ("status" ) == "ok"
273300
274301 def finish (self , hub = None ):
275302 # type: (Optional[sentry_sdk.Hub]) -> Optional[str]
@@ -315,6 +342,7 @@ def finish(self, hub=None):
315342 "type" : "transaction" ,
316343 "transaction" : self .transaction ,
317344 "contexts" : {"trace" : self .get_trace_context ()},
345+ "tags" : self ._tags ,
318346 "timestamp" : self .timestamp ,
319347 "start_timestamp" : self .start_timestamp ,
320348 "spans" : [
@@ -427,29 +455,13 @@ def record_sql_queries(
427455 yield span
428456
429457
430- @contextlib .contextmanager
431- def record_http_request (hub , url , method ):
432- # type: (sentry_sdk.Hub, str, str) -> Generator[Dict[str, str], None, None]
433- data_dict = {"url" : url , "method" : method }
434-
435- with hub .start_span (op = "http" , description = "%s %s" % (method , url )) as span :
436- try :
437- yield data_dict
438- finally :
439- if span is not None :
440- if "status_code" in data_dict :
441- span .set_tag ("http.status_code" , data_dict ["status_code" ])
442- for k , v in data_dict .items ():
443- span .set_data (k , v )
444-
445-
446458def _maybe_create_breadcrumbs_from_span (hub , span ):
447459 # type: (sentry_sdk.Hub, Span) -> None
448460 if span .op == "redis" :
449461 hub .add_breadcrumb (
450462 message = span .description , type = "redis" , category = "redis" , data = span ._tags
451463 )
452- elif span .op == "http" and span . is_success () :
464+ elif span .op == "http" :
453465 hub .add_breadcrumb (type = "http" , category = "httplib" , data = span ._data )
454466 elif span .op == "subprocess" :
455467 hub .add_breadcrumb (
0 commit comments