Skip to content

Commit bff9adb

Browse files
authored
feat: BI-6450 split debug query for logging and inspector (#1379)
* add inspector query * fixes * tmp test * trying to fix debug query in good inspector * rm debug strings * lint * lint * typo fix
1 parent 217090c commit bff9adb

File tree

38 files changed

+292
-164
lines changed

38 files changed

+292
-164
lines changed

lib/dl_api_lib/dl_api_lib/dataset/validator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ def get_schema_info(exists: bool) -> Optional[SchemaInfo]:
11781178
raise common_exc.SourceDoesNotExist(
11791179
db_message="",
11801180
query="",
1181+
inspector_query="",
11811182
params=params,
11821183
)
11831184
return None

lib/dl_compeng_pg/dl_compeng_pg/compeng_asyncpg/exec_adapter_asyncpg.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ async def _execute_ddl(self, query: Union[str, sa.sql.base.Executable]) -> None:
8080
await self._execute(query)
8181

8282
@contextmanager
83-
def handle_db_errors(self, query_text: str) -> Generator[None, None, None]:
83+
def handle_db_errors(self, debug_query: str, inspector_query: str | None) -> Generator[None, None, None]:
8484
try:
8585
yield
8686
except Exception as wrapper_exc:
87-
trans_exc = self._error_transformer.make_bi_error(wrapper_exc=wrapper_exc, debug_compiled_query=query_text)
87+
trans_exc = self._error_transformer.make_bi_error(
88+
wrapper_exc=wrapper_exc, debug_query=debug_query, inspector_query=inspector_query
89+
)
8890
raise trans_exc from wrapper_exc
8991

9092
async def _execute_and_fetch(
@@ -107,7 +109,7 @@ async def _execute_and_fetch(
107109
async def chunked_data_gen() -> AsyncGenerator[list[list[TBIDataValue]], None]:
108110
"""Fetch data in chunks"""
109111

110-
with self.handle_db_errors(query_text=query_text):
112+
with self.handle_db_errors(debug_query=query_text, inspector_query=query_text): # TODO: BI-6448
111113
cur = await self._conn.cursor(query_text, *params)
112114
while True:
113115
chunk = []

lib/dl_connector_bitrix_gds/dl_connector_bitrix_gds/core/adapter.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ async def _run_query(self, dba_query: DBAdapterQuery) -> Any:
163163
if limit is not None:
164164
request_params["limit"] = limit
165165

166-
with self.handle_execution_error(query_text):
166+
# TODO: BI-6448 check it works and replace
167+
# debug_query = compile_query_for_debug(dba_query.query, self.get_dialect())
168+
with self.handle_execution_error(debug_query=query_text, inspector_query=query_text):
167169
resp = await self._session.post(
168170
url=api_url,
169171
params=request_params,
@@ -250,6 +252,7 @@ def _parse_response_body(self, body: Any, dba_query: DBAdapterQuery) -> dict:
250252
message=f"Unexpected API response body: {err.args[0]}",
251253
db_message="",
252254
query=dba_query.debug_compiled_query,
255+
inspector_query=dba_query.inspector_query,
253256
orig=None,
254257
details={},
255258
) from err
@@ -297,9 +300,12 @@ async def _get_user_tables(self, schema_ident: SchemaIdent) -> list[str]:
297300
body: dict[str, Any] = {
298301
"key": self._target_dto.token,
299302
}
303+
# TODO: BI-6448 example if we want hide portal (want we? don't know)
304+
# base_url = "https://{portal}/bitrix/tools/biconnector/gds.php?show_tables"
305+
# api_url = base_url.format(portal=self._target_dto.portal)
300306
api_url: str = f"https://{self._target_dto.portal}/bitrix/tools/biconnector/gds.php?show_tables"
301307

302-
with self.handle_execution_error(api_url):
308+
with self.handle_execution_error(debug_query=api_url, inspector_query=api_url):
303309
resp = await self._session.post(
304310
url=api_url,
305311
json=body,

lib/dl_connector_bundle_chs3/dl_connector_bundle_chs3/chs3_base/core/adapter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ async def _make_query(self, dba_q: DBAdapterQuery, mirroring_mode: bool = False)
6565
)
6666

6767
debug_query = dba_q.debug_compiled_query if dba_q.debug_compiled_query is not None else query_str_raw
68+
debug_compiled_query = reduce(lambda a, kv: a.replace(*kv), secrets_hidden, debug_query) % ()
6869

6970
dba_q = dba_q.clone(
7071
query=reduce(lambda a, kv: a.replace(*kv), secrets, query_str_raw) % (),
71-
debug_compiled_query=reduce(lambda a, kv: a.replace(*kv), secrets_hidden, debug_query) % (),
72+
debug_compiled_query=debug_compiled_query,
73+
inspector_query=debug_compiled_query, # TODO: BI-6448
7274
)
7375
return await super()._make_query(dba_q, mirroring_mode)

lib/dl_connector_chyt/dl_connector_chyt/core/adapters.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ def get_ch_settings(self) -> dict:
7878
def make_exc( # TODO: Move to ErrorTransformer
7979
cls,
8080
wrapper_exc: Exception,
81-
orig_exc: Optional[Exception],
82-
debug_compiled_query: Optional[str],
81+
orig_exc: Exception | None,
82+
debug_query: str | None,
83+
inspector_query: str | None,
8384
**kwargs: Any,
8485
) -> tuple[type[exc.DatabaseQueryError], DBExcKWArgs]:
8586
exc_cls, kw = super().make_exc(
8687
wrapper_exc=wrapper_exc,
8788
orig_exc=orig_exc,
88-
debug_compiled_query=debug_compiled_query,
89+
debug_query=debug_query,
90+
inspector_query=inspector_query,
8991
**kwargs,
9092
)
9193
return ensure_db_message(exc_cls, kw)

lib/dl_connector_clickhouse/dl_connector_clickhouse/core/clickhouse_base/adapters.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ def _convert_headers_to_dsn_params(headers: dict[str, str]) -> dict[str, str]:
183183

184184
@classmethod
185185
def make_exc( # TODO: Move to ErrorTransformer
186-
cls, wrapper_exc: Exception, orig_exc: Optional[Exception], debug_compiled_query: Optional[str]
186+
cls,
187+
wrapper_exc: Exception,
188+
orig_exc: Exception | None,
189+
debug_query: str | None,
190+
inspector_query: str | None,
187191
) -> tuple[type[exc.DatabaseQueryError], DBExcKWArgs]:
188-
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_compiled_query)
192+
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_query, inspector_query)
189193

190194
ch_exc_cls: Optional[type[exc.DatabaseQueryError]] = None
191195
if isinstance(wrapper_exc, ch_exc.DatabaseException):
@@ -517,6 +521,7 @@ async def _parse_response_body(self, resp: ClientResponse) -> AsyncGenerator[tup
517521
debug_info=dict(msg="Unexpected last event", event=event, data=data, chunk=decoded_chunk),
518522
# TODO: provide the actual query
519523
query="",
524+
inspector_query="",
520525
# TODO?: show the chunk here for the user databases?
521526
db_message="",
522527
)
@@ -542,7 +547,9 @@ async def execute(self, query: DBAdapterQuery) -> AsyncRawExecutionResult:
542547
if resp.status != 200:
543548
bytes_body = await resp.read()
544549
body = bytes_body.decode(errors="replace")
545-
db_exc = self.make_exc(resp.status, body, debug_compiled_query=query.debug_compiled_query)
550+
db_exc = self.make_exc(
551+
resp.status, body, debug_query=query.debug_compiled_query, inspector_query=query.inspector_query
552+
)
546553
raise db_exc
547554

548555
# Primarily for DashSQL
@@ -563,7 +570,9 @@ def _safe_col_converter(col_conv: Callable[[str], Any], val: str) -> Any:
563570
try:
564571
return col_conv(val)
565572
except ValueError as err:
566-
raise exc.DataParseError(f"Cannot convert {val!r}", query=query.debug_compiled_query) from err
573+
raise exc.DataParseError(
574+
f"Cannot convert {val!r}", query=query.debug_compiled_query, inspector_query=query.inspector_query
575+
) from err
567576

568577
events_generator = self._parse_response_body(resp)
569578
with self.wrap_execute_excs(query=query, stage="meta"):
@@ -642,7 +651,8 @@ def make_exc( # TODO: Move to ErrorTransformer
642651
self,
643652
status_code: int, # noqa
644653
err_body: str,
645-
debug_compiled_query: Optional[str] = None,
654+
debug_query: str | None = None,
655+
inspector_query: str | None = None,
646656
) -> exc.DatabaseQueryError:
647657
exc_cls: type[exc.DatabaseQueryError]
648658
try:
@@ -652,7 +662,8 @@ def make_exc( # TODO: Move to ErrorTransformer
652662

653663
return exc_cls(
654664
db_message=err_body,
655-
query=debug_compiled_query,
665+
query=debug_query,
666+
inspector_query=inspector_query,
656667
orig=None,
657668
details={},
658669
params=exc_params,

lib/dl_connector_clickhouse/dl_connector_clickhouse/core/clickhouse_base/exc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(
1616
self,
1717
db_message: Optional[str] = None,
1818
query: Optional[str] = None,
19+
inspector_query: Optional[str] = None,
1920
message: Optional[str] = None,
2021
details: Optional[dict[str, Any]] = None,
2122
orig: Optional[Exception] = None,
@@ -25,6 +26,7 @@ def __init__(
2526
super(ClickHouseSourceDoesNotExistError, self).__init__(
2627
db_message=db_message,
2728
query=query,
29+
inspector_query=inspector_query,
2830
message=message,
2931
details=details,
3032
orig=orig,

lib/dl_connector_metrica/dl_connector_metrica/core/adapters_metrica_x.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ def _get_db_engine(self, db_name: str, disable_streaming: bool = False) -> Engin
5555

5656
@classmethod
5757
def make_exc( # TODO: Move to ErrorTransformer
58-
cls, wrapper_exc: Exception, orig_exc: Optional[Exception], debug_compiled_query: Optional[str]
58+
cls,
59+
wrapper_exc: Exception,
60+
orig_exc: Exception | None,
61+
debug_query: str | None,
62+
inspector_query: str | None,
5963
) -> tuple[type[exc.DatabaseQueryError], DBExcKWArgs]:
60-
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_compiled_query)
64+
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_query, inspector_query)
6165

6266
if isinstance(
6367
orig_exc,

lib/dl_connector_mssql/dl_connector_mssql/core/adapters_mssql.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,13 @@ def get_exc_class(cls, err_msg: str) -> Optional[type[exc.DatabaseQueryError]]:
255255

256256
@classmethod
257257
def make_exc( # TODO: Move to ErrorTransformer
258-
cls, wrapper_exc: Exception, orig_exc: Optional[Exception], debug_compiled_query: Optional[str]
258+
cls,
259+
wrapper_exc: Exception,
260+
orig_exc: Exception | None,
261+
debug_query: str | None,
262+
inspector_query: str | None,
259263
) -> tuple[type[exc.DatabaseQueryError], DBExcKWArgs]:
260-
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_compiled_query)
264+
exc_cls, kw = super().make_exc(wrapper_exc, orig_exc, debug_query, inspector_query)
261265

262266
db_msg = kw["db_message"]
263267
specific_exc_cls = cls.get_exc_class(db_msg) # type: ignore # TODO: fix

lib/dl_connector_mssql/dl_connector_mssql/core/exc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(
1414
self,
1515
db_message: Optional[str] = None,
1616
query: Optional[str] = None,
17+
inspector_query: Optional[str] = None,
1718
message: Optional[str] = None,
1819
details: Optional[dict[str, Any]] = None,
1920
orig: Optional[Exception] = None,
@@ -23,6 +24,7 @@ def __init__(
2324
super(SyncMssqlSourceDoesNotExistError, self).__init__(
2425
db_message=db_message,
2526
query=query,
27+
inspector_query=inspector_query,
2628
message=message,
2729
details=details,
2830
orig=orig,

0 commit comments

Comments
 (0)