Skip to content

Commit 2c22c16

Browse files
move query source before exit
1 parent 56ee084 commit 2c22c16

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

sentry_sdk/integrations/sqlalchemy.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ def _after_cursor_execute(
8080
context, "_sentry_sql_span_manager", None
8181
)
8282

83-
if ctx_mgr is not None:
84-
context._sentry_sql_span_manager = None
85-
ctx_mgr.__exit__(None, None, None)
86-
83+
# Record query source immediately before span is finished: accurate end timestamp and before the span is flushed.
8784
span: "Optional[Span]" = getattr(context, "_sentry_sql_span", None)
8885
if span is not None:
8986
with capture_internal_exceptions():
9087
add_query_source(span)
9188

89+
if ctx_mgr is not None:
90+
context._sentry_sql_span_manager = None
91+
ctx_mgr.__exit__(None, None, None)
92+
9293

9394
def _handle_error(context: "Any", *args: "Any") -> None:
9495
execution_context = context.execution_context

0 commit comments

Comments
 (0)