Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat: log most recent API calls as recent-bigframes-api-xx labels on BigQuery jobs #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 45 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c2009ed
docs: link to ML.EVALUATE BQML page for score() methods
ashleyxuu Oct 24, 2023
09ad5e4
feat: label query job with bigframes-api-xx using decorator
ashleyxuu Oct 25, 2023
4f4eb9b
reorganize the commit
ashleyxuu Oct 25, 2023
9ee937c
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Oct 26, 2023
272f0af
test: Log slowest tests durations (#146)
shobsi Oct 26, 2023
0e4c49c
docs: link to ML.EVALUATE BQML page for score() methods (#137)
ashleyxuu Oct 26, 2023
aad2c1a
feat: populate ibis version in user agent (#140)
ashleyxuu Oct 26, 2023
1043d6d
fix: don't override the global logging config (#138)
tswast Oct 26, 2023
1f49ef9
fix: use indexee's session for loc listlike cases (#152)
milkshakeiii Oct 26, 2023
c4c1e6e
feat: add pandas.qcut (#104)
TrevorBergeron Oct 26, 2023
4a27f44
feat: add unstack to series, add level param (#115)
TrevorBergeron Oct 26, 2023
fface57
feat: add `DataFrame.to_pandas_batches()` to download large `DataFram…
tswast Oct 26, 2023
bbc3c69
fix: resolve plotly rendering issue by using ipython html for job pro…
orrbradford Oct 26, 2023
a99d62c
refactor: ArrayValue is now a tree that defers conversion to ibis (#110)
TrevorBergeron Oct 27, 2023
f37d0b0
fix: fix bug with column names under repeated column assignment (#150)
milkshakeiii Oct 27, 2023
aba301c
test: refactor remote function tests (#147)
shobsi Oct 27, 2023
53bb2cd
feat: add dataframe melt (#116)
TrevorBergeron Oct 28, 2023
2bf4bcc
docs: add artithmetic df sample code (#153)
ashleyxuu Oct 30, 2023
343414a
feat: Implement operator `@` for `DataFrame.dot` (#139)
shobsi Oct 30, 2023
4eac10d
fix: fix typo and address comments
ashleyxuu Oct 30, 2023
868d2ad
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Oct 30, 2023
c03a8d9
Merge branch 'main' into ashleyxu-add-api-methods
tswast Nov 2, 2023
39321e4
fix: address comments
ashleyxuu Nov 3, 2023
aebcf11
Remove utils folder and refactor it in core directory
ashleyxuu Nov 3, 2023
72217c2
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Nov 3, 2023
ec526b5
Remove utils folder and refactor it in core directory
ashleyxuu Nov 3, 2023
9edfe31
Merge remote-tracking branch 'origin/ashleyxu-add-api-methods' into a…
ashleyxuu Nov 3, 2023
4baa373
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Nov 3, 2023
3a94c23
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 3, 2023
d84c569
fix merge conflicts
ashleyxuu Nov 3, 2023
308c9a7
Merge remote-tracking branch 'origin/ashleyxu-add-api-methods' into a…
ashleyxuu Nov 3, 2023
4618107
commit the conflicts
ashleyxuu Nov 13, 2023
a87bcb8
redesign the log adapter
ashleyxuu Nov 14, 2023
cf97f8b
resolve conflicts and merge remote-tracking branch 'origin/main' into…
ashleyxuu Nov 14, 2023
53a99f9
Make the global _api_methods and lock threads
ashleyxuu Nov 14, 2023
3cc3599
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Nov 14, 2023
1c3deb5
Make the global _api_methods and lock threads
ashleyxuu Nov 14, 2023
99f423b
merge conflicts
ashleyxuu Nov 14, 2023
115de27
address comments
ashleyxuu Nov 14, 2023
b0adf27
address comments
ashleyxuu Nov 14, 2023
b4ea9e3
Merge remote-tracking branch 'origin/ashleyxu-add-api-methods' into a…
ashleyxuu Nov 14, 2023
df9c9c0
fix error
ashleyxuu Nov 14, 2023
00bb6de
fix None job_config error
ashleyxuu Nov 14, 2023
36fea06
address comments
ashleyxuu Nov 14, 2023
e872d18
Merge branch 'main' into ashleyxu-add-api-methods
ashleyxuu Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 3 bigframes/core/groupby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pandas as pd

import bigframes.constants as constants
from bigframes.core import log_adapter
import bigframes.core as core
import bigframes.core.block_transforms as block_ops
import bigframes.core.blocks as blocks
Expand All @@ -33,6 +34,7 @@
import third_party.bigframes_vendored.pandas.core.groupby as vendored_pandas_groupby


@log_adapter.class_logger
class DataFrameGroupBy(vendored_pandas_groupby.DataFrameGroupBy):
__doc__ = vendored_pandas_groupby.GroupBy.__doc__

Expand Down Expand Up @@ -406,6 +408,7 @@ def _resolve_label(self, label: blocks.Label) -> str:
return col_ids[0]


@log_adapter.class_logger
class SeriesGroupBy(vendored_pandas_groupby.SeriesGroupBy):
__doc__ = vendored_pandas_groupby.GroupBy.__doc__

Expand Down
61 changes: 61 additions & 0 deletions 61 bigframes/core/log_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import threading
from typing import List

_lock = threading.Lock()
MAX_LABELS_COUNT = 64
_api_methods: List = []


def class_logger(decorated_cls):
"""Decorator that adds logging functionality to each method of the class."""
for attr_name, attr_value in decorated_cls.__dict__.items():
if callable(attr_value):
setattr(decorated_cls, attr_name, method_logger(attr_value))
return decorated_cls
ashleyxuu marked this conversation as resolved.
Show resolved Hide resolved


def method_logger(method):
"""Decorator that adds logging functionality to a method."""

@functools.wraps(method)
def wrapper(*args, **kwargs):
api_method_name = str(method.__name__)
# Track regular and "dunder" methods
if api_method_name.startswith("__") or not api_method_name.startswith("_"):
add_api_method(api_method_name)
return method(*args, **kwargs)

return wrapper


def add_api_method(api_method_name):
global _lock
global _api_methods
with _lock:
# Push the method to the front of the _api_methods list
_api_methods.insert(0, api_method_name)
# Keep the list length within the maximum limit (adjust MAX_LABELS_COUNT as needed)
_api_methods = _api_methods[:MAX_LABELS_COUNT]


def get_and_reset_api_methods():
global _lock
with _lock:
previous_api_methods = list(_api_methods)
_api_methods.clear()
return previous_api_methods
1 change: 1 addition & 0 deletions 1 bigframes/core/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from dataclasses import dataclass, field
Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/core/window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import typing

from bigframes.core import log_adapter
import bigframes.core as core
import bigframes.core.blocks as blocks
import bigframes.operations.aggregations as agg_ops
import third_party.bigframes_vendored.pandas.core.window.rolling as vendored_pandas_rolling


@log_adapter.class_logger
class Window(vendored_pandas_rolling.Window):
__doc__ = vendored_pandas_rolling.Window.__doc__

Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import bigframes._config.display_options as display_options
import bigframes.constants as constants
import bigframes.core
from bigframes.core import log_adapter
import bigframes.core.block_transforms as block_ops
import bigframes.core.blocks as blocks
import bigframes.core.groupby as groupby
Expand Down Expand Up @@ -81,6 +82,7 @@


# Inherits from pandas DataFrame so that we can use the same docstrings.
@log_adapter.class_logger
class DataFrame(vendored_pandas_frame.DataFrame):
__doc__ = vendored_pandas_frame.DataFrame.__doc__

Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/operations/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

from __future__ import annotations

from bigframes.core import log_adapter
import bigframes.operations as ops
import bigframes.operations.base
import bigframes.series as series
import third_party.bigframes_vendored.pandas.core.indexes.accessor as vendordt


@log_adapter.class_logger
class DatetimeMethods(
bigframes.operations.base.SeriesMethods, vendordt.DatetimeProperties
):
Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/operations/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import cast, Literal, Optional, Union

import bigframes.constants as constants
from bigframes.core import log_adapter
import bigframes.dataframe as df
import bigframes.operations as ops
import bigframes.operations.base
Expand All @@ -32,6 +33,7 @@
}


@log_adapter.class_logger
class StringMethods(bigframes.operations.base.SeriesMethods, vendorstr.StringMethods):
__doc__ = vendorstr.StringMethods.__doc__

Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/operations/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import ibis.expr.types as ibis_types

from bigframes.core import log_adapter
import bigframes.dataframe
import bigframes.operations
import bigframes.operations.base
Expand All @@ -38,6 +39,7 @@ def _as_ibis(self, x: ibis_types.Value):
return struct_value[name].name(name)


@log_adapter.class_logger
class StructAccessor(
bigframes.operations.base.SeriesMethods, vendoracessors.StructAccessor
):
Expand Down
2 changes: 2 additions & 0 deletions 2 bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import bigframes.constants as constants
import bigframes.core
from bigframes.core import log_adapter
import bigframes.core.block_transforms as block_ops
import bigframes.core.blocks as blocks
import bigframes.core.groupby as groupby
Expand All @@ -55,6 +56,7 @@
LevelsType = typing.Union[LevelType, typing.Sequence[LevelType]]


@log_adapter.class_logger
class Series(bigframes.operations.base.SeriesMethods, vendored_pandas_series.Series):
def __init__(self, *args, **kwargs):
self._query_job: Optional[bigquery.QueryJob] = None
Expand Down
7 changes: 7 additions & 0 deletions 7 bigframes/session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import bigframes._config.bigquery_options as bigquery_options
import bigframes.constants as constants
from bigframes.core import log_adapter
import bigframes.core as core
import bigframes.core.blocks as blocks
import bigframes.core.guid as guid
Expand Down Expand Up @@ -1347,6 +1348,10 @@ def _start_query(
Starts query job and waits for results.
"""
job_config = self._prepare_job_config(job_config)
api_methods = log_adapter.get_and_reset_api_methods()
job_config.labels = bigframes_io.create_job_configs_labels(
job_configs_labels=job_config.labels, api_methods=api_methods
)
query_job = self.bqclient.query(sql, job_config=job_config)

opts = bigframes.options.display
Expand Down Expand Up @@ -1381,6 +1386,8 @@ def _prepare_job_config(
) -> bigquery.QueryJobConfig:
if job_config is None:
job_config = self.bqclient.default_query_job_config
if job_config is None:
job_config = bigquery.QueryJobConfig()
if bigframes.options.compute.maximum_bytes_billed is not None:
job_config.maximum_bytes_billed = (
bigframes.options.compute.maximum_bytes_billed
Expand Down
21 changes: 20 additions & 1 deletion 21 bigframes/session/_io/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@
from __future__ import annotations

import datetime
import itertools
import textwrap
import types
from typing import Dict, Iterable, Optional, Union
from typing import Dict, Iterable, Optional, Sequence, Union
import uuid

import google.cloud.bigquery as bigquery

IO_ORDERING_ID = "bqdf_row_nums"
MAX_LABELS_COUNT = 64
TEMP_TABLE_PREFIX = "bqdf{date}_{random_id}"


def create_job_configs_labels(
job_configs_labels: Optional[Dict[str, str]],
api_methods: Sequence[str],
) -> Dict[str, str]:
if job_configs_labels is None:
job_configs_labels = {}

labels = list(
itertools.chain(
job_configs_labels.keys(),
(f"recent-bigframes-api-{i}" for i in range(len(api_methods))),
)
)
values = list(itertools.chain(job_configs_labels.values(), api_methods))
return dict(zip(labels[:MAX_LABELS_COUNT], values[:MAX_LABELS_COUNT]))


def create_export_csv_statement(
table_id: str, uri: str, field_delimiter: str, header: bool
) -> str:
Expand Down
60 changes: 60 additions & 0 deletions 60 tests/unit/core/test_log_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
ashleyxuu marked this conversation as resolved.
Show resolved Hide resolved

from bigframes.core import log_adapter

MAX_LABELS_COUNT = 64


@pytest.fixture
def test_instance():
# Create a simple class for testing
@log_adapter.class_logger
class TestClass:
def method1(self):
pass

def method2(self):
pass

return TestClass()


def test_method_logging(test_instance):
test_instance.method1()
test_instance.method2()

# Check if the methods were added to the _api_methods list
api_methods = log_adapter.get_and_reset_api_methods()
assert api_methods is not None
assert "method1" in api_methods
assert "method2" in api_methods


def test_add_api_method_limit(test_instance):
# Ensure that add_api_method correctly adds a method to _api_methods
for i in range(70):
test_instance.method2()
assert len(log_adapter._api_methods) == MAX_LABELS_COUNT


def test_get_and_reset_api_methods(test_instance):
# Ensure that get_and_reset_api_methods returns a copy and resets the list
test_instance.method1()
test_instance.method2()
previous_methods = log_adapter.get_and_reset_api_methods()
assert previous_methods is not None
assert log_adapter._api_methods == []
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.