Skip to content

Commit 704d259

Browse files
authoredJan 29, 2024
Enable DB query source by default (#2629)
1 parent 1a9225c commit 704d259

File tree

5 files changed

+144
-21
lines changed

5 files changed

+144
-21
lines changed
 

‎sentry_sdk/consts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def __init__(
290290
max_value_length=DEFAULT_MAX_VALUE_LENGTH, # type: int
291291
enable_backpressure_handling=True, # type: bool
292292
error_sampler=None, # type: Optional[Callable[[Event, Hint], Union[float, bool]]]
293-
enable_db_query_source=False, # type: bool
293+
enable_db_query_source=True, # type: bool
294294
db_query_source_threshold_ms=100, # type: int
295295
spotlight=None, # type: Optional[Union[bool, str]]
296296
):

‎sentry_sdk/tracing_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def add_query_source(hub, span):
180180
if span.timestamp is None or span.start_timestamp is None:
181181
return
182182

183-
should_add_query_source = client.options.get("enable_db_query_source", False)
183+
should_add_query_source = client.options.get("enable_db_query_source", True)
184184
if not should_add_query_source:
185185
return
186186

‎tests/integrations/asyncpg/test_asyncpg.py

+42-7
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,13 @@ async def test_connection_pool(sentry_init, capture_events) -> None:
472472

473473

474474
@pytest.mark.asyncio
475-
@pytest.mark.parametrize("enable_db_query_source", [None, False])
476-
async def test_query_source_disabled(
477-
sentry_init, capture_events, enable_db_query_source
478-
):
475+
async def test_query_source_disabled(sentry_init, capture_events):
479476
sentry_options = {
480477
"integrations": [AsyncPGIntegration()],
481478
"enable_tracing": True,
479+
"enable_db_query_source": False,
480+
"db_query_source_threshold_ms": 0,
482481
}
483-
if enable_db_query_source is not None:
484-
sentry_options["enable_db_query_source"] = enable_db_query_source
485-
sentry_options["db_query_source_threshold_ms"] = 0
486482

487483
sentry_init(**sentry_options)
488484

@@ -510,6 +506,45 @@ async def test_query_source_disabled(
510506
assert SPANDATA.CODE_FUNCTION not in data
511507

512508

509+
@pytest.mark.asyncio
510+
@pytest.mark.parametrize("enable_db_query_source", [None, True])
511+
async def test_query_source_enabled(
512+
sentry_init, capture_events, enable_db_query_source
513+
):
514+
sentry_options = {
515+
"integrations": [AsyncPGIntegration()],
516+
"enable_tracing": True,
517+
"db_query_source_threshold_ms": 0,
518+
}
519+
if enable_db_query_source is not None:
520+
sentry_options["enable_db_query_source"] = enable_db_query_source
521+
522+
sentry_init(**sentry_options)
523+
524+
events = capture_events()
525+
526+
with start_transaction(name="test_transaction", sampled=True):
527+
conn: Connection = await connect(PG_CONNECTION_URI)
528+
529+
await conn.execute(
530+
"INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')",
531+
)
532+
533+
await conn.close()
534+
535+
(event,) = events
536+
537+
span = event["spans"][-1]
538+
assert span["description"].startswith("INSERT INTO")
539+
540+
data = span.get("data", {})
541+
542+
assert SPANDATA.CODE_LINENO in data
543+
assert SPANDATA.CODE_NAMESPACE in data
544+
assert SPANDATA.CODE_FILEPATH in data
545+
assert SPANDATA.CODE_FUNCTION in data
546+
547+
513548
@pytest.mark.asyncio
514549
async def test_query_source(sentry_init, capture_events):
515550
sentry_init(

‎tests/integrations/django/test_db_query_data.py

+47-7
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,14 @@ def client():
3535

3636
@pytest.mark.forked
3737
@pytest_mark_django_db_decorator(transaction=True)
38-
@pytest.mark.parametrize("enable_db_query_source", [None, False])
39-
def test_query_source_disabled(
40-
sentry_init, client, capture_events, enable_db_query_source
41-
):
38+
def test_query_source_disabled(sentry_init, client, capture_events):
4239
sentry_options = {
4340
"integrations": [DjangoIntegration()],
4441
"send_default_pii": True,
4542
"traces_sample_rate": 1.0,
43+
"enable_db_query_source": False,
44+
"db_query_source_threshold_ms": 0,
4645
}
47-
if enable_db_query_source is not None:
48-
sentry_options["enable_db_query_source"] = enable_db_query_source
49-
sentry_options["db_query_source_threshold_ms"] = 0
5046

5147
sentry_init(**sentry_options)
5248

@@ -75,6 +71,50 @@ def test_query_source_disabled(
7571
raise AssertionError("No db span found")
7672

7773

74+
@pytest.mark.forked
75+
@pytest_mark_django_db_decorator(transaction=True)
76+
@pytest.mark.parametrize("enable_db_query_source", [None, True])
77+
def test_query_source_enabled(
78+
sentry_init, client, capture_events, enable_db_query_source
79+
):
80+
sentry_options = {
81+
"integrations": [DjangoIntegration()],
82+
"send_default_pii": True,
83+
"traces_sample_rate": 1.0,
84+
"db_query_source_threshold_ms": 0,
85+
}
86+
87+
if enable_db_query_source is not None:
88+
sentry_options["enable_db_query_source"] = enable_db_query_source
89+
90+
sentry_init(**sentry_options)
91+
92+
if "postgres" not in connections:
93+
pytest.skip("postgres tests disabled")
94+
95+
# trigger Django to open a new connection by marking the existing one as None.
96+
connections["postgres"].connection = None
97+
98+
events = capture_events()
99+
100+
_, status, _ = unpack_werkzeug_response(client.get(reverse("postgres_select_orm")))
101+
assert status == "200 OK"
102+
103+
(event,) = events
104+
for span in event["spans"]:
105+
if span.get("op") == "db" and "auth_user" in span.get("description"):
106+
data = span.get("data", {})
107+
108+
assert SPANDATA.CODE_LINENO in data
109+
assert SPANDATA.CODE_NAMESPACE in data
110+
assert SPANDATA.CODE_FILEPATH in data
111+
assert SPANDATA.CODE_FUNCTION in data
112+
113+
break
114+
else:
115+
raise AssertionError("No db span found")
116+
117+
78118
@pytest.mark.forked
79119
@pytest_mark_django_db_decorator(transaction=True)
80120
def test_query_source(sentry_init, client, capture_events):

‎tests/integrations/sqlalchemy/test_sqlalchemy.py

+53-5
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,13 @@ def test_engine_name_not_string(sentry_init):
235235
con.execute(text("SELECT 0"))
236236

237237

238-
@pytest.mark.parametrize("enable_db_query_source", [None, False])
239-
def test_query_source_disabled(sentry_init, capture_events, enable_db_query_source):
238+
def test_query_source_disabled(sentry_init, capture_events):
240239
sentry_options = {
241240
"integrations": [SqlalchemyIntegration()],
242241
"enable_tracing": True,
242+
"enable_db_query_source": False,
243+
"db_query_source_threshold_ms": 0,
243244
}
244-
if enable_db_query_source is not None:
245-
sentry_options["enable_db_query_source"] = enable_db_query_source
246-
sentry_options["db_query_source_threshold_ms"] = 0
247245

248246
sentry_init(**sentry_options)
249247

@@ -285,6 +283,56 @@ class Person(Base):
285283
raise AssertionError("No db span found")
286284

287285

286+
@pytest.mark.parametrize("enable_db_query_source", [None, True])
287+
def test_query_source_enabled(sentry_init, capture_events, enable_db_query_source):
288+
sentry_options = {
289+
"integrations": [SqlalchemyIntegration()],
290+
"enable_tracing": True,
291+
"db_query_source_threshold_ms": 0,
292+
}
293+
if enable_db_query_source is not None:
294+
sentry_options["enable_db_query_source"] = enable_db_query_source
295+
296+
sentry_init(**sentry_options)
297+
298+
events = capture_events()
299+
300+
with start_transaction(name="test_transaction", sampled=True):
301+
Base = declarative_base() # noqa: N806
302+
303+
class Person(Base):
304+
__tablename__ = "person"
305+
id = Column(Integer, primary_key=True)
306+
name = Column(String(250), nullable=False)
307+
308+
engine = create_engine("sqlite:///:memory:")
309+
Base.metadata.create_all(engine)
310+
311+
Session = sessionmaker(bind=engine) # noqa: N806
312+
session = Session()
313+
314+
bob = Person(name="Bob")
315+
session.add(bob)
316+
317+
assert session.query(Person).first() == bob
318+
319+
(event,) = events
320+
321+
for span in event["spans"]:
322+
if span.get("op") == "db" and span.get("description").startswith(
323+
"SELECT person"
324+
):
325+
data = span.get("data", {})
326+
327+
assert SPANDATA.CODE_LINENO in data
328+
assert SPANDATA.CODE_NAMESPACE in data
329+
assert SPANDATA.CODE_FILEPATH in data
330+
assert SPANDATA.CODE_FUNCTION in data
331+
break
332+
else:
333+
raise AssertionError("No db span found")
334+
335+
288336
def test_query_source(sentry_init, capture_events):
289337
sentry_init(
290338
integrations=[SqlalchemyIntegration()],

0 commit comments

Comments
 (0)
Please sign in to comment.