1212import datetime
1313import os
1414from contextlib import contextmanager
15+ from datetime import timezone
1516from unittest import mock
1617
1718import asyncpg
2021from asyncpg import Connection , connect
2122
2223import sentry_sdk
23- from sentry_sdk import capture_message , start_transaction
24+ from sentry_sdk import capture_message
2425from sentry_sdk .consts import OP , SPANDATA
2526from sentry_sdk .integrations .asyncpg import AsyncPGIntegration
2627from sentry_sdk .tracing_utils import record_sql_queries
@@ -539,6 +540,7 @@ async def test_query_source_disabled(
539540 )
540541
541542 await conn .close ()
543+
542544 sentry_sdk .flush ()
543545
544546 spans = [item .payload for item in items ]
@@ -587,6 +589,7 @@ async def test_query_source_enabled(
587589 )
588590
589591 await conn .close ()
592+
590593 sentry_sdk .flush ()
591594
592595 spans = [item .payload for item in items ]
@@ -600,12 +603,8 @@ async def test_query_source_enabled(
600603 assert segment ["name" ] == "test_segment"
601604 assert insert_span ["name" ].startswith ("INSERT INTO" )
602605 assert connect_span ["name" ] == "connect"
603- data = insert_span .get ("attributes" , {})
604606
605- assert "code.line.number" in data
606- assert "code.file.path" in data
607- assert SPANDATA .CODE_NAMESPACE in data
608- assert SPANDATA .CODE_FUNCTION in data
607+ _assert_query_source (insert_span , "test_query_source_enabled" )
609608
610609
611610@pytest .mark .asyncio
@@ -627,39 +626,16 @@ async def test_query_source(sentry_init, capture_items):
627626 )
628627
629628 await conn .close ()
629+
630630 sentry_sdk .flush ()
631631
632632 spans = [item .payload for item in items ]
633633
634634 assert len (spans ) == 3
635635
636- connect_span = spans [0 ]
637- insert_span = spans [1 ]
638- segment = spans [2 ]
636+ _ , insert_span , _ = spans
639637
640- assert segment ["name" ] == "test_segment"
641- assert insert_span ["name" ].startswith ("INSERT INTO" )
642- assert connect_span ["name" ] == "connect"
643- data = insert_span .get ("attributes" , {})
644-
645- assert "code.line.number" in data
646- assert "code.file.path" in data
647- assert SPANDATA .CODE_NAMESPACE in data
648- assert SPANDATA .CODE_FUNCTION in data
649-
650- assert type (data .get ("code.line.number" )) == int
651- assert data .get ("code.line.number" ) > 0
652- assert (
653- data .get (SPANDATA .CODE_NAMESPACE ) == "tests.integrations.asyncpg.test_asyncpg"
654- )
655- assert data .get ("code.file.path" ).endswith (
656- "tests/integrations/asyncpg/test_asyncpg.py"
657- )
658-
659- is_relative_path = data .get ("code.file.path" )[0 ] != os .sep
660- assert is_relative_path
661-
662- assert data .get (SPANDATA .CODE_FUNCTION ) == "test_query_source"
638+ _assert_query_source (insert_span , "test_query_source" )
663639
664640
665641@pytest .mark .asyncio
@@ -730,32 +706,21 @@ async def test_no_query_source_if_duration_too_short(
730706 integrations = [AsyncPGIntegration ()],
731707 traces_sample_rate = 1.0 ,
732708 enable_db_query_source = True ,
733- db_query_source_threshold_ms = 100 ,
709+ db_query_source_threshold_ms = 100000 ,
734710 trace_lifecycle = "stream" ,
735711 )
736712
737713 items = capture_items ("span" )
738714
739- @contextmanager
740- def fake_record_sql_queries (* args , ** kwargs ):
741- with record_sql_queries (* args , ** kwargs ) as span :
742- pass
743- span ._start_timestamp = datetime .datetime (2024 , 1 , 1 , microsecond = 0 )
744- span ._end_timestamp = datetime .datetime (2024 , 1 , 1 , microsecond = 99999 )
745- yield span
746-
747715 with sentry_sdk .traces .start_span (name = "test_segment" ):
748716 conn : Connection = await connect (PG_CONNECTION_URI )
749717
750- with mock .patch (
751- "sentry_sdk.integrations.asyncpg.record_sql_queries" ,
752- fake_record_sql_queries ,
753- ):
754- await conn .execute (
755- "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')" ,
756- )
718+ await conn .execute (
719+ "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')" ,
720+ )
757721
758722 await conn .close ()
723+
759724 sentry_sdk .flush ()
760725
761726 spans = [item .payload for item in items ]
@@ -778,26 +743,27 @@ def fake_record_sql_queries(*args, **kwargs):
778743
779744
780745@pytest .mark .asyncio
781- async def test_query_source_if_duration_over_threshold (sentry_init , capture_events ):
746+ async def test_query_source_if_duration_over_threshold (sentry_init , capture_items ):
782747 sentry_init (
783748 integrations = [AsyncPGIntegration ()],
784749 traces_sample_rate = 1.0 ,
750+ trace_lifecycle = "stream" ,
785751 enable_db_query_source = True ,
786752 db_query_source_threshold_ms = 100 ,
787753 )
788754
789- events = capture_events ()
755+ items = capture_items ()
790756
791- with start_transaction (name = "test_transaction" , sampled = True ):
757+ with sentry_sdk . traces . start_span (name = "test_segment" ):
792758 conn : Connection = await connect (PG_CONNECTION_URI )
793759
794760 @contextmanager
795761 def fake_record_sql_queries (* args , ** kwargs ):
796762 with record_sql_queries (* args , ** kwargs ) as span :
797- pass
798- span . start_timestamp = datetime . datetime ( 2024 , 1 , 1 , microsecond = 0 )
799- span . timestamp = datetime . datetime ( 2024 , 1 , 1 , microsecond = 100001 )
800- yield span
763+ span . _start_timestamp = datetime . datetime (
764+ 2024 , 1 , 1 , microsecond = 0 , tzinfo = timezone . utc
765+ )
766+ yield span
801767
802768 with mock .patch (
803769 "sentry_sdk.integrations.asyncpg.record_sql_queries" ,
@@ -809,34 +775,21 @@ def fake_record_sql_queries(*args, **kwargs):
809775
810776 await conn .close ()
811777
812- (event ,) = events
813-
814- span = event ["spans" ][- 1 ]
815- assert span ["description" ].startswith ("INSERT INTO" )
778+ sentry_sdk .flush ()
816779
817- data = span . get ( "data" , {})
780+ spans = [ item . payload for item in items ]
818781
819- assert SPANDATA .CODE_LINENO in data
820- assert SPANDATA .CODE_NAMESPACE in data
821- assert SPANDATA .CODE_FILEPATH in data
822- assert SPANDATA .CODE_FUNCTION in data
782+ assert len (spans ) == 3
823783
824- assert type (data .get (SPANDATA .CODE_LINENO )) == int
825- assert data .get (SPANDATA .CODE_LINENO ) > 0
826- assert (
827- data .get (SPANDATA .CODE_NAMESPACE ) == "tests.integrations.asyncpg.test_asyncpg"
828- )
829- assert data .get (SPANDATA .CODE_FILEPATH ).endswith (
830- "tests/integrations/asyncpg/test_asyncpg.py"
831- )
784+ connect_span = spans [0 ]
785+ insert_span = spans [1 ]
786+ segment = spans [2 ]
832787
833- is_relative_path = data .get (SPANDATA .CODE_FILEPATH )[0 ] != os .sep
834- assert is_relative_path
788+ assert segment ["name" ] == "test_segment"
789+ assert insert_span ["name" ].startswith ("INSERT INTO" )
790+ assert connect_span ["name" ] == "connect"
835791
836- assert (
837- data .get (SPANDATA .CODE_FUNCTION )
838- == "test_query_source_if_duration_over_threshold"
839- )
792+ _assert_query_source (insert_span , "test_query_source_if_duration_over_threshold" )
840793
841794
842795@pytest .mark .asyncio
@@ -902,6 +855,7 @@ async def test_multiline_query_description_normalized(
902855 """
903856 )
904857 await conn .close ()
858+
905859 sentry_sdk .flush ()
906860
907861 spans = [item .payload for item in items ]
@@ -917,48 +871,6 @@ async def test_multiline_query_description_normalized(
917871 assert select_span ["name" ] == "SELECT id, name FROM users WHERE name = 'Alice'"
918872
919873
920- @pytest .mark .asyncio
921- async def test_before_send_transaction_sees_normalized_description (
922- sentry_init , capture_events
923- ):
924- def before_send_transaction (event , hint ):
925- for span in event .get ("spans" , []):
926- desc = span .get ("description" , "" )
927- if "SELECT id, name FROM users" in desc :
928- span ["description" ] = "filtered"
929- return event
930-
931- sentry_init (
932- integrations = [AsyncPGIntegration ()],
933- traces_sample_rate = 1.0 ,
934- before_send_transaction = before_send_transaction ,
935- )
936- events = capture_events ()
937-
938- with start_transaction (name = "test_transaction" ):
939- conn : Connection = await connect (PG_CONNECTION_URI )
940- await conn .execute (
941- """
942- SELECT
943- id,
944- name
945- FROM
946- users
947- """
948- )
949- await conn .close ()
950-
951- (event ,) = events
952- spans = [
953- s
954- for s in event ["spans" ]
955- if s ["op" ] == "db" and "filtered" in s .get ("description" , "" )
956- ]
957-
958- assert len (spans ) == 1
959- assert spans [0 ]["description" ] == "filtered"
960-
961-
962874def _assert_query_source (span , expected_function ):
963875 data = span .get ("attributes" , {})
964876
0 commit comments