-
Notifications
You must be signed in to change notification settings - Fork 565
feat(integrations): Add tracing to DramatiqIntegration #4571
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
Changes from 10 commits
edee89d
5562a9c
863d16b
5674a62
9764f19
7156ac4
39e3a50
a41b477
4746db6
a5e9ad0
1357bea
39d3532
db239dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,31 @@ | ||||||
import json | ||||||
|
||||||
import sentry_sdk | ||||||
from sentry_sdk.integrations import Integration | ||||||
from sentry_sdk.consts import OP, SPANSTATUS | ||||||
from sentry_sdk.api import continue_trace, get_baggage, get_traceparent | ||||||
from sentry_sdk.integrations import Integration, DidNotEnable | ||||||
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds | ||||||
from sentry_sdk.tracing import ( | ||||||
BAGGAGE_HEADER_NAME, | ||||||
SENTRY_TRACE_HEADER_NAME, | ||||||
TransactionSource, | ||||||
) | ||||||
from sentry_sdk.utils import ( | ||||||
AnnotatedValue, | ||||||
capture_internal_exceptions, | ||||||
event_from_exception, | ||||||
) | ||||||
from typing import TypeVar | ||||||
|
||||||
R = TypeVar("R") | ||||||
|
||||||
from dramatiq.broker import Broker # type: ignore | ||||||
from dramatiq.message import Message # type: ignore | ||||||
from dramatiq.middleware import Middleware, default_middleware # type: ignore | ||||||
from dramatiq.errors import Retry # type: ignore | ||||||
try: | ||||||
from dramatiq.broker import Broker | ||||||
from dramatiq.middleware import Middleware, default_middleware | ||||||
from dramatiq.errors import Retry | ||||||
from dramatiq.message import Message | ||||||
except ImportError: | ||||||
raise DidNotEnable("Dramatiq is not installed") | ||||||
|
||||||
from typing import TYPE_CHECKING | ||||||
|
||||||
|
@@ -34,10 +47,12 @@ class DramatiqIntegration(Integration): | |||||
""" | ||||||
|
||||||
identifier = "dramatiq" | ||||||
origin = f"auto.queue.{identifier}" | ||||||
|
||||||
@staticmethod | ||||||
def setup_once(): | ||||||
# type: () -> None | ||||||
|
||||||
_patch_dramatiq_broker() | ||||||
|
||||||
|
||||||
|
@@ -85,50 +100,79 @@ class SentryMiddleware(Middleware): # type: ignore[misc] | |||||
DramatiqIntegration. | ||||||
""" | ||||||
|
||||||
def before_enqueue(self, broker, message, delay): | ||||||
# type: (Broker, Message[R], int) -> None | ||||||
message.options["sentry_headers"] = { | ||||||
BAGGAGE_HEADER_NAME: get_baggage(), | ||||||
SENTRY_TRACE_HEADER_NAME: get_traceparent(), | ||||||
} | ||||||
|
||||||
def before_process_message(self, broker, message): | ||||||
# type: (Broker, Message) -> None | ||||||
# type: (Broker, Message[R]) -> None | ||||||
integration = sentry_sdk.get_client().get_integration(DramatiqIntegration) | ||||||
if integration is None: | ||||||
return | ||||||
|
||||||
message._scope_manager = sentry_sdk.new_scope() | ||||||
message._scope_manager.__enter__() | ||||||
Comment on lines
-94
to
-95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still need some sort of scope management in order to make sure the data we collect about tasks is isolated. The general rule of thumb is: if you start a transaction, you should start it in a new isolation scope. See for example huey. So we should start an isolation scope right after the initial scope = sentry_sdk.isolation_scope()
message._scope_manager = scope
scope.__enter__() Everything that we do on the And finally, we need to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
|
||||||
scope = sentry_sdk.get_current_scope() | ||||||
scope.set_transaction_name(message.actor_name) | ||||||
scope.set_extra("dramatiq_message_id", message.message_id) | ||||||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
scope.clear_breadcrumbs() | ||||||
scope.add_event_processor(_make_message_event_processor(message, integration)) | ||||||
|
||||||
sentry_headers = message.options.get("sentry_headers") or {} | ||||||
|
sentry_headers = message.options.get("sentry_headers") or {} | |
sentry_headers = message.options.get("_sentry_headers") or {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Transaction Initialization Redundancy
The transaction initialization in before_process_message
uses an incorrect pattern. It creates a transaction with continue_trace()
, then passes this existing transaction object to sentry_sdk.start_transaction()
, which is designed to create new transactions. This leads to redundant initialization, a manual transaction.__enter__()
call, and causes the origin
parameter set by continue_trace()
to be lost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep this -- it's a fallback in case the integration/the SDK/the client has been deactivated in the meantime. In that case we shouldn't do anything.
We should have this also in the new before_enqueue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, but I believe it's unnecessary due to this: https://github.com/getsentry/sentry-python/pull/4571/files#diff-2722e3fe31f13ffc24072313765f1fc89f0f0721154b7ca072bb46b1f9573f5bR84
Middleware won't be in use if integration is not enabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I don't think we're really in risk of collisions here, but let's maybe call this
_sentry_headers
just to denote it's something internalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed