Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 2616894

Browse files
committed
fixed lint
1 parent 9b2cd1c commit 2616894

8 files changed

Lines changed: 111 additions & 82 deletions

File tree

google/cloud/bigtable/data/_metrics/handlers/gcp_exporter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@
5353
# avoid reformatting into individual lines
5454
# fmt: off
5555
MILLIS_AGGREGATION = view.ExplicitBucketHistogramAggregation(
56-
[ 0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40,
57-
50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650,
58-
800, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000,
59-
200_000, 400_000, 800_000, 1_600_000, 3_200_000
56+
[
57+
0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40,
58+
50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650,
59+
800, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000,
60+
200_000, 400_000, 800_000, 1_600_000, 3_200_000
6061
]
6162
)
6263
# fmt: on

google/cloud/bigtable/data/_sync_autogen/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ def __init__(
776776
instance_id=instance_id,
777777
table_id=table_id,
778778
app_profile_id=app_profile_id,
779+
client_version=client._client_version(),
779780
)
780781
]
781782
)

tests/system/data/setup_fixtures.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import pytest
2020
import os
2121
import uuid
22-
import datetime
2322

2423

2524
@pytest.fixture(scope="session")

tests/system/data/test_metrics_async.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import os
1616
import pytest
1717
import uuid
18-
import datetime
1918

2019
from grpc import StatusCode
2120

@@ -26,11 +25,9 @@
2625
from google.cloud.bigtable.data._metrics.data_model import (
2726
CompletedOperationMetric,
2827
CompletedAttemptMetric,
29-
OperationType,
3028
)
3129
from google.cloud.bigtable.data.read_rows_query import ReadRowsQuery
3230
from google.cloud.bigtable_v2.types import ResponseParams
33-
from google.cloud.bigtable import __version__ as CLIENT_VERSION
3431

3532
from google.cloud.bigtable.data._cross_sync import CrossSync
3633

tests/system/data/test_system_autogen.py

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,62 +1081,43 @@ def test_execute_metadata_on_empty_response(
10811081
SqlType.Bytes(), SqlType.Int64()
10821082
)
10831083

1084+
@pytest.fixture(scope="session")
1085+
def metrics_client(self, client):
1086+
yield client._gcp_metrics_exporter.client
1087+
10841088
@pytest.mark.order("last")
1085-
class TestExportedMetrics(SystemTestRunner):
1086-
"""
1087-
Checks to make sure metrics were exported by tests
1088-
1089-
Runs at the end of test suite, to allow other tests to write metrics
1090-
"""
1091-
1092-
@pytest.fixture(scope="session")
1093-
def metrics_client(self, client):
1094-
yield client._gcp_metrics_exporter.client
1095-
1096-
@pytest.fixture(scope="session")
1097-
def time_interval(self, start_timestamp):
1098-
"""Build a time interval between when system tests started, and the exported metric tests
1099-
1100-
Optionally adds LOOKBACK_MINUTES value for testing"""
1101-
end_time = datetime.datetime.now(datetime.timezone.utc)
1102-
LOOKBACK_MINUTES = os.getenv("LOOKBACK_MINUTES")
1103-
if LOOKBACK_MINUTES is not None:
1104-
print(f"running with LOOKBACK_MINUTES={LOOKBACK_MINUTES}")
1105-
start_timestamp = start_timestamp - datetime.timedelta(
1106-
minutes=int(LOOKBACK_MINUTES)
1107-
)
1108-
return {"start_time": start_timestamp, "end_time": end_time}
1109-
1110-
@pytest.mark.parametrize(
1111-
"metric,methods",
1112-
[
1113-
("attempt_latencies", [m.value for m in OperationType]),
1114-
("operation_latencies", [m.value for m in OperationType]),
1115-
("retry_count", [m.value for m in OperationType]),
1116-
("first_response_latencies", [OperationType.READ_ROWS]),
1117-
("server_latencies", [m.value for m in OperationType]),
1118-
("connectivity_error_count", [m.value for m in OperationType]),
1119-
("application_blocking_latencies", [OperationType.READ_ROWS]),
1120-
],
1121-
)
1122-
@retry.Retry(predicate=retry.if_exception_type(AssertionError))
1123-
def test_metric_existence(
1124-
self, client, table_id, metrics_client, time_interval, metric, methods
1125-
):
1126-
"""Checks existence of each metric in Cloud Monitoring"""
1127-
from google.cloud.bigtable import __version__ as CLIENT_VERSION
1128-
1129-
for m in methods:
1130-
metric_filter = (
1131-
f'metric.type = "bigtable.googleapis.com/client/{metric}" '
1132-
+ f'AND metric.labels.client_name = "python-bigtable/{CLIENT_VERSION}" AND resource.labels.table = "{table_id}" '
1133-
)
1134-
results = list(
1135-
metrics_client.list_time_series(
1136-
name=f"projects/{client.project}",
1137-
filter=metric_filter,
1138-
interval=time_interval,
1139-
view=0,
1140-
)
1089+
@pytest.mark.parametrize(
1090+
"metric,methods",
1091+
[
1092+
("attempt_latencies", [m.value for m in OperationType]),
1093+
("operation_latencies", [m.value for m in OperationType]),
1094+
("retry_count", [m.value for m in OperationType]),
1095+
("first_response_latencies", [OperationType.READ_ROWS]),
1096+
("server_latencies", [m.value for m in OperationType]),
1097+
("connectivity_error_count", [m.value for m in OperationType]),
1098+
("application_blocking_latencies", [OperationType.READ_ROWS]),
1099+
],
1100+
)
1101+
@retry.Retry(predicate=retry.if_exception_type(AssertionError))
1102+
def test_metric_existence(
1103+
self, client, table_id, metrics_client, start_timestamp, metric, methods
1104+
):
1105+
"""Checks to make sure metrics were exported by tests
1106+
1107+
Runs at the end of test suite, to let other tests write metrics"""
1108+
end_timestamp = datetime.datetime.now(datetime.timezone.utc)
1109+
for m in methods:
1110+
metric_filter = (
1111+
f'metric.type = "bigtable.googleapis.com/client/{metric}" '
1112+
+ f'AND metric.labels.client_name = "python-bigtable/{client._client_version()}" '
1113+
+ f'AND resource.labels.table = "{table_id}" '
1114+
)
1115+
results = list(
1116+
metrics_client.list_time_series(
1117+
name=f"projects/{client.project}",
1118+
filter=metric_filter,
1119+
interval={"start_time": start_timestamp, "end_time": end_timestamp},
1120+
view=0,
11411121
)
1142-
assert len(results) > 0, f"No data found for {metric} {m}"
1122+
)
1123+
assert len(results) > 0, f"No data found for {metric} {m}"

tests/unit/data/_metrics/test_gcp_exporter_handler.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
ScopeMetrics,
2525
Metric,
2626
Sum,
27-
Histogram,
2827
AggregationTemporality,
2928
)
3029
from google.cloud.monitoring_v3 import (
@@ -46,7 +45,6 @@
4645

4746

4847
class TestGoogleCloudMetricsHandler:
49-
5048
def _make_one(self, *args, **kwargs):
5149
return GoogleCloudMetricsHandler(*args, **kwargs)
5250

@@ -62,7 +60,7 @@ def test_ctor_defaults(self):
6260
handler = self._make_one(
6361
expected_exporter,
6462
instance_id=expected_instance,
65-
table_id=expected_table
63+
table_id=expected_table,
6664
)
6765
assert isinstance(handler.meter_provider, MeterProvider)
6866
assert isinstance(handler.otel, _OpenTelemetryInstruments)
@@ -80,7 +78,6 @@ def test_ctor_explicit(self):
8078
expected_version = "my_version"
8179
expected_uid = "my_uid"
8280
expected_app_profile = "my_profile"
83-
expected_instruments = object()
8481
expected_exporter = BigtableMetricsExporter("project")
8582
handler = self._make_one(
8683
expected_exporter,
@@ -99,8 +96,6 @@ def test_ctor_explicit(self):
9996
)
10097
assert handler.shared_labels["client_uid"] == expected_uid
10198

102-
103-
10499
@mock.patch(
105100
"google.cloud.bigtable.data._metrics.handlers.gcp_exporter.PeriodicExportingMetricReader"
106101
)
@@ -188,13 +183,20 @@ def test__to_point_w_number(self, value, expected_field):
188183
expected_start_time_nanos = 100
189184
expected_end_time_nanos = 200
190185
dp = NumberDataPoint(
191-
attributes={}, start_time_unix_nano=expected_start_time_nanos, time_unix_nano=expected_end_time_nanos, value=value
186+
attributes={},
187+
start_time_unix_nano=expected_start_time_nanos,
188+
time_unix_nano=expected_end_time_nanos,
189+
value=value,
192190
)
193191
point = instance._to_point(dp)
194192
assert isinstance(point, Point)
195193
assert getattr(point.value, expected_field) == value
196-
assert (point.interval.start_time.second * 10**9) + point.interval.start_time.nanosecond == expected_start_time_nanos
197-
assert (point.interval.end_time.second * 10**9) + point.interval.end_time.nanosecond == expected_end_time_nanos
194+
assert (
195+
point.interval.start_time.second * 10**9
196+
) + point.interval.start_time.nanosecond == expected_start_time_nanos
197+
assert (
198+
point.interval.end_time.second * 10**9
199+
) + point.interval.end_time.nanosecond == expected_end_time_nanos
198200

199201
def test__to_point_w_histogram(self):
200202
"""Test that HistogramDataPoint is converted to a Point correctly."""
@@ -223,9 +225,16 @@ def test__to_point_w_histogram(self):
223225
assert dist.count == expected_count
224226
assert dist.mean == expected_sum / expected_count
225227
assert list(dist.bucket_counts) == expected_bucket_counts
226-
assert list(dist.bucket_options.explicit_buckets.bounds) == expected_explicit_bounds
227-
assert (point.interval.start_time.second * 10**9) + point.interval.start_time.nanosecond == expected_start_time_nanos
228-
assert (point.interval.end_time.second * 10**9) + point.interval.end_time.nanosecond == expected_end_time_nanos
228+
assert (
229+
list(dist.bucket_options.explicit_buckets.bounds)
230+
== expected_explicit_bounds
231+
)
232+
assert (
233+
point.interval.start_time.second * 10**9
234+
) + point.interval.start_time.nanosecond == expected_start_time_nanos
235+
assert (
236+
point.interval.end_time.second * 10**9
237+
) + point.interval.end_time.nanosecond == expected_end_time_nanos
229238

230239
def test__to_point_w_histogram_zero_count(self):
231240
"""Test that HistogramDataPoint with zero count is converted to a Point correctly."""
@@ -329,7 +338,9 @@ def test_export(self):
329338
is_monotonic=False,
330339
),
331340
)
332-
scope_metric = ScopeMetrics(scope=mock.Mock(), metrics=[metric], schema_url=None)
341+
scope_metric = ScopeMetrics(
342+
scope=mock.Mock(), metrics=[metric], schema_url=None
343+
)
333344
resource_metric = ResourceMetrics(
334345
resource=mock.Mock(), scope_metrics=[scope_metric], schema_url=None
335346
)
@@ -372,7 +383,9 @@ def test_export_no_attributes(self):
372383
is_monotonic=False,
373384
),
374385
)
375-
scope_metric = ScopeMetrics(scope=mock.Mock(), metrics=[metric], schema_url=None)
386+
scope_metric = ScopeMetrics(
387+
scope=mock.Mock(), metrics=[metric], schema_url=None
388+
)
376389
resource_metric = ResourceMetrics(
377390
resource=mock.Mock(), scope_metrics=[scope_metric], schema_url=None
378391
)
@@ -412,7 +425,9 @@ def test_exception_in_export(self):
412425
is_monotonic=False,
413426
),
414427
)
415-
scope_metric = ScopeMetrics(scope=mock.Mock(), metrics=[metric], schema_url=None)
428+
scope_metric = ScopeMetrics(
429+
scope=mock.Mock(), metrics=[metric], schema_url=None
430+
)
416431
resource_metric = ResourceMetrics(
417432
resource=mock.Mock(), scope_metrics=[scope_metric], schema_url=None
418433
)

tests/unit/data/_metrics/test_opentelemetry_handler.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from grpc import StatusCode
1818

1919
from google.cloud.bigtable.data._metrics.data_model import (
20-
DEFAULT_CLUSTER_ID,
21-
DEFAULT_ZONE,
2220
ActiveOperationMetric,
2321
CompletedAttemptMetric,
2422
CompletedOperationMetric,

tests/unit/data/_sync_autogen/test_client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def _make_client(cls, *args, use_emulator=True, **kwargs):
7979
return cls._get_target_class()(*args, **kwargs)
8080

8181
def test_ctor(self):
82+
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
83+
BigtableMetricsExporter,
84+
)
85+
8286
expected_project = "project-id"
8387
expected_credentials = AnonymousCredentials()
8488
client = self._make_client(
@@ -92,6 +96,8 @@ def test_ctor(self):
9296
assert isinstance(
9397
client._metrics_interceptor, CrossSync._Sync_Impl.MetricsInterceptor
9498
)
99+
assert client._gcp_metrics_exporter is not None
100+
assert isinstance(client._gcp_metrics_exporter, BigtableMetricsExporter)
95101
client.close()
96102

97103
def test_ctor_super_inits(self):
@@ -154,6 +160,31 @@ def test_ctor_dict_options(self):
154160
start_background_refresh.assert_called_once()
155161
client.close()
156162

163+
def test_metrics_exporter_init_shares_arguments(self):
164+
expected_credentials = AnonymousCredentials()
165+
expected_project = "custom_project"
166+
expected_options = client_options.ClientOptions()
167+
expected_options.credentials_file = None
168+
expected_options.quota_project_id = None
169+
with mock.patch(
170+
"google.cloud.bigtable.data._metrics.handlers.gcp_exporter.BigtableMetricsExporter.__init__",
171+
return_value=None,
172+
) as exporter_mock:
173+
with self._make_client(
174+
project=expected_project,
175+
credentials=expected_credentials,
176+
client_options=expected_options,
177+
):
178+
exporter_mock.assert_called_once_with(
179+
project_id=expected_project,
180+
credentials=expected_credentials,
181+
client_options=expected_options,
182+
)
183+
184+
def test_metrics_exporter_init_implicit_project(self):
185+
with self._make_client() as client:
186+
assert client._gcp_metrics_exporter.project_id == client.project
187+
157188
def test_veneer_grpc_headers(self):
158189
client_component = "data-async" if CrossSync._Sync_Impl.is_async else "data"
159190
VENEER_HEADER_REGEX = re.compile(
@@ -941,6 +972,7 @@ def test_ctor(self):
941972
from google.cloud.bigtable.data._metrics import (
942973
BigtableClientSideMetricsController,
943974
)
975+
from google.cloud.bigtable.data._metrics import GoogleCloudMetricsHandler
944976

945977
expected_table_id = "table-id"
946978
expected_instance_id = "instance-id"
@@ -982,6 +1014,8 @@ def test_ctor(self):
9821014
assert instance_key in client._active_instances
9831015
assert client._instance_owners[instance_key] == {id(table)}
9841016
assert isinstance(table._metrics, BigtableClientSideMetricsController)
1017+
assert len(table._metrics.handlers) == 1
1018+
assert isinstance(table._metrics.handlers[0], GoogleCloudMetricsHandler)
9851019
assert table.default_operation_timeout == expected_operation_timeout
9861020
assert table.default_attempt_timeout == expected_attempt_timeout
9871021
assert (
@@ -1201,6 +1235,7 @@ def test_ctor(self):
12011235
from google.cloud.bigtable.data._helpers import _WarmedInstanceKey
12021236
from google.cloud.bigtable.data._metrics import (
12031237
BigtableClientSideMetricsController,
1238+
GoogleCloudMetricsHandler,
12041239
)
12051240

12061241
expected_table_id = "table-id"
@@ -1250,6 +1285,8 @@ def test_ctor(self):
12501285
assert instance_key in client._active_instances
12511286
assert client._instance_owners[instance_key] == {id(view)}
12521287
assert isinstance(view._metrics, BigtableClientSideMetricsController)
1288+
assert len(view._metrics.handlers) == 1
1289+
assert isinstance(view._metrics.handlers[0], GoogleCloudMetricsHandler)
12531290
assert view.default_operation_timeout == expected_operation_timeout
12541291
assert view.default_attempt_timeout == expected_attempt_timeout
12551292
assert (

0 commit comments

Comments
 (0)