Skip to content

Commit 3ef6cbb

Browse files
committed
Make sure to add data before span is closed. some cleanup
1 parent a962fab commit 3ef6cbb

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

sentry_sdk/integrations/sqlalchemy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
7676
context, "_sentry_sql_span_manager", None
7777
) # type: Optional[ContextManager[Any]]
7878

79-
if ctx_mgr is not None:
80-
context._sentry_sql_span_manager = None
81-
ctx_mgr.__exit__(None, None, None)
82-
8379
span = getattr(context, "_sentry_sql_span", None) # type: Optional[Span]
8480
if span is not None:
8581
with capture_internal_exceptions():
8682
add_query_source(span)
8783

84+
if ctx_mgr is not None:
85+
context._sentry_sql_span_manager = None
86+
ctx_mgr.__exit__(None, None, None)
87+
8888

8989
def _handle_error(context, *args):
9090
# type: (Any, *Any) -> None

sentry_sdk/tracing_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def record_sql_queries(
148148
op=OP.DB,
149149
name=query,
150150
origin=span_origin,
151+
only_if_parent=True,
151152
) as span:
152153
for k, v in data.items():
153154
span.set_data(k, v)

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from sqlalchemy import text
1212

1313
import sentry_sdk
14-
from sentry_sdk import capture_message, start_transaction
1514
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, SPANDATA
1615
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
1716
from sentry_sdk.serializer import MAX_EVENT_BYTES
@@ -54,7 +53,7 @@ class Address(Base):
5453

5554
assert session.query(Person).first() == bob
5655

57-
capture_message("hi")
56+
sentry_sdk.capture_message("hi")
5857

5958
(event,) = events
6059

@@ -111,7 +110,7 @@ class Address(Base):
111110
Session = sessionmaker(bind=engine) # noqa: N806
112111
session = Session()
113112

114-
with start_transaction(name="test_transaction", sampled=True):
113+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
115114
with session.begin_nested():
116115
session.query(Person).first()
117116

@@ -135,7 +134,7 @@ class Address(Base):
135134
assert (
136135
render_span_tree(event)
137136
== """\
138-
- op=null: description=null
137+
- op="test_transaction": description=null
139138
- op="db": description="SAVEPOINT sa_savepoint_1"
140139
- op="db": description="SELECT person.id AS person_id, person.name AS person_name \\nFROM person\\n LIMIT ? OFFSET ?"
141140
- op="db": description="RELEASE SAVEPOINT sa_savepoint_1"
@@ -185,7 +184,7 @@ class Address(Base):
185184
Session = sessionmaker(bind=engine) # noqa: N806
186185
session = Session()
187186

188-
with start_transaction(name="test_transaction", sampled=True):
187+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
189188
with session.begin_nested():
190189
session.query(Person).first()
191190

@@ -217,7 +216,7 @@ def test_long_sql_query_preserved(sentry_init, capture_events):
217216
engine = create_engine(
218217
"sqlite:///:memory:", connect_args={"check_same_thread": False}
219218
)
220-
with start_transaction(name="test"):
219+
with sentry_sdk.start_span(name="test"):
221220
with engine.connect() as con:
222221
con.execute(text(" UNION ".join("SELECT {}".format(i) for i in range(100))))
223222

@@ -246,7 +245,7 @@ def processor(event, hint):
246245
engine = create_engine(
247246
"sqlite:///:memory:", connect_args={"check_same_thread": False}
248247
)
249-
with start_transaction(name="test"):
248+
with sentry_sdk.start_span(name="test"):
250249
with engine.connect() as con:
251250
for _ in range(1500):
252251
con.execute(
@@ -306,7 +305,7 @@ def test_query_source_disabled(sentry_init, capture_events):
306305

307306
events = capture_events()
308307

309-
with start_transaction(name="test_transaction", sampled=True):
308+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
310309
Base = declarative_base() # noqa: N806
311310

312311
class Person(Base):
@@ -358,7 +357,7 @@ def test_query_source_enabled(sentry_init, capture_events, enable_db_query_sourc
358357

359358
events = capture_events()
360359

361-
with start_transaction(name="test_transaction", sampled=True):
360+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
362361
Base = declarative_base() # noqa: N806
363362

364363
class Person(Base):
@@ -405,7 +404,7 @@ def test_query_source(sentry_init, capture_events):
405404
)
406405
events = capture_events()
407406

408-
with start_transaction(name="test_transaction", sampled=True):
407+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
409408
Base = declarative_base() # noqa: N806
410409

411410
class Person(Base):
@@ -475,7 +474,7 @@ def test_query_source_with_module_in_search_path(sentry_init, capture_events):
475474
query_first_model_from_session,
476475
)
477476

478-
with start_transaction(name="test_transaction", sampled=True):
477+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
479478
Base = declarative_base() # noqa: N806
480479

481480
class Person(Base):
@@ -533,7 +532,7 @@ def test_no_query_source_if_duration_too_short(sentry_init, capture_events):
533532
)
534533
events = capture_events()
535534

536-
with start_transaction(name="test_transaction", sampled=True):
535+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
537536
Base = declarative_base() # noqa: N806
538537

539538
class Person(Base):
@@ -601,7 +600,7 @@ def test_query_source_if_duration_over_threshold(sentry_init, capture_events):
601600
)
602601
events = capture_events()
603602

604-
with start_transaction(name="test_transaction", sampled=True):
603+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
605604
Base = declarative_base() # noqa: N806
606605

607606
class Person(Base):
@@ -687,7 +686,7 @@ def test_span_origin(sentry_init, capture_events):
687686
engine = create_engine(
688687
"sqlite:///:memory:", connect_args={"check_same_thread": False}
689688
)
690-
with start_transaction(name="foo"):
689+
with sentry_sdk.start_span(name="foo"):
691690
with engine.connect() as con:
692691
con.execute(text("SELECT 0"))
693692

0 commit comments

Comments
 (0)