Skip to content

Commit 1610301

Browse files
committed
Cleanup
1 parent 1ed7175 commit 1610301

File tree

1 file changed

+41
-67
lines changed

1 file changed

+41
-67
lines changed

sentry_sdk/integrations/celery/__init__.py

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,10 @@ def apply_async(*args, **kwargs):
283283
) # type: Union[Span, NoOpMgr]
284284

285285
with span_mgr as span:
286-
try:
287-
kwargs["headers"] = _update_celery_task_headers(
288-
kwarg_headers, span, integration.monitor_beat_tasks
289-
)
290-
return_value = f(*args, **kwargs)
291-
292-
except Exception:
293-
reraise(*sys.exc_info())
294-
else:
295-
span.set_status(SPANSTATUS.OK)
296-
297-
return return_value
286+
kwargs["headers"] = _update_celery_task_headers(
287+
kwarg_headers, span, integration.monitor_beat_tasks
288+
)
289+
return f(*args, **kwargs)
298290

299291
return apply_async # type: ignore
300292

@@ -322,30 +314,22 @@ def _inner(*args, **kwargs):
322314
# something such as attribute access can fail.
323315
headers = args[3].get("headers") or {}
324316
with sentry_sdk.continue_trace(headers):
325-
try:
326-
with sentry_sdk.start_span(
327-
op=OP.QUEUE_TASK_CELERY,
328-
name=task.name,
329-
source=TRANSACTION_SOURCE_TASK,
330-
origin=CeleryIntegration.origin,
331-
custom_sampling_context={
332-
"celery_job": {
333-
"task": task.name,
334-
# for some reason, args[1] is a list if non-empty but a
335-
# tuple if empty
336-
"args": list(args[1]),
337-
"kwargs": args[2],
338-
}
339-
},
340-
) as span:
341-
return_value = f(*args, **kwargs)
342-
343-
except Exception:
344-
reraise(*sys.exc_info())
345-
else:
346-
span.set_status(SPANSTATUS.OK)
347-
348-
return return_value
317+
with sentry_sdk.start_span(
318+
op=OP.QUEUE_TASK_CELERY,
319+
name=task.name,
320+
source=TRANSACTION_SOURCE_TASK,
321+
origin=CeleryIntegration.origin,
322+
custom_sampling_context={
323+
"celery_job": {
324+
"task": task.name,
325+
# for some reason, args[1] is a list if non-empty but a
326+
# tuple if empty
327+
"args": list(args[1]),
328+
"kwargs": args[2],
329+
}
330+
},
331+
):
332+
return f(*args, **kwargs)
349333

350334
return _inner # type: ignore
351335

@@ -413,17 +397,13 @@ def _inner(*args, **kwargs):
413397
task.app.connection().transport.driver_type,
414398
)
415399

416-
result = f(*args, **kwargs)
400+
return f(*args, **kwargs)
417401

418402
except Exception:
419403
exc_info = sys.exc_info()
420404
with capture_internal_exceptions():
421405
_capture_exception(task, exc_info)
422406
reraise(*exc_info)
423-
else:
424-
span.set_status(SPANSTATUS.OK)
425-
426-
return result
427407

428408
return _inner # type: ignore
429409

@@ -513,35 +493,29 @@ def sentry_publish(self, *args, **kwargs):
513493
routing_key = kwargs.get("routing_key")
514494
exchange = kwargs.get("exchange")
515495

516-
try:
517-
with sentry_sdk.start_span(
518-
op=OP.QUEUE_PUBLISH,
519-
name=task_name,
520-
origin=CeleryIntegration.origin,
521-
only_if_parent=True,
522-
) as span:
523-
if task_id is not None:
524-
span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task_id)
525-
526-
if exchange == "" and routing_key is not None:
527-
# Empty exchange indicates the default exchange, meaning messages are
528-
# routed to the queue with the same name as the routing key.
529-
span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key)
530-
531-
if retries is not None:
532-
span.set_data(SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, retries)
496+
with sentry_sdk.start_span(
497+
op=OP.QUEUE_PUBLISH,
498+
name=task_name,
499+
origin=CeleryIntegration.origin,
500+
only_if_parent=True,
501+
) as span:
502+
if task_id is not None:
503+
span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task_id)
504+
505+
if exchange == "" and routing_key is not None:
506+
# Empty exchange indicates the default exchange, meaning messages are
507+
# routed to the queue with the same name as the routing key.
508+
span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key)
533509

534-
with capture_internal_exceptions():
535-
span.set_data(
536-
SPANDATA.MESSAGING_SYSTEM, self.connection.transport.driver_type
537-
)
510+
if retries is not None:
511+
span.set_data(SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, retries)
538512

539-
return_value = original_publish(self, *args, **kwargs)
513+
with capture_internal_exceptions():
514+
span.set_data(
515+
SPANDATA.MESSAGING_SYSTEM, self.connection.transport.driver_type
516+
)
540517

541-
except Exception:
542-
reraise(*sys.exc_info())
543-
else:
544-
span.set_status(SPANSTATUS.OK)
518+
return_value = original_publish(self, *args, **kwargs)
545519

546520
return return_value
547521

0 commit comments

Comments
 (0)