Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 662
feat(rq): Support span streaming#6493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2707b43c167118dd85278ec3319d0e8d44cc2bec97ee8c79e6b1ad310aebe930cf6c53943dec02e1a8c1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,15 +2,16 @@ | ||
| import sentry_sdk | ||
| from sentry_sdk.api import continue_trace | ||
| from sentry_sdk.consts import OP | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version | ||
| from sentry_sdk.integrations.logging import ignore_logger | ||
| from sentry_sdk.scope import should_send_default_pii | ||
| from sentry_sdk.scope import Scope, should_send_default_pii | ||
| from sentry_sdk.traces import SegmentSource | ||
| from sentry_sdk.tracing import TransactionSource | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
| from sentry_sdk.utils import ( | ||
| SENSITIVE_DATA_SUBSTITUTE, | ||
| capture_internal_exceptions, | ||
| ensure_integration_enabled, | ||
| event_from_exception, | ||
| format_timestamp, | ||
| parse_version, | ||
| @@ -61,30 +62,59 @@ def setup_once() -> None: | ||
| old_perform_job = worker_cls.perform_job | ||
| @ensure_integration_enabled(RqIntegration, old_perform_job) | ||
| def sentry_patched_perform_job( | ||
| self: "Any", job: "Job", *args: "Queue", **kwargs: "Any" | ||
| ) -> bool: | ||
| client = sentry_sdk.get_client() | ||
| if client.get_integration(RqIntegration) is None: | ||
| return old_perform_job(self, job, *args, **kwargs) | ||
| with sentry_sdk.new_scope() as scope: | ||
| scope.clear_breadcrumbs() | ||
| scope.add_event_processor(_make_event_processor(weakref.ref(job))) | ||
| transaction = continue_trace( | ||
| job.meta.get("_sentry_trace_headers") or {}, | ||
| op=OP.QUEUE_TASK_RQ, | ||
| name="unknown RQ task", | ||
| source=TransactionSource.TASK, | ||
| origin=RqIntegration.origin, | ||
| ) | ||
| with capture_internal_exceptions(): | ||
| transaction.name = job.func_name | ||
| with sentry_sdk.start_transaction( | ||
| transaction, | ||
| custom_sampling_context={"rq_job": job}, | ||
| ): | ||
| rv = old_perform_job(self, job, *args, **kwargs) | ||
| if has_span_streaming_enabled(client.options): | ||
| sentry_sdk.traces.continue_trace( | ||
| job.meta.get("_sentry_trace_headers") or {} | ||
| ) | ||
| Scope.set_custom_sampling_context({"rq_job": job}) | ||
| func_name = None | ||
| with capture_internal_exceptions(): | ||
| func_name = job.func_name | ||
| with sentry_sdk.traces.start_span( | ||
| name="unknown RQ task" if func_name is None else func_name, | ||
| attributes={ | ||
| "sentry.op": OP.QUEUE_TASK_RQ, | ||
| "sentry.origin": RqIntegration.origin, | ||
| "sentry.span.source": SegmentSource.TASK, | ||
| SPANDATA.MESSAGING_MESSAGE_ID: job.id, | ||
| }, | ||
| parent_span=None, | ||
| ) as span: | ||
| if func_name is not None: | ||
| span.set_attribute(SPANDATA.CODE_FUNCTION_NAME, func_name) | ||
| rv = old_perform_job(self, job, *args, **kwargs) | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| else: | ||
| transaction = continue_trace( | ||
| job.meta.get("_sentry_trace_headers") or {}, | ||
| op=OP.QUEUE_TASK_RQ, | ||
| name="unknown RQ task", | ||
| source=TransactionSource.TASK, | ||
| origin=RqIntegration.origin, | ||
| ) | ||
| with capture_internal_exceptions(): | ||
| transaction.name = job.func_name | ||
| with sentry_sdk.start_transaction( | ||
| transaction, | ||
| custom_sampling_context={"rq_job": job}, | ||
| ): | ||
| rv = old_perform_job(self, job, *args, **kwargs) | ||
| if self.is_horse: | ||
| # We're inside of a forked process and RQ is | ||
| @@ -116,12 +146,20 @@ def sentry_patched_handle_exception( | ||
| old_enqueue_job = Queue.enqueue_job | ||
| @ensure_integration_enabled(RqIntegration, old_enqueue_job) | ||
| def sentry_patched_enqueue_job( | ||
| self: "Queue", job: "Any", **kwargs: "Any" | ||
| ) -> "Any": | ||
| client = sentry_sdk.get_client() | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if client.get_integration(RqIntegration) is None: | ||
| return old_enqueue_job(self, job, **kwargs) | ||
| scope = sentry_sdk.get_current_scope() | ||
| if scope.span is not None: | ||
| span = ( | ||
| scope.streamed_span | ||
| if has_span_streaming_enabled(client.options) | ||
| else scope.span | ||
| ) | ||
| if span is not None: | ||
| job.meta["_sentry_trace_headers"] = dict( | ||
| scope.iter_trace_propagation_headers() | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.