Skip to content

Commit 064dd83

Browse files
use separate path for streaming
1 parent f9faa2f commit 064dd83

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

sentry_sdk/integrations/sqlalchemy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from sentry_sdk.consts import SPANSTATUS, SPANDATA
22
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
3-
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
3+
from sentry_sdk.tracing_utils import (
4+
add_query_source,
5+
record_sql_queries_supporting_streaming,
6+
)
47
from sentry_sdk.utils import (
58
capture_internal_exceptions,
69
ensure_integration_enabled,
@@ -49,7 +52,7 @@ def _before_cursor_execute(
4952
executemany: bool,
5053
*args: "Any",
5154
) -> None:
52-
ctx_mgr = record_sql_queries(
55+
ctx_mgr = record_sql_queries_supporting_streaming(
5356
cursor,
5457
statement,
5558
parameters,

sentry_sdk/tracing_utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,52 @@ def record_sql_queries(
133133
executemany: bool,
134134
record_cursor_repr: bool = False,
135135
span_origin: str = "manual",
136+
) -> "Generator[sentry_sdk.tracing.Span, None, None]":
137+
# TODO: Bring back capturing of params by default
138+
if sentry_sdk.get_client().options["_experiments"].get("record_sql_params", False):
139+
if not params_list or params_list == [None]:
140+
params_list = None
141+
142+
if paramstyle == "pyformat":
143+
paramstyle = "format"
144+
else:
145+
params_list = None
146+
paramstyle = None
147+
148+
query = _format_sql(cursor, query)
149+
150+
data = {}
151+
if params_list is not None:
152+
data["db.params"] = params_list
153+
if paramstyle is not None:
154+
data["db.paramstyle"] = paramstyle
155+
if executemany:
156+
data["db.executemany"] = True
157+
if record_cursor_repr and cursor is not None:
158+
data["db.cursor"] = cursor
159+
160+
with capture_internal_exceptions():
161+
sentry_sdk.add_breadcrumb(message=query, category="query", data=data)
162+
163+
with sentry_sdk.start_span(
164+
op=OP.DB,
165+
name=query,
166+
origin=span_origin,
167+
) as span:
168+
for k, v in data.items():
169+
span.set_data(k, v)
170+
yield span
171+
172+
173+
@contextlib.contextmanager
174+
def record_sql_queries_supporting_streaming(
175+
cursor: "Any",
176+
query: "Any",
177+
params_list: "Any",
178+
paramstyle: "Optional[str]",
179+
executemany: bool,
180+
record_cursor_repr: bool = False,
181+
span_origin: str = "manual",
136182
) -> "Generator[Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan], None, None]":
137183
# TODO: Bring back capturing of params by default
138184
client = sentry_sdk.get_client()

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, SPANDATA
1515
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
1616
from sentry_sdk.serializer import MAX_EVENT_BYTES
17-
from sentry_sdk.tracing_utils import record_sql_queries
17+
from sentry_sdk.tracing_utils import record_sql_queries_supporting_streaming
1818
from sentry_sdk.utils import json_dumps
1919

2020

@@ -922,7 +922,9 @@ class Person(Base):
922922

923923
class fake_record_sql_queries: # noqa: N801
924924
def __init__(self, *args, **kwargs):
925-
with record_sql_queries(*args, **kwargs) as span:
925+
with record_sql_queries_supporting_streaming(
926+
*args, **kwargs
927+
) as span:
926928
self.span = span
927929

928930
if span_streaming:
@@ -939,7 +941,7 @@ def __exit__(self, type, value, traceback):
939941
pass
940942

941943
with mock.patch(
942-
"sentry_sdk.integrations.sqlalchemy.record_sql_queries",
944+
"sentry_sdk.integrations.sqlalchemy.record_sql_queries_supporting_streaming",
943945
fake_record_sql_queries,
944946
):
945947
assert session.query(Person).first() == bob
@@ -983,7 +985,9 @@ class Person(Base):
983985

984986
class fake_record_sql_queries: # noqa: N801
985987
def __init__(self, *args, **kwargs):
986-
with record_sql_queries(*args, **kwargs) as span:
988+
with record_sql_queries_supporting_streaming(
989+
*args, **kwargs
990+
) as span:
987991
self.span = span
988992

989993
if span_streaming:
@@ -1000,7 +1004,7 @@ def __exit__(self, type, value, traceback):
10001004
pass
10011005

10021006
with mock.patch(
1003-
"sentry_sdk.integrations.sqlalchemy.record_sql_queries",
1007+
"sentry_sdk.integrations.sqlalchemy.record_sql_queries_supporting_streaming",
10041008
fake_record_sql_queries,
10051009
):
10061010
assert session.query(Person).first() == bob
@@ -1061,7 +1065,9 @@ class Person(Base):
10611065

10621066
class fake_record_sql_queries: # noqa: N801
10631067
def __init__(self, *args, **kwargs):
1064-
with record_sql_queries(*args, **kwargs) as span:
1068+
with record_sql_queries_supporting_streaming(
1069+
*args, **kwargs
1070+
) as span:
10651071
self.span = span
10661072

10671073
self.span._start_timestamp = datetime(2024, 1, 1, microsecond=0)
@@ -1074,7 +1080,7 @@ def __exit__(self, type, value, traceback):
10741080
pass
10751081

10761082
with mock.patch(
1077-
"sentry_sdk.integrations.sqlalchemy.record_sql_queries",
1083+
"sentry_sdk.integrations.sqlalchemy.record_sql_queries_supporting_streaming",
10781084
fake_record_sql_queries,
10791085
):
10801086
assert session.query(Person).first() == bob
@@ -1131,7 +1137,9 @@ class Person(Base):
11311137

11321138
class fake_record_sql_queries: # noqa: N801
11331139
def __init__(self, *args, **kwargs):
1134-
with record_sql_queries(*args, **kwargs) as span:
1140+
with record_sql_queries_supporting_streaming(
1141+
*args, **kwargs
1142+
) as span:
11351143
self.span = span
11361144

11371145
self.span.start_timestamp = datetime(2024, 1, 1, microsecond=0)
@@ -1144,7 +1152,7 @@ def __exit__(self, type, value, traceback):
11441152
pass
11451153

11461154
with mock.patch(
1147-
"sentry_sdk.integrations.sqlalchemy.record_sql_queries",
1155+
"sentry_sdk.integrations.sqlalchemy.record_sql_queries_supporting_streaming",
11481156
fake_record_sql_queries,
11491157
):
11501158
assert session.query(Person).first() == bob

0 commit comments

Comments
 (0)