8000 Fix potel tests in potel (#3841) · getsentry/sentry-python@8b70a66 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8b70a66

Browse files
authored
Fix potel tests in potel (#3841)
* Make sure to add data before span is closed. some cleanup * Fixed some tests
1 parent 2e23931 commit 8b70a66

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
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 10000 _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
@@ -147,6 +147,7 @@ def record_sql_queries(
147147
op=OP.DB,
148148
name=query,
149149
origin=span_origin,
150+
only_if_parent=True,
150151
) as span:
151152
for k, v in data.items():
152153
span.set_data(k, v)

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import os
23
from datetime import datetime
34
from unittest import mock
@@ -11,7 +12,6 @@
1112
from sqlalchemy import text
1213

1314
import sentry_sdk
14-
from sentry_sdk import capture_message, start_transaction
1515
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, SPANDATA
1616
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
1717
from sentry_sdk.serializer import MAX_EVENT_BYTES
@@ -54,7 +54,7 @@ class Address(Base):
5454

5555
assert session.query(Person).first() == bob
5656

57-
capture_message("hi")
57+
sentry_sdk.capture_message("hi")
5858

5959
(event,) = events
6060

@@ -111,7 +111,7 @@ class Address(Base):
111111
Session = sessionmaker(bind=engine) # noqa: N806
112112
session = Session()
113113

114-
with start_transaction(name="test_transaction", sampled=True):
114+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
115115
with session.begin_nested():
116116
session.query(Person).first()
117117

@@ -135,7 +135,7 @@ class Address(Base):
135135
assert (
136136
render_span_tree(event)
137137
== """\
138-
- op=null: description=null
138+
- op="test_transaction": description=null
139139
- op="db": description="SAVEPOINT sa_savepoint_1"
140140
- op="db": description="SELECT person.id AS person_id, person.name AS person_name \\nFROM person\\n LIMIT ? OFFSET ?"
141141
- op="db": description="RELEASE SAVEPOINT sa_savepoint_1"
@@ -185,7 +185,7 @@ class Address(Base):
185185
Session = sessionmaker(bind=engine) # noqa: N806
186186
session = Session()
187187

188-
with start_transaction(name="test_transaction", sampled=True):
188+
with sentry_sdk.start_span(name="test_transaction", sampled=True):
189189
with session.begin_nested():
190190
session.query(Person).first()
191191

@@ -217,7 +217,7 @@ def test_long_sql_query_preserved(sentry_init, capture_events):
217217
engine = create_engine(
218218
"sqlite:///:memory:", connect_args={"check_same_thread": False}
219219
)
220-
with start_transaction(name="test"):
220+
with sentry_sdk.start_span(name="test"):
221221
with engine.connect() as con:
222222
con.execute(text(" UNION ".join("SELECT {}".format(i) for i in range(100))))
223223

@@ -246,7 +246,7 @@ def processor(event, hint):
246246
engine = create_engine(
247247
"sqlite:///:memory:", connect_args={"check_same_thread": False}
248248
)
249-
with start_transaction(name="test"):
249+
with sentry_sdk.start_span(name="test"):
250250
with engine.connect() as con:
251251
for _ in range(1500):
252252
con.execute(
@@ -306,7 +306,7 @@ def test_query_source_disabled(sentry_init, capture_events):
306306

307307
events = capture_events()
308308

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

312312
class Person(Base):
@@ -358,7 +358,7 @@ def test_query_source_enabled(sentry_init, capture_events, enable_db_query_sourc
358358

359359
events = capture_events()
360360

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

364364
class Person(Base):
@@ -405,7 +405,7 @@ def test_query_source(sentry_init, capture_events):
405405
)
406406
events = capture_events()
407407

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

411411
class Person(Base):
@@ -475,7 +475,7 @@ def test_query_source_with_module_in_search_path(sentry_init, capture_events):
475475
query_first_model_from_session,
476476
)
477477

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

481481
class Person(Base):
@@ -533,7 +533,7 @@ def test_no_query_source_if_duration_too_short(sentry_init, capture_events):
533533
)
534534
events = capture_events()
535535

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

539539
class Person(Base):
@@ -601,7 +601,7 @@ def test_query_source_if_duration_over_threshold(sentry_init, capture_events):
601601
)
602602
events = capture_events()
603603

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

607607
class Person(Base):
@@ -620,21 +620,15 @@ class Person(Base):
620620
bob = Person(name="Bob")
621621
session.add(bob)
622622

623-
class fake_record_sql_queries: # noqa: N801
624-
def __init__(self, *args, **kwargs):
625-
with freeze_time(datetime(2024, 1, 1, microsecond=0)):
626-
with record_sql_queries(*args, **kwargs) as span:
627-
self.span = span
628-
freezer = freeze_time(datetime(2024, 1, 1, microsecond=99999))
629-
freezer.start()
623+
@contextlib.contextmanager
624+
def fake_record_sql_queries(*args, **kwargs): # noqa: N801
625+
with freeze_time(datetime(2024, 1, 1, second=0)):
626+
with record_sql_queries(*args, **kwargs) as span:
627+
freezer = freeze_time(datetime(2024, 1, 1, second=1))
628+
freezer.start()
629+
yield span
630630

631-
freezer.stop()
632-
633-
def __enter__(self):
634-
return self.span
635-
636-
def __exit__(self, type, value, traceback):
637-
pass
631+
freezer.stop()
638632

639633
with mock.patch(
640634
"sentry_sdk.integrations.sqlalchemy.record_sql_queries",
@@ -687,7 +681,7 @@ def test_span_origin(sentry_init, capture_events):
687681
engine = create_engine(
688682
"sqlite:///:memory:", connect_args={"check_same_thread": False}
689683
)
690-
with start_transaction(name="foo"):
684+
with sentry_sdk.start_span(name="foo"):
691685
with engine.connect() as con:
692686
con.execute(text("SELECT 0"))
693687

0 commit comments

Comments
 (0)
0