|
2 | 2 |
|
3 | 3 | import sentry_sdk |
4 | 4 | from sentry_sdk.api import continue_trace |
5 | | -from sentry_sdk.consts import OP |
| 5 | +from sentry_sdk.consts import OP, SPANDATA |
6 | 6 | from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version |
7 | 7 | from sentry_sdk.integrations.logging import ignore_logger |
8 | | -from sentry_sdk.scope import should_send_default_pii |
| 8 | +from sentry_sdk.scope import Scope, should_send_default_pii |
| 9 | +from sentry_sdk.traces import SegmentSource |
9 | 10 | from sentry_sdk.tracing import TransactionSource |
| 11 | +from sentry_sdk.tracing_utils import has_span_streaming_enabled |
10 | 12 | from sentry_sdk.utils import ( |
11 | 13 | SENSITIVE_DATA_SUBSTITUTE, |
12 | 14 | capture_internal_exceptions, |
13 | | - ensure_integration_enabled, |
14 | 15 | event_from_exception, |
15 | 16 | format_timestamp, |
16 | 17 | parse_version, |
@@ -61,30 +62,59 @@ def setup_once() -> None: |
61 | 62 |
|
62 | 63 | old_perform_job = worker_cls.perform_job |
63 | 64 |
|
64 | | - @ensure_integration_enabled(RqIntegration, old_perform_job) |
65 | 65 | def sentry_patched_perform_job( |
66 | 66 | self: "Any", job: "Job", *args: "Queue", **kwargs: "Any" |
67 | 67 | ) -> bool: |
| 68 | + client = sentry_sdk.get_client() |
| 69 | + if client.get_integration(RqIntegration) is None: |
| 70 | + return old_perform_job(self, job, *args, **kwargs) |
| 71 | + |
68 | 72 | with sentry_sdk.new_scope() as scope: |
69 | 73 | scope.clear_breadcrumbs() |
70 | 74 | scope.add_event_processor(_make_event_processor(weakref.ref(job))) |
71 | 75 |
|
72 | | - transaction = continue_trace( |
73 | | - job.meta.get("_sentry_trace_headers") or {}, |
74 | | - op=OP.QUEUE_TASK_RQ, |
75 | | - name="unknown RQ task", |
76 | | - source=TransactionSource.TASK, |
77 | | - origin=RqIntegration.origin, |
78 | | - ) |
79 | | - |
80 | | - with capture_internal_exceptions(): |
81 | | - transaction.name = job.func_name |
82 | | - |
83 | | - with sentry_sdk.start_transaction( |
84 | | - transaction, |
85 | | - custom_sampling_context={"rq_job": job}, |
86 | | - ): |
87 | | - rv = old_perform_job(self, job, *args, **kwargs) |
| 76 | + if has_span_streaming_enabled(client.options): |
| 77 | + sentry_sdk.traces.continue_trace( |
| 78 | + job.meta.get("_sentry_trace_headers") or {} |
| 79 | + ) |
| 80 | + |
| 81 | + Scope.set_custom_sampling_context({"rq_job": job}) |
| 82 | + |
| 83 | + func_name = None |
| 84 | + with capture_internal_exceptions(): |
| 85 | + func_name = job.func_name |
| 86 | + |
| 87 | + with sentry_sdk.traces.start_span( |
| 88 | + name="unknown RQ task" if func_name is None else func_name, |
| 89 | + attributes={ |
| 90 | + "sentry.op": OP.QUEUE_TASK_RQ, |
| 91 | + "sentry.origin": RqIntegration.origin, |
| 92 | + "sentry.span.source": SegmentSource.TASK, |
| 93 | + SPANDATA.MESSAGING_MESSAGE_ID: job.id, |
| 94 | + }, |
| 95 | + parent_span=None, |
| 96 | + ) as span: |
| 97 | + if func_name is not None: |
| 98 | + span.set_attribute(SPANDATA.CODE_FUNCTION_NAME, func_name) |
| 99 | + |
| 100 | + rv = old_perform_job(self, job, *args, **kwargs) |
| 101 | + else: |
| 102 | + transaction = continue_trace( |
| 103 | + job.meta.get("_sentry_trace_headers") or {}, |
| 104 | + op=OP.QUEUE_TASK_RQ, |
| 105 | + name="unknown RQ task", |
| 106 | + source=TransactionSource.TASK, |
| 107 | + origin=RqIntegration.origin, |
| 108 | + ) |
| 109 | + |
| 110 | + with capture_internal_exceptions(): |
| 111 | + transaction.name = job.func_name |
| 112 | + |
| 113 | + with sentry_sdk.start_transaction( |
| 114 | + transaction, |
| 115 | + custom_sampling_context={"rq_job": job}, |
| 116 | + ): |
| 117 | + rv = old_perform_job(self, job, *args, **kwargs) |
88 | 118 |
|
89 | 119 | if self.is_horse: |
90 | 120 | # We're inside of a forked process and RQ is |
@@ -116,12 +146,20 @@ def sentry_patched_handle_exception( |
116 | 146 |
|
117 | 147 | old_enqueue_job = Queue.enqueue_job |
118 | 148 |
|
119 | | - @ensure_integration_enabled(RqIntegration, old_enqueue_job) |
120 | 149 | def sentry_patched_enqueue_job( |
121 | 150 | self: "Queue", job: "Any", **kwargs: "Any" |
122 | 151 | ) -> "Any": |
| 152 | + client = sentry_sdk.get_client() |
| 153 | + if client.get_integration(RqIntegration) is None: |
| 154 | + return old_enqueue_job(self, job, **kwargs) |
| 155 | + |
123 | 156 | scope = sentry_sdk.get_current_scope() |
124 | | - if scope.span is not None: |
| 157 | + span = ( |
| 158 | + scope.streamed_span |
| 159 | + if has_span_streaming_enabled(client.options) |
| 160 | + else scope.span |
| 161 | + ) |
| 162 | + if span is not None: |
125 | 163 | job.meta["_sentry_trace_headers"] = dict( |
126 | 164 | scope.iter_trace_propagation_headers() |
127 | 165 | ) |
|
0 commit comments