Skip to content

Commit 21bd3e4

Browse files
authored
chore: log and labels update (#674)
* chore: log and labels update * remove unused logic * Update unit test. * fixes for mypy * lint update
1 parent f6bdc4a commit 21bd3e4

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

bigframes/core/log_adapter.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
_api_methods: List = []
2222
_excluded_methods = ["__setattr__", "__getattr__"]
2323

24+
# Stack to track method calls
25+
_call_stack: List = []
26+
2427

2528
def class_logger(decorated_cls):
2629
"""Decorator that adds logging functionality to each method of the class."""
@@ -38,10 +41,17 @@ def wrapper(*args, **kwargs):
3841
class_name = decorated_cls.__name__ # Access decorated class name
3942
api_method_name = str(method.__name__)
4043
full_method_name = f"{class_name.lower()}-{api_method_name}"
41-
# Track regular and "dunder" methods
42-
if api_method_name.startswith("__") or not api_method_name.startswith("_"):
44+
45+
# Track directly called methods
46+
if len(_call_stack) == 0:
4347
add_api_method(full_method_name)
44-
return method(*args, **kwargs)
48+
49+
_call_stack.append(full_method_name)
50+
51+
try:
52+
return method(*args, **kwargs)
53+
finally:
54+
_call_stack.pop()
4555

4656
return wrapper
4757

bigframes/session/_io/bigquery/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import textwrap
2424
import types
2525
import typing
26-
from typing import Dict, Iterable, Mapping, Optional, Sequence, Tuple, Union
26+
from typing import Dict, Iterable, Mapping, Optional, Tuple, Union
2727

2828
import bigframes_vendored.pandas.io.gbq as third_party_pandas_gbq
2929
import google.api_core.exceptions
@@ -43,11 +43,15 @@
4343

4444
def create_job_configs_labels(
4545
job_configs_labels: Optional[Dict[str, str]],
46-
api_methods: Sequence[str],
46+
api_methods: typing.List[str],
4747
) -> Dict[str, str]:
4848
if job_configs_labels is None:
4949
job_configs_labels = {}
5050

51+
if api_methods:
52+
job_configs_labels["bigframes-api"] = api_methods[0]
53+
del api_methods[0]
54+
5155
labels = list(
5256
itertools.chain(
5357
job_configs_labels.keys(),
@@ -198,10 +202,11 @@ def start_query_with_client(
198202
"""
199203
Starts query job and waits for results.
200204
"""
201-
api_methods = log_adapter.get_and_reset_api_methods()
202-
job_config.labels = create_job_configs_labels(
203-
job_configs_labels=job_config.labels, api_methods=api_methods
204-
)
205+
if not job_config.dry_run:
206+
api_methods = log_adapter.get_and_reset_api_methods()
207+
job_config.labels = create_job_configs_labels(
208+
job_configs_labels=job_config.labels, api_methods=api_methods
209+
)
205210

206211
try:
207212
query_job = bq_client.query(sql, job_config=job_config, timeout=timeout)

bigframes/session/_io/bigquery/read_gbq_table.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ def get_table_metadata(
112112
# atomically.
113113
table = bqclient.get_table(table_ref)
114114

115-
# TODO(b/336521938): Refactor to make sure we set the "bigframes-api"
116-
# whereever we execute a query.
117115
job_config = bigquery.QueryJobConfig()
118116
job_config.labels["bigframes-api"] = api_name
119117
snapshot_timestamp = list(
@@ -344,8 +342,6 @@ def get_time_travel_datetime_and_table_metadata(
344342
# atomically.
345343
table = bqclient.get_table(table_ref)
346344

347-
# TODO(b/336521938): Refactor to make sure we set the "bigframes-api"
348-
# whereever we execute a query.
349345
job_config = bigquery.QueryJobConfig()
350346
job_config.labels["bigframes-api"] = api_name
351347
snapshot_timestamp = list(

tests/unit/session/test_io_bigquery.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,32 @@ def test_create_job_configs_labels_is_none():
3030
labels = io_bq.create_job_configs_labels(
3131
job_configs_labels=None, api_methods=api_methods
3232
)
33-
expected_dict = {
34-
"recent-bigframes-api-0": "agg",
35-
"recent-bigframes-api-1": "series-mode",
36-
}
33+
expected_dict = {"bigframes-api": "agg", "recent-bigframes-api-0": "series-mode"}
3734
assert labels is not None
3835
assert labels == expected_dict
3936

4037

4138
def test_create_job_configs_labels_length_limit_not_met():
4239
cur_labels = {
43-
"bigframes-api": "read_pandas",
4440
"source": "bigquery-dataframes-temp",
4541
}
4642
api_methods = ["agg", "series-mode"]
4743
labels = io_bq.create_job_configs_labels(
4844
job_configs_labels=cur_labels, api_methods=api_methods
4945
)
5046
expected_dict = {
51-
"bigframes-api": "read_pandas",
5247
"source": "bigquery-dataframes-temp",
53-
"recent-bigframes-api-0": "agg",
54-
"recent-bigframes-api-1": "series-mode",
48+
"bigframes-api": "agg",
49+
"recent-bigframes-api-0": "series-mode",
5550
}
5651
assert labels is not None
57-
assert len(labels) == 4
52+
assert len(labels) == 3
5853
assert labels == expected_dict
5954

6055

6156
def test_create_job_configs_labels_log_adaptor_call_method_under_length_limit():
6257
log_adapter.get_and_reset_api_methods()
6358
cur_labels = {
64-
"bigframes-api": "read_pandas",
6559
"source": "bigquery-dataframes-temp",
6660
}
6761
df = bpd.DataFrame(
@@ -76,14 +70,10 @@ def test_create_job_configs_labels_log_adaptor_call_method_under_length_limit():
7670
job_configs_labels=cur_labels, api_methods=api_methods
7771
)
7872
expected_dict = {
79-
"bigframes-api": "read_pandas",
8073
"source": "bigquery-dataframes-temp",
81-
"recent-bigframes-api-0": "series-__init__",
82-
"recent-bigframes-api-1": "dataframe-max",
83-
"recent-bigframes-api-2": "dataframe-__init__",
84-
"recent-bigframes-api-3": "dataframe-head",
85-
"recent-bigframes-api-4": "dataframe-__init__",
86-
"recent-bigframes-api-5": "dataframe-__init__",
74+
"bigframes-api": "dataframe-max",
75+
"recent-bigframes-api-0": "dataframe-head",
76+
"recent-bigframes-api-1": "dataframe-__init__",
8777
}
8878
assert labels == expected_dict
8979

@@ -94,7 +84,7 @@ def test_create_job_configs_labels_length_limit_met_and_labels_is_none():
9484
{"col1": [1, 2], "col2": [3, 4]}, session=resources.create_bigquery_session()
9585
)
9686
# Test running methods more than the labels' length limit
97-
for i in range(66):
87+
for i in range(100):
9888
df.head()
9989
api_methods = log_adapter._api_methods
10090

@@ -112,7 +102,7 @@ def test_create_job_configs_labels_length_limit_met():
112102
"bigframes-api": "read_pandas",
113103
"source": "bigquery-dataframes-temp",
114104
}
115-
for i in range(60):
105+
for i in range(100):
116106
key = f"bigframes-api-test-{i}"
117107
value = f"test{i}"
118108
cur_labels[key] = value

0 commit comments

Comments
 (0)