@@ -173,7 +173,7 @@ def __enter__(self):
173
173
def __exit__ (self , ty , value , tb ):
174
174
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
175
175
if value is not None :
176
- self .set_failure ( )
176
+ self ._tags . setdefault ( "status" , "internal_error" )
177
177
178
178
hub , scope , old_span = self ._context_manager_state
179
179
del self ._context_manager_state
@@ -259,17 +259,44 @@ def set_data(self, key, value):
259
259
# type: (str, Any) -> None
260
260
self ._data [key ] = value
261
261
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 )
265
265
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" )
269
296
270
297
def is_success (self ):
271
298
# type: () -> bool
272
- return self ._tags .get ("status" ) in ( None , "success" )
299
+ return self ._tags .get ("status" ) == "ok"
273
300
274
301
def finish (self , hub = None ):
275
302
# type: (Optional[sentry_sdk.Hub]) -> Optional[str]
@@ -315,6 +342,7 @@ def finish(self, hub=None):
315
342
"type" : "transaction" ,
316
343
"transaction" : self .transaction ,
317
344
"contexts" : {"trace" : self .get_trace_context ()},
345
+ "tags" : self ._tags ,
318
346
"timestamp" : self .timestamp ,
319
347
"start_timestamp" : self .start_timestamp ,
320
348
"spans" : [
@@ -427,29 +455,13 @@ def record_sql_queries(
427
455
yield span
428
456
429
457
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
-
446
458
def _maybe_create_breadcrumbs_from_span (hub , span ):
447
459
# type: (sentry_sdk.Hub, Span) -> None
448
460
if span .op == "redis" :
449
461
hub .add_breadcrumb (
450
462
message = span .description , type = "redis" , category = "redis" , data = span ._tags
451
463
)
452
- elif span .op == "http" and span . is_success () :
464
+ elif span .op == "http" :
453
465
hub .add_breadcrumb (type = "http" , category = "httplib" , data = span ._data )
454
466
elif span .op == "subprocess" :
455
467
hub .add_breadcrumb (
0 commit comments