Skip to content

Commit c7e0ead

Browse files
authored
chore: add logger support for properties. (#683)
* chore: add logger support for properties. * update function * update label format
1 parent 6eb19a7 commit c7e0ead

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

bigframes/core/log_adapter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def class_logger(decorated_cls):
3030
for attr_name, attr_value in decorated_cls.__dict__.items():
3131
if callable(attr_value) and (attr_name not in _excluded_methods):
3232
setattr(decorated_cls, attr_name, method_logger(attr_value, decorated_cls))
33+
elif isinstance(attr_value, property):
34+
setattr(
35+
decorated_cls, attr_name, property_logger(attr_value, decorated_cls)
36+
)
3337
return decorated_cls
3438

3539

@@ -56,6 +60,35 @@ def wrapper(*args, **kwargs):
5660
return wrapper
5761

5862

63+
def property_logger(prop, decorated_cls):
64+
"""Decorator that adds logging functionality to a property."""
65+
66+
def shared_wrapper(f):
67+
@functools.wraps(f)
68+
def wrapped(*args, **kwargs):
69+
class_name = decorated_cls.__name__
70+
property_name = f.__name__
71+
full_property_name = f"{class_name.lower()}-{property_name.lower()}"
72+
73+
if len(_call_stack) == 0:
74+
add_api_method(full_property_name)
75+
76+
_call_stack.append(full_property_name)
77+
try:
78+
return f(*args, **kwargs)
79+
finally:
80+
_call_stack.pop()
81+
82+
return wrapped
83+
84+
# Apply the wrapper to the getter, setter, and deleter
85+
return property(
86+
shared_wrapper(prop.fget),
87+
shared_wrapper(prop.fset) if prop.fset else None,
88+
shared_wrapper(prop.fdel) if prop.fdel else None,
89+
)
90+
91+
5992
def add_api_method(api_method_name):
6093
global _lock
6194
global _api_methods

tests/unit/session/test_io_bigquery.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,18 @@ def test_create_job_configs_labels_log_adaptor_call_method_under_length_limit():
6464
# Test running two methods
6565
df.head()
6666
df.max()
67+
df.columns
6768
api_methods = log_adapter._api_methods
6869

6970
labels = io_bq.create_job_configs_labels(
7071
job_configs_labels=cur_labels, api_methods=api_methods
7172
)
7273
expected_dict = {
7374
"source": "bigquery-dataframes-temp",
74-
"bigframes-api": "dataframe-max",
75-
"recent-bigframes-api-0": "dataframe-head",
76-
"recent-bigframes-api-1": "dataframe-__init__",
75+
"bigframes-api": "dataframe-columns",
76+
"recent-bigframes-api-0": "dataframe-max",
77+
"recent-bigframes-api-1": "dataframe-head",
78+
"recent-bigframes-api-2": "dataframe-__init__",
7779
}
7880
assert labels == expected_dict
7981

0 commit comments

Comments
 (0)