|
| 1 | +import logging |
| 2 | + |
| 3 | +from ddtrace import Pin |
| 4 | + |
| 5 | +from celery import registry |
| 6 | + |
| 7 | +from . import constants as c |
| 8 | +from .utils import ( |
| 9 | + tags_from_context, |
| 10 | + retrieve_task_id, |
| 11 | + attach_span, |
| 12 | + detach_span, |
| 13 | + retrieve_span, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def trace_prerun(*args, **kwargs): |
| 21 | + # safe-guard to avoid crashes in case the signals API |
| 22 | + # changes in Celery |
| 23 | + task = kwargs.get('sender') |
| 24 | + task_id = kwargs.get('task_id') |
| 25 | + if task is None or task_id is None: |
| 26 | + log.debug('unable to extract the Task and the task_id. This version of Celery may not be supported.') |
| 27 | + return |
| 28 | + |
| 29 | + # retrieve the task Pin or fallback to the global one |
| 30 | + pin = Pin.get_from(task) or Pin.get_from(task.app) |
| 31 | + if pin is None: |
| 32 | + return |
| 33 | + |
| 34 | + # propagate the `Span` in the current task Context |
| 35 | + span = pin.tracer.trace(c.WORKER_ROOT_SPAN, service=c.WORKER_SERVICE, resource=task.name) |
| 36 | + attach_span(task, task_id, span) |
| 37 | + |
| 38 | + |
| 39 | +def trace_postrun(*args, **kwargs): |
| 40 | + # safe-guard to avoid crashes in case the signals API |
| 41 | + # changes in Celery |
| 42 | + task = kwargs.get('sender') |
| 43 | + task_id = kwargs.get('task_id') |
| 44 | + if task is None or task_id is None: |
| 45 | + log.debug('unable to extract the Task and the task_id. This version of Celery may not be supported.') |
| 46 | + return |
| 47 | + |
| 48 | + # retrieve and finish the Span |
| 49 | + span = retrieve_span(task, task_id) |
| 50 | + if span is None: |
| 51 | + return |
| 52 | + else: |
| 53 | + # request context tags |
| 54 | + span.set_tag(c.TASK_TAG_KEY, c.TASK_RUN) |
| 55 | + span.set_tags(tags_from_context(kwargs)) |
| 56 | + span.set_tags(tags_from_context(task.request)) |
| 57 | + span.finish() |
| 58 | + detach_span(task, task_id) |
| 59 | + |
| 60 | + |
| 61 | +def trace_before_publish(*args, **kwargs): |
| 62 | + # `before_task_publish` signal doesn't propagate the task instance so |
| 63 | + # we need to retrieve it from the Celery Registry to access the `Pin`. The |
| 64 | + # `Task` instance **does not** include any information about the current |
| 65 | + # execution, so it **must not** be used to retrieve `request` data. |
| 66 | + task_name = kwargs.get('sender') |
| 67 | + task = registry.tasks.get(task_name) |
| 68 | + task_id = retrieve_task_id(kwargs) |
| 69 | + # safe-guard to avoid crashes in case the signals API |
| 70 | + # changes in Celery |
| 71 | + if task is None or task_id is None: |
| 72 | + log.debug('unable to extract the Task and the task_id. This version of Celery may not be supported.') |
| 73 | + return |
| 74 | + |
| 75 | + # propagate the `Span` in the current task Context |
| 76 | + pin = Pin.get_from(task) or Pin.get_from(task.app) |
| 77 | + if pin is None: |
| 78 | + return |
| 79 | + |
| 80 | + # apply some tags here because most of the data is not available |
| 81 | + # in the task_after_publish signal |
| 82 | + span = pin.tracer.trace(c.PRODUCER_ROOT_SPAN, service=c.PRODUCER_SERVICE, resource=task_name) |
| 83 | + span.set_tag(c.TASK_TAG_KEY, c.TASK_APPLY_ASYNC) |
| 84 | + span.set_tag('celery.id', task_id) |
| 85 | + span.set_tags(tags_from_context(kwargs)) |
| 86 | + # Note: adding tags from `traceback` or `state` calls will make an |
| 87 | + # API call to the backend for the properties so we should rely |
| 88 | + # only on the given `Context` |
| 89 | + attach_span(task, task_id, span) |
| 90 | + |
| 91 | + |
| 92 | +def trace_after_publish(*args, **kwargs): |
| 93 | + task_name = kwargs.get('sender') |
| 94 | + task = registry.tasks.get(task_name) |
| 95 | + task_id = retrieve_task_id(kwargs) |
| 96 | + # safe-guard to avoid crashes in case the signals API |
| 97 | + # changes in Celery |
| 98 | + if task is None or task_id is None: |
| 99 | + log.debug('unable to extract the Task and the task_id. This version of Celery may not be supported.') |
| 100 | + return |
| 101 | + |
| 102 | + # retrieve and finish the Span |
| 103 | + span = retrieve_span(task, task_id) |
| 104 | + if span is None: |
| 105 | + return |
| 106 | + else: |
| 107 | + span.finish() |
| 108 | + detach_span(task, task_id) |
| 109 | + |
| 110 | + |
| 111 | +def trace_failure(*args, **kwargs): |
| 112 | + # safe-guard to avoid crashes in case the signals API |
| 113 | + # changes in Celery |
| 114 | + task = kwargs.get('sender') |
| 115 | + task_id = kwargs.get('task_id') |
| 116 | + if task is None or task_id is None: |
| 117 | + log.debug('unable to extract the Task and the task_id. This version of Celery may not be supported.') |
| 118 | + return |
| 119 | + |
| 120 | + # retrieve and finish the Span |
| 121 | + span = retrieve_span(task, task_id) |
| 122 | + if span is None: |
| 123 | + return |
| 124 | + else: |
| 125 | + # add Exception tags; post signals are still called |
| 126 | + # so we don't need to attach other tags here |
| 127 | + ex = kwargs.get('einfo') |
| 128 | + if ex is None: |
| 129 | + return |
| 130 | + span.set_exc_info(ex.type, ex.exception, ex.tb) |
0 commit comments