Skip to content

Commit 9334dff

Browse files
committed
clickhouse fixes
1 parent 7422a4c commit 9334dff

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ensure_integration_enabled,
1010
)
1111

12-
from typing import TYPE_CHECKING, TypeVar
12+
from typing import TYPE_CHECKING, Any, TypeVar
1313

1414
# Hack to get new Python features working in older versions
1515
# without introducing a hard dependency on `typing_extensions`
@@ -93,17 +93,17 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
9393

9494
connection._sentry_span = span # type: ignore[attr-defined]
9595

96-
_set_db_data(span, connection)
97-
98-
if should_send_default_pii():
99-
span.set_attribute("db.query.text", query)
96+
data = _get_db_data(connection)
97+
data["db.query.text"] = query
10098

10199
if query_id:
102-
span.set_attribute("db.query_id", query_id)
100+
data["db.query_id"] = query_id
103101

104102
if params and should_send_default_pii():
105-
connection._sentry_db_params = params
106-
span.set_attribute("db.params", _serialize_span_attribute(params))
103+
data["db.params"] = params
104+
105+
connection._sentry_db_data = data # type: ignore[attr-defined]
106+
_set_on_span(span, data)
107107

108108
# run the original code
109109
ret = f(*args, **kwargs)
@@ -116,28 +116,20 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
116116
def _wrap_end(f: Callable[P, T]) -> Callable[P, T]:
117117
def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T:
118118
res = f(*args, **kwargs)
119-
instance = args[0]
120-
span = getattr(instance.connection, "_sentry_span", None) # type: ignore[attr-defined]
119+
connection = args[0].connection
121120

121+
span = getattr(connection, "_sentry_span", None) # type: ignore[attr-defined]
122122
if span is not None:
123+
data = getattr(connection, "_sentry_db_data", {})
124+
123125
if res is not None and should_send_default_pii():
124-
span.set_attribute("db.result", _serialize_span_attribute(res))
126+
serialized_result = _serialize_span_attribute(res)
127+
data["db.result"] = serialized_result
128+
span.set_attribute("db.result", serialized_result)
125129

126130
with capture_internal_exceptions():
127-
query = span.get_attribute("db.query.text")
131+
query = data.pop("db.query.text", None)
128132
if query:
129-
data = {}
130-
for attr in (
131-
"db.params",
132-
"db.result",
133-
SPANDATA.DB_SYSTEM,
134-
SPANDATA.DB_USER,
135-
SPANDATA.SERVER_ADDRESS,
136-
SPANDATA.SERVER_PORT,
137-
):
138-
if span.get_attribute(attr):
139-
data[attr] = span.get_attribute(attr)
140-
141133
sentry_sdk.add_breadcrumb(
142134
message=query, category="query", data=data
143135
)
@@ -152,20 +144,22 @@ def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T:
152144
def _wrap_send_data(f: Callable[P, T]) -> Callable[P, T]:
153145
def _inner_send_data(*args: P.args, **kwargs: P.kwargs) -> T:
154146
instance = args[0] # type: clickhouse_driver.client.Client
155-
data = args[2]
147+
db_params_data = args[2]
156148
span = getattr(instance.connection, "_sentry_span", None)
157149

158150
if span is not None:
159-
_set_db_data(span, instance.connection)
151+
data = _get_db_data(instance.connection)
152+
_set_on_span(span, data)
160153

161154
if should_send_default_pii():
162155
db_params = (
163-
getattr(instance.connection, "_sentry_db_params", None) or []
156+
getattr(instance.connection, "_sentry_db_data", {}).get("db.params")
157+
or []
164158
)
165-
db_params.extend(data)
159+
db_params.extend(db_params_data)
166160
span.set_attribute("db.params", _serialize_span_attribute(db_params))
167161
try:
168-
del instance.connection._sentry_db_params
162+
del instance.connection._sentry_db_data
169163
except AttributeError:
170164
pass
171165

@@ -174,11 +168,16 @@ def _inner_send_data(*args: P.args, **kwargs: P.kwargs) -> T:
174168
return _inner_send_data
175169

176170

177-
def _set_db_data(
178-
span: Span, connection: clickhouse_driver.connection.Connection
179-
) -> None:
180-
span.set_attribute(SPANDATA.DB_SYSTEM, "clickhouse")
181-
span.set_attribute(SPANDATA.SERVER_ADDRESS, connection.host)
182-
span.set_attribute(SPANDATA.SERVER_PORT, connection.port)
183-
span.set_attribute(SPANDATA.DB_NAME, connection.database)
184-
span.set_attribute(SPANDATA.DB_USER, connection.user)
171+
def _get_db_data(connection: clickhouse_driver.connection.Connection) -> dict[str, str]:
172+
return {
173+
SPANDATA.DB_SYSTEM: "clickhouse",
174+
SPANDATA.SERVER_ADDRESS: connection.host,
175+
SPANDATA.SERVER_PORT: connection.port,
176+
SPANDATA.DB_NAME: connection.database,
177+
SPANDATA.DB_USER: connection.user,
178+
}
179+
180+
181+
def _set_on_span(span: Span, data: dict[str, Any]):
182+
for key, value in data.items():
183+
span.set_attribute(key, _serialize_span_attribute(value))

0 commit comments

Comments
 (0)