Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ def connect(self: "BaseDatabaseWrapper") -> None:

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return real_connect(self)
with sentry_sdk.traces.start_span(
name="connect",
attributes={
Expand All @@ -750,6 +752,8 @@ def _commit(self: "BaseDatabaseWrapper") -> None:

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return real_commit(self)
with sentry_sdk.traces.start_span(
name=SPANNAME.DB_COMMIT,
attributes={
Expand All @@ -776,6 +780,8 @@ def _rollback(self: "BaseDatabaseWrapper") -> None:

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return real_rollback(self)
with sentry_sdk.traces.start_span(
name=SPANNAME.DB_ROLLBACK,
attributes={
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ async def sentry_wrapped_callback(
return await callback(request, *args, **kwargs)

if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return await callback(request, *args, **kwargs)
with sentry_sdk.traces.start_span(
name=request.resolver_match.view_name,
attributes={
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def _instrument_call(

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return original_method(*args, **kwargs)
with sentry_sdk.traces.start_span(
name=description,
attributes={
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def _check_middleware_span(
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
middleware_span: "Union[Span, StreamedSpan]"
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return None
middleware_span = sentry_sdk.traces.start_span(
name=description,
attributes={
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/django/signals_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any":
sentry_sdk.get_client().options
)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return receiver(*args, **kwargs)
with sentry_sdk.traces.start_span(
name=signal_name,
attributes={
"sentry.op": OP.EVENT_DJANGO,
"sentry.origin": DjangoIntegration.origin,
SPANDATA.CODE_FUNCTION_NAME: signal_name,
},
) as span:
):
return receiver(*args, **kwargs)
else:
with sentry_sdk.start_span(
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def _sentry_enqueue(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return old_task_enqueue(self, *args, **kwargs)
with sentry_sdk.traces.start_span(
name=name,
attributes={
Expand Down
8 changes: 6 additions & 2 deletions sentry_sdk/integrations/django/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ def patch_templates() -> None:
def rendered_content(self: "SimpleTemplateResponse") -> str:
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return real_rendered_content.fget(self)
with sentry_sdk.traces.start_span(
name=_get_template_name_description(self.template_name),
attributes={
"sentry.op": OP.TEMPLATE_RENDER,
"sentry.origin": DjangoIntegration.origin,
},
) as span:
):
return real_rendered_content.fget(self)
else:
with sentry_sdk.start_span(
Expand Down Expand Up @@ -109,13 +111,15 @@ def render(
span_streaming = has_span_streaming_enabled(client.options)

if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return real_render(request, template_name, context, *args, **kwargs)
with sentry_sdk.traces.start_span(
name=_get_template_name_description(template_name),
attributes={
"sentry.op": OP.TEMPLATE_RENDER,
"sentry.origin": DjangoIntegration.origin,
},
) as span:
):
return real_render(request, template_name, context, *args, **kwargs)
else:
with sentry_sdk.start_span(
Expand Down
4 changes: 4 additions & 0 deletions sentry_sdk/integrations/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def patch_views() -> None:
def sentry_patched_render(self: "SimpleTemplateResponse") -> "Any":
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return old_render(self)
with sentry_sdk.traces.start_span(
name="serialize response",
attributes={
Expand Down Expand Up @@ -108,6 +110,8 @@ def sentry_wrapped_callback(request: "Any", *args: "Any", **kwargs: "Any") -> "A
return callback(request, *args, **kwargs)

if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return callback(request, *args, **kwargs)
with sentry_sdk.traces.start_span(
name=request.resolver_match.view_name,
attributes={
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ async def test_transaction_http_method_custom(
sentry_sdk.flush()
spans = [item.payload for item in items]

assert spans[10]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS"
assert spans[16]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD"
assert spans[5]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS"
assert spans[11]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD"
else:
events = capture_events()

Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2370,8 +2370,8 @@ def test_transaction_http_method_custom(
sentry_sdk.flush()
spans = [item.payload for item in items]

assert spans[4]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS"
assert spans[7]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD"
assert spans[2]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS"
assert spans[5]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD"
else:
events = capture_events()

Expand Down
Loading