9
9
ensure_integration_enabled ,
10
10
)
11
11
12
- from typing import TYPE_CHECKING , TypeVar
12
+ from typing import TYPE_CHECKING , Any , Dict , TypeVar
13
13
14
14
# Hack to get new Python features working in older versions
15
15
# without introducing a hard dependency on `typing_extensions`
@@ -94,17 +94,17 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
94
94
95
95
connection ._sentry_span = span # type: ignore[attr-defined]
96
96
97
- _set_db_data (span , connection )
98
-
99
- if should_send_default_pii ():
100
- span .set_attribute ("db.query.text" , query )
97
+ data = _get_db_data (connection )
98
+ data ["db.query.text" ] = query
101
99
102
100
if query_id :
103
- span . set_attribute ( "db.query_id" , query_id )
101
+ data [ "db.query_id" ] = query_id
104
102
105
103
if params and should_send_default_pii ():
106
- connection ._sentry_db_params = params
107
- span .set_attribute ("db.params" , _serialize_span_attribute (params ))
104
+ data ["db.params" ] = params
105
+
106
+ connection ._sentry_db_data = data # type: ignore[attr-defined]
107
+ _set_on_span (span , data )
108
108
109
109
# run the original code
110
110
ret = f (* args , ** kwargs )
@@ -117,69 +117,68 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
117
117
def _wrap_end (f : Callable [P , T ]) -> Callable [P , T ]:
118
118
def _inner_end (* args : P .args , ** kwargs : P .kwargs ) -> T :
119
119
res = f (* args , ** kwargs )
120
- instance = args [0 ]
121
- span = getattr (instance .connection , "_sentry_span" , None ) # type: ignore[attr-defined]
120
+ connection = args [0 ].connection
122
121
122
+ span = getattr (connection , "_sentry_span" , None ) # type: ignore[attr-defined]
123
123
if span is not None :
124
+ data = getattr (connection , "_sentry_db_data" , {})
125
+
124
126
if res is not None and should_send_default_pii ():
127
+ data ["db.result" ] = res
125
128
span .set_attribute ("db.result" , _serialize_span_attribute (res ))
126
129
127
130
with capture_internal_exceptions ():
128
- query = span . get_attribute ("db.query.text" )
131
+ query = data . pop ("db.query.text" , None )
129
132
if query :
130
- data = {}
131
- for attr in (
132
- "db.params" ,
133
- "db.result" ,
134
- SPANDATA .DB_SYSTEM ,
135
- SPANDATA .DB_USER ,
136
- SPANDATA .SERVER_ADDRESS ,
137
- SPANDATA .SERVER_PORT ,
138
- ):
139
- if span .get_attribute (attr ):
140
- data [attr ] = span .get_attribute (attr )
141
-
142
133
sentry_sdk .add_breadcrumb (
143
134
message = query , category = "query" , data = data
144
135
)
145
136
146
137
span .finish ()
147
138
139
+ try :
140
+ del connection ._sentry_db_data
141
+ del connection ._sentry_span
142
+ except AttributeError :
143
+ pass
144
+
148
145
return res
149
146
150
147
return _inner_end
151
148
152
149
153
150
def _wrap_send_data (f : Callable [P , T ]) -> Callable [P , T ]:
154
151
def _inner_send_data (* args : P .args , ** kwargs : P .kwargs ) -> T :
155
- instance = args [0 ] # type: clickhouse_driver.client.Client
156
- data = args [2 ]
157
- span = getattr (instance . connection , "_sentry_span" , None )
152
+ connection = args [0 ]. connection
153
+ db_params_data = args [2 ]
154
+ span = getattr (connection , "_sentry_span" , None )
158
155
159
156
if span is not None :
160
- _set_db_data (span , instance .connection )
157
+ data = _get_db_data (connection )
158
+ _set_on_span (span , data )
161
159
162
160
if should_send_default_pii ():
163
- db_params = (
164
- getattr ( instance . connection , "_sentry_db_params" , None ) or []
165
- )
166
- db_params . extend ( data )
161
+ saved_db_data = getattr ( connection , "_sentry_db_data" , {})
162
+ db_params = saved_db_data . get ( "db.params" ) or []
163
+ db_params . extend ( db_params_data )
164
+ saved_db_data [ "db.params" ] = db_params
167
165
span .set_attribute ("db.params" , _serialize_span_attribute (db_params ))
168
- try :
169
- del instance .connection ._sentry_db_params
170
- except AttributeError :
171
- pass
172
166
173
167
return f (* args , ** kwargs )
174
168
175
169
return _inner_send_data
176
170
177
171
178
- def _set_db_data (
179
- span : Span , connection : clickhouse_driver .connection .Connection
180
- ) -> None :
181
- span .set_attribute (SPANDATA .DB_SYSTEM , "clickhouse" )
182
- span .set_attribute (SPANDATA .SERVER_ADDRESS , connection .host )
183
- span .set_attribute (SPANDATA .SERVER_PORT , connection .port )
184
- span .set_attribute (SPANDATA .DB_NAME , connection .database )
185
- span .set_attribute (SPANDATA .DB_USER , connection .user )
172
+ def _get_db_data (connection : clickhouse_driver .connection .Connection ) -> Dict [str , str ]:
173
+ return {
174
+ SPANDATA .DB_SYSTEM : "clickhouse" ,
175
+ SPANDATA .SERVER_ADDRESS : connection .host ,
176
+ SPANDATA .SERVER_PORT : connection .port ,
177
+ SPANDATA .DB_NAME : connection .database ,
178
+ SPANDATA .DB_USER : connection .user ,
179
+ }
180
+
181
+
182
+ def _set_on_span (span : Span , data : Dict [str , Any ]):
183
+ for key , value in data .items ():
184
+ span .set_attribute (key , _serialize_span_attribute (value ))
0 commit comments