Skip to content

Commit 5520bdb

Browse files
authored
Fix DSC in celery tasks started by Celery Beat. (#3047)
Only create the span for enqueuing the task when we are not currently running in a celery beat task. (because then there is only the span without a transaction and thus the baggage header can not be given to the child celery task.) Without the span the child celery task creates its own trace, this is what we want.
1 parent 0f0cde7 commit 5520bdb

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import boto3
2-
import sentry_sdk
2+
import sentry_sdk
33

44

55
monitor_slug = "python-sdk-aws-lambda-tests-cleanup"
66
monitor_config = {
77
"schedule": {
88
"type": "crontab",
9-
"value": "0 12 * * 0", # 12 o'clock on Sunday
9+
"value": "0 12 * * 0", # 12 o'clock on Sunday
1010
},
1111
"timezone": "UTC",
1212
"checkin_margin": 2,
@@ -24,7 +24,7 @@ def delete_lambda_functions(prefix="test_"):
2424
"""
2525
client = boto3.client("lambda", region_name="us-east-1")
2626
functions_deleted = 0
27-
27+
2828
functions_paginator = client.get_paginator("list_functions")
2929
for functions_page in functions_paginator.paginate():
3030
for func in functions_page["Functions"]:
@@ -39,17 +39,17 @@ def delete_lambda_functions(prefix="test_"):
3939
print(f"Got exception: {ex}")
4040

4141
return functions_deleted
42-
42+
4343

4444
def lambda_handler(event, context):
4545
functions_deleted = delete_lambda_functions()
46-
46+
4747
sentry_sdk.metrics.gauge(
48-
key="num_aws_functions_deleted",
48+
key="num_aws_functions_deleted",
4949
value=functions_deleted,
5050
)
51-
51+
5252
return {
53-
'statusCode': 200,
54-
'body': f"{functions_deleted} AWS Lambda functions deleted successfully."
53+
"statusCode": 200,
54+
"body": f"{functions_deleted} AWS Lambda functions deleted successfully.",
5555
}

sentry_sdk/integrations/celery/__init__.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from typing import List
3131
from typing import Optional
3232
from typing import TypeVar
33+
from typing import Union
3334

3435
from sentry_sdk._types import EventProcessor, Event, Hint, ExcInfo
3536
from sentry_sdk.tracing import Span
@@ -223,6 +224,16 @@ def _update_celery_task_headers(original_headers, span, monitor_beat_tasks):
223224
return updated_headers
224225

225226

227+
class NoOpMgr:
228+
def __enter__(self):
229+
# type: () -> None
230+
return None
231+
232+
def __exit__(self, exc_type, exc_value, traceback):
233+
# type: (Any, Any, Any) -> None
234+
return None
235+
236+
226237
def _wrap_apply_async(f):
227238
# type: (F) -> F
228239
@wraps(f)
@@ -242,9 +253,17 @@ def apply_async(*args, **kwargs):
242253

243254
task = args[0]
244255

245-
with sentry_sdk.start_span(
246-
op=OP.QUEUE_SUBMIT_CELERY, description=task.name
247-
) as span:
256+
task_started_from_beat = (
257+
sentry_sdk.Scope.get_isolation_scope()._name == "celery-beat"
258+
)
259+
260+
span_mgr = (
261+
sentry_sdk.start_span(op=OP.QUEUE_SUBMIT_CELERY, description=task.name)
262+
if not task_started_from_beat
263+
else NoOpMgr()
264+
) # type: Union[Span, NoOpMgr]
265+
266+
with span_mgr as span:
248267
kwargs["headers"] = _update_celery_task_headers(
249268
kwarg_headers, span, integration.monitor_beat_tasks
250269
)

sentry_sdk/tracing_utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ def update(self, other_dict):
421421
except AttributeError:
422422
pass
423423

424+
def __repr__(self):
425+
# type: (...) -> str
426+
return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} dynamic_sampling_context={}>".format(
427+
self._trace_id,
428+
self._span_id,
429+
self.parent_span_id,
430+
self.parent_sampled,
431+
self.dynamic_sampling_context,
432+
)
433+
424434

425435
class Baggage:
426436
"""

0 commit comments

Comments
 (0)