1
+ import contextlib
1
2
import os
2
3
from datetime import datetime
3
4
from unittest import mock
11
12
from sqlalchemy import text
12
13
13
14
import sentry_sdk
14
- from sentry_sdk import capture_message , start_transaction
15
15
from sentry_sdk .consts import DEFAULT_MAX_VALUE_LENGTH , SPANDATA
16
16
from sentry_sdk .integrations .sqlalchemy import SqlalchemyIntegration
17
17
from sentry_sdk .serializer import MAX_EVENT_BYTES
@@ -54,7 +54,7 @@ class Address(Base):
54
54
55
55
assert session .query (Person ).first () == bob
56
56
57
- capture_message ("hi" )
57
+ sentry_sdk . capture_message ("hi" )
58
58
59
59
(event ,) = events
60
60
@@ -111,7 +111,7 @@ class Address(Base):
111
111
Session = sessionmaker (bind = engine ) # noqa: N806
112
112
session = Session ()
113
113
114
- with start_transaction (name = "test_transaction" , sampled = True ):
114
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
115
115
with session .begin_nested ():
116
116
session .query (Person ).first ()
117
117
@@ -135,7 +135,7 @@ class Address(Base):
135
135
assert (
136
136
render_span_tree (event )
137
137
== """\
138
- - op=null : description=null
138
+ - op="test_transaction" : description=null
139
139
- op="db": description="SAVEPOINT sa_savepoint_1"
140
140
- op="db": description="SELECT person.id AS person_id, person.name AS person_name \\ nFROM person\\ n LIMIT ? OFFSET ?"
141
141
- op="db": description="RELEASE SAVEPOINT sa_savepoint_1"
@@ -185,7 +185,7 @@ class Address(Base):
185
185
Session = sessionmaker (bind = engine ) # noqa: N806
186
186
session = Session ()
187
187
188
- with start_transaction (name = "test_transaction" , sampled = True ):
188
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
189
189
with session .begin_nested ():
190
190
session .query (Person ).first ()
191
191
@@ -217,7 +217,7 @@ def test_long_sql_query_preserved(sentry_init, capture_events):
217
217
engine = create_engine (
218
218
"sqlite:///:memory:" , connect_args = {"check_same_thread" : False }
219
219
)
220
- with start_transaction (name = "test" ):
220
+ with sentry_sdk . start_span (name = "test" ):
221
221
with engine .connect () as con :
222
222
con .execute (text (" UNION " .join ("SELECT {}" .format (i ) for i in range (100 ))))
223
223
@@ -246,7 +246,7 @@ def processor(event, hint):
246
246
engine = create_engine (
247
247
"sqlite:///:memory:" , connect_args = {"check_same_thread" : False }
248
248
)
249
- with start_transaction (name = "test" ):
249
+ with sentry_sdk . start_span (name = "test" ):
250
250
with engine .connect () as con :
251
251
for _ in range (1500 ):
252
252
con .execute (
@@ -306,7 +306,7 @@ def test_query_source_disabled(sentry_init, capture_events):
306
306
307
307
events = capture_events ()
308
308
309
- with start_transaction (name = "test_transaction" , sampled = True ):
309
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
310
310
Base = declarative_base () # noqa: N806
311
311
312
312
class Person (Base ):
@@ -358,7 +358,7 @@ def test_query_source_enabled(sentry_init, capture_events, enable_db_query_sourc
358
358
359
359
events = capture_events ()
360
360
361
- with start_transaction (name = "test_transaction" , sampled = True ):
361
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
362
362
Base = declarative_base () # noqa: N806
363
363
364
364
class Person (Base ):
@@ -405,7 +405,7 @@ def test_query_source(sentry_init, capture_events):
405
405
)
406
406
events = capture_events ()
407
407
408
- with start_transaction (name = "test_transaction" , sampled = True ):
408
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
409
409
Base = declarative_base () # noqa: N806
410
410
411
411
class Person (Base ):
@@ -475,7 +475,7 @@ def test_query_source_with_module_in_search_path(sentry_init, capture_events):
475
475
query_first_model_from_session ,
476
476
)
477
477
478
- with start_transaction (name = "test_transaction" , sampled = True ):
478
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
479
479
Base = declarative_base () # noqa: N806
480
480
481
481
class Person (Base ):
@@ -533,7 +533,7 @@ def test_no_query_source_if_duration_too_short(sentry_init, capture_events):
533
533
)
534
534
events = capture_events ()
535
535
536
- with start_transaction (name = "test_transaction" , sampled = True ):
536
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
537
537
Base = declarative_base () # noqa: N806
538
538
539
539
class Person (Base ):
@@ -601,7 +601,7 @@ def test_query_source_if_duration_over_threshold(sentry_init, capture_events):
601
601
)
602
602
events = capture_events ()
603
603
604
- with start_transaction (name = "test_transaction" , sampled = True ):
604
+ with sentry_sdk . start_span (name = "test_transaction" , sampled = True ):
605
605
Base = declarative_base () # noqa: N806
606
606
607
607
class Person (Base ):
@@ -620,21 +620,15 @@ class Person(Base):
620
620
bob = Person (name = "Bob" )
621
621
session .add (bob )
622
622
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
630
630
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 ()
638
632
639
633
with mock .patch (
640
634
"sentry_sdk.integrations.sqlalchemy.record_sql_queries" ,
@@ -687,7 +681,7 @@ def test_span_origin(sentry_init, capture_events):
687
681
engine = create_engine (
688
682
"sqlite:///:memory:" , connect_args = {"check_same_thread" : False }
689
683
)
690
- with start_transaction (name = "foo" ):
684
+ with sentry_sdk . start_span (name = "foo" ):
691
685
with engine .connect () as con :
692
686
con .execute (text ("SELECT 0" ))
693
687
0 commit comments