Skip to content

Commit

Permalink
logging has CQL + SCB-download at DEBUG and CQL args at TRACE levels (#…
Browse files Browse the repository at this point in the history
…141)

logging has CQL + SCB-download at DEBUG and CQL args at TRACE levels
  • Loading branch information
hemidactylus authored Feb 15, 2024
1 parent 66ff77e commit 84a771f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cassio/config/bundle_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Facilities to manage the download of a secure-connect-bundle from an Astra DB
token.
"""
import logging
import requests # type: ignore
from typing import Optional

Expand All @@ -11,6 +12,9 @@
)


logger = logging.getLogger(__name__)


def get_astra_bundle_url(
database_id: str, token: str, bundle_url_template: Optional[str] = None
) -> str:
Expand All @@ -23,10 +27,11 @@ def get_astra_bundle_url(
if not bundle_url_template:
bundle_url_template = DEFAULT_GET_BUNDLE_URL_TEMPLATE
url = bundle_url_template.format(database_id=database_id)

headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}

logger.debug(f"Obtaining SCB URL from: {url}")
response = requests.post(url, headers=headers)
logger.debug(f"SCB URL response: {response.text}")

response_json = response.json()
# Print the response
Expand Down Expand Up @@ -67,6 +72,7 @@ def download_astra_bundle_url(
bundle_url = get_astra_bundle_url(
database_id=database_id, token=token, bundle_url_template=bundle_url_template
)
logger.debug(f"Downloading SCB from: {bundle_url}")
bundle_data = requests.get(bundle_url)
with open(out_file_path, "wb") as f:
f.write(bundle_data.content)
Expand Down
27 changes: 27 additions & 0 deletions src/cassio/table/base_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from asyncio import InvalidStateError, Task
import logging
from typing import (
Any,
cast,
Expand Down Expand Up @@ -34,6 +35,21 @@
from cassio.table.utils import call_wrapped_async


class CustomLogger(logging.Logger):
def trace(self, msg: str, *args: Any, **kwargs: Any) -> None:
if self.isEnabledFor(5):
self._log(5, msg, args, **kwargs)


logging.addLevelName(5, "TRACE")


logging.setLoggerClass(CustomLogger)


logger = logging.getLogger(__name__)


class BaseTable:
ordering_in_partition: Optional[str] = None

Expand Down Expand Up @@ -374,6 +390,7 @@ def _obtain_prepared_statement(self, final_cql: str) -> PreparedStatement:
_preparable_cql = final_cql.replace("%s", "?")
# handle the cache of prepared statements
if _preparable_cql not in self._prepared_statements:
logger.debug(f'Preparing statement "{_preparable_cql}"')
self._prepared_statements[_preparable_cql] = self.session.prepare(
_preparable_cql
)
Expand All @@ -389,12 +406,16 @@ def execute_cql(
#
if op_type == CQLOpType.SCHEMA and self.skip_provisioning:
# these operations are not executed for this instance:
logger.debug(f'Not executing statement "{final_cql}"')
return []
if op_type == CQLOpType.SCHEMA:
# schema operations are not to be 'prepared'
statement = SimpleStatement(final_cql)
logger.debug(f'Executing statement "{final_cql}" as simple (unprepared)')
else:
statement = self._obtain_prepared_statement(final_cql)
logger.debug(f'Executing statement "{final_cql}" as prepared')
logger.trace(f'Statement "{final_cql}" has args: "{str(args)}"') # type: ignore
return cast(Iterable[RowType], self.session.execute(statement, args))

def execute_cql_async(
Expand All @@ -408,6 +429,8 @@ def execute_cql_async(
if op_type == CQLOpType.SCHEMA:
raise RuntimeError("Schema operations cannot be asynchronous")
statement = self._obtain_prepared_statement(final_cql)
logger.debug(f'Executing_async statement "{final_cql}" as prepared')
logger.trace(f'Statement "{final_cql}" has args: "{str(args)}"') # type: ignore
return self.session.execute_async(statement, args)

async def aexecute_cql(
Expand All @@ -420,12 +443,16 @@ async def aexecute_cql(
#
if op_type == CQLOpType.SCHEMA and self.skip_provisioning:
# these operations are not executed for this instance:
logger.debug(f'Not aexecuting statement "{final_cql}"')
return []
if op_type == CQLOpType.SCHEMA:
# schema operations are not to be 'prepared'
statement = SimpleStatement(final_cql)
logger.debug(f'aExecuting statement "{final_cql}" as simple (unprepared)')
else:
statement = self._obtain_prepared_statement(final_cql)
logger.debug(f'aExecuting statement "{final_cql}" as prepared')
logger.trace(f'Statement "{final_cql}" has args: "{str(args)}"') # type: ignore
return cast(
Iterable[RowType],
await call_wrapped_async(self.session.execute_async, statement, args),
Expand Down

0 comments on commit 84a771f

Please sign in to comment.