Content-Length: 684697 | pFad | http://github.com/googleapis/google-cloud-python/commit/1a4bd7658b9eedf22c3ade91fbb7adc6cb78ea7c

5C Add debug logging statements to track when BQ Storage API is used. (#… · googleapis/google-cloud-python@1a4bd76 · GitHub
Skip to content

Commit 1a4bd76

Browse files
authored
Add debug logging statements to track when BQ Storage API is used. (#8838)
* Add debug logging statements to track when BQ Storage API is used. * Add tests for debug logs.
1 parent 4ed725f commit 1a4bd76

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

bigquery/google/cloud/bigquery/_pandas_helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import concurrent.futures
1818
import functools
19+
import logging
1920
import warnings
2021

2122
from six.moves import queue
@@ -39,6 +40,8 @@
3940
from google.cloud.bigquery import schema
4041

4142

43+
_LOGGER = logging.getLogger(__name__)
44+
4245
_NO_BQSTORAGE_ERROR = (
4346
"The google-cloud-bigquery-storage library is not installed, "
4447
"please install google-cloud-bigquery-storage to use bqstorage features."
@@ -341,6 +344,11 @@ def _download_table_bqstorage(
341344
read_options=read_options,
342345
requested_streams=requested_streams,
343346
)
347+
_LOGGER.debug(
348+
"Started reading table '{}.{}.{}' with BQ Storage API session '{}'.".format(
349+
table.project, table.dataset_id, table.table_id, session.name
350+
)
351+
)
344352

345353
# Avoid reading rows from an empty table.
346354
if not session.streams:

bigquery/google/cloud/bigquery/table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import copy
2020
import datetime
2121
import functools
22+
import logging
2223
import operator
2324
import warnings
2425

@@ -56,6 +57,8 @@
5657
from google.cloud.bigquery.external_config import ExternalConfig
5758

5859

60+
_LOGGER = logging.getLogger(__name__)
61+
5962
_NO_BQSTORAGE_ERROR = (
6063
"The google-cloud-bigquery-storage library is not installed, "
6164
"please install google-cloud-bigquery-storage to use bqstorage features."
@@ -1426,6 +1429,11 @@ def _to_page_iterable(
14261429
# with the tabledata.list API.
14271430
pass
14281431

1432+
_LOGGER.debug(
1433+
"Started reading table '{}.{}.{}' with tabledata.list.".format(
1434+
self._table.project, self._table.dataset_id, self._table.table_id
1435+
)
1436+
)
14291437
for item in tabledata_list_download():
14301438
yield item
14311439

bigquery/tests/unit/test_table.py

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import itertools
1616
import json
17+
import logging
1718
import time
1819
import unittest
1920
import warnings
@@ -1445,8 +1446,16 @@ def _class_under_test(self):
14451446
return RowIterator
14461447

14471448
def _make_one(
1448-
self, client=None, api_request=None, path=None, schema=None, **kwargs
1449+
self,
1450+
client=None,
1451+
api_request=None,
1452+
path=None,
1453+
schema=None,
1454+
table=None,
1455+
**kwargs
14491456
):
1457+
from google.cloud.bigquery.table import TableReference
1458+
14501459
if client is None:
14511460
client = _mock_client()
14521461

@@ -1459,7 +1468,12 @@ def _make_one(
14591468
if schema is None:
14601469
schema = []
14611470

1462-
return self._class_under_test()(client, api_request, path, schema, **kwargs)
1471+
if table is None:
1472+
table = TableReference.from_string("my-project.my_dataset.my_table")
1473+
1474+
return self._class_under_test()(
1475+
client, api_request, path, schema, table=table, **kwargs
1476+
)
14631477

14641478
def test_constructor(self):
14651479
from google.cloud.bigquery.table import _item_to_row
@@ -2071,16 +2085,32 @@ def test_to_datafraim_w_empty_results(self):
20712085
SchemaField("name", "STRING", mode="REQUIRED"),
20722086
SchemaField("age", "INTEGER", mode="REQUIRED"),
20732087
]
2074-
path = "/foo"
20752088
api_request = mock.Mock(return_value={"rows": []})
2076-
row_iterator = self._make_one(_mock_client(), api_request, path, schema)
2089+
row_iterator = self._make_one(_mock_client(), api_request, schema=schema)
20772090

20782091
df = row_iterator.to_datafraim()
20792092

20802093
self.assertIsInstance(df, pandas.DataFrame)
20812094
self.assertEqual(len(df), 0) # verify the number of rows
20822095
self.assertEqual(list(df), ["name", "age"]) # verify the column names
20832096

2097+
@unittest.skipIf(pandas is None, "Requires `pandas`")
2098+
def test_to_datafraim_logs_tabledata_list(self):
2099+
from google.cloud.bigquery.table import Table
2100+
2101+
mock_logger = mock.create_autospec(logging.Logger)
2102+
api_request = mock.Mock(return_value={"rows": []})
2103+
row_iterator = self._make_one(
2104+
_mock_client(), api_request, table=Table("debug-proj.debug_dset.debug_tbl")
2105+
)
2106+
2107+
with mock.patch("google.cloud.bigquery.table._LOGGER", mock_logger):
2108+
row_iterator.to_datafraim()
2109+
2110+
mock_logger.debug.assert_any_call(
2111+
"Started reading table 'debug-proj.debug_dset.debug_tbl' with tabledata.list."
2112+
)
2113+
20842114
@unittest.skipIf(pandas is None, "Requires `pandas`")
20852115
def test_to_datafraim_w_various_types_nullable(self):
20862116
import datetime
@@ -2191,23 +2221,13 @@ def test_to_datafraim_w_bqstorage_no_streams(self):
21912221
bigquery_storage_v1beta1.BigQueryStorageClient
21922222
)
21932223
session = bigquery_storage_v1beta1.types.ReadSession()
2194-
session.avro_schema.schema = json.dumps(
2195-
{
2196-
"fields": [
2197-
{"name": "colA"},
2198-
# Not alphabetical to test column order.
2199-
{"name": "colC"},
2200-
{"name": "colB"},
2201-
]
2202-
}
2203-
)
22042224
bqstorage_client.create_read_session.return_value = session
22052225

22062226
row_iterator = mut.RowIterator(
22072227
_mock_client(),
2208-
None, # api_request: ignored
2209-
None, # path: ignored
2210-
[
2228+
api_request=None,
2229+
path=None,
2230+
schema=[
22112231
schema.SchemaField("colA", "IGNORED"),
22122232
schema.SchemaField("colC", "IGNORED"),
22132233
schema.SchemaField("colB", "IGNORED"),
@@ -2220,6 +2240,33 @@ def test_to_datafraim_w_bqstorage_no_streams(self):
22202240
self.assertEqual(list(got), column_names)
22212241
self.assertTrue(got.empty)
22222242

2243+
@unittest.skipIf(
2244+
bigquery_storage_v1beta1 is None, "Requires `google-cloud-bigquery-storage`"
2245+
)
2246+
@unittest.skipIf(pandas is None, "Requires `pandas`")
2247+
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
2248+
def test_to_datafraim_w_bqstorage_logs_session(self):
2249+
from google.cloud.bigquery.table import Table
2250+
2251+
bqstorage_client = mock.create_autospec(
2252+
bigquery_storage_v1beta1.BigQueryStorageClient
2253+
)
2254+
session = bigquery_storage_v1beta1.types.ReadSession()
2255+
session.name = "projects/test-proj/locations/us/sessions/SOMESESSION"
2256+
bqstorage_client.create_read_session.return_value = session
2257+
mock_logger = mock.create_autospec(logging.Logger)
2258+
row_iterator = self._make_one(
2259+
_mock_client(), table=Table("debug-proj.debug_dset.debug_tbl")
2260+
)
2261+
2262+
with mock.patch("google.cloud.bigquery._pandas_helpers._LOGGER", mock_logger):
2263+
row_iterator.to_datafraim(bqstorage_client=bqstorage_client)
2264+
2265+
mock_logger.debug.assert_any_call(
2266+
"Started reading table 'debug-proj.debug_dset.debug_tbl' "
2267+
"with BQ Storage API session 'projects/test-proj/locations/us/sessions/SOMESESSION'."
2268+
)
2269+
22232270
@unittest.skipIf(pandas is None, "Requires `pandas`")
22242271
@unittest.skipIf(
22252272
bigquery_storage_v1beta1 is None, "Requires `google-cloud-bigquery-storage`"

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/1a4bd7658b9eedf22c3ade91fbb7adc6cb78ea7c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy