3232
3333from .test_system import _create_row_and_mutation , init_table_id , column_family_config , cluster_config , temp_rows , TEST_FAMILY , TEST_CLUSTER , TEST_ZONE
3434
35+ # use this value to make sure we are testing against a consistent test of metrics
36+ # if _populate_calls is modified, this value will have to change
37+ EXPECTED_METRIC_COUNT = 172
38+
3539
3640async def _populate_calls (client , instance_id , table_id ):
3741 """
@@ -75,16 +79,13 @@ def _get_stubs_for_table(table):
7579 }
7680 return rpc_stubs
7781
78- num_calls = 0
79-
8082 # Call each rpc with no errors. Should be successful.
8183 print ("populating successful rpcs..." )
8284 async with table_with_profile ("success" ) as table :
8385 stubs = _get_stubs_for_table (table )
8486 for stub_list in stubs .values ():
8587 for stub in stub_list :
8688 await stub ()
87- num_calls += 1
8889
8990 # Call each rpc with a terminal exception. Does not hit gcp servers
9091 print ("populating terminal NotFound rpcs..." )
@@ -95,7 +96,6 @@ def _get_stubs_for_table(table):
9596 with mock .patch (f"google.cloud.bigtable_v2.BigtableAsyncClient.{ rpc_name } " , side_effect = NotFound ("test" )):
9697 with pytest .raises ((NotFound , MutationsExceptionGroup )):
9798 await stub ()
98- num_calls += 1
9999
100100 non_retryable_rpcs = ["read_modify_write_row" , "check_and_mutate_row" ]
101101 # Calls hit retryable errors, then succeed
@@ -116,7 +116,6 @@ def side_effect(*args, **kwargs):
116116 return true_fn (table .client ._gapic_client , * args , ** kwargs )
117117 with mock .patch (f"google.cloud.bigtable_v2.BigtableAsyncClient.{ rpc_name } " , side_effect = side_effect ):
118118 await stub (retryable_errors = (ServiceUnavailable ,))
119- num_calls += 1
120119
121120 # Calls hit retryable errors, then hit deadline
122121 print ("populating retryable timeout rpcs..." )
@@ -127,7 +126,6 @@ def side_effect(*args, **kwargs):
127126 for stub in stub_list :
128127 with pytest .raises ((DeadlineExceeded , MutationsExceptionGroup )):
129128 await stub (operation_timeout = 0.5 , retryable_errors = (ServiceUnavailable ,))
130- num_calls += 1
131129
132130 # Calls hit retryable errors, then hit terminal exception
133131 print ("populating retryable then terminal error rpcs..." )
@@ -139,8 +137,6 @@ def side_effect(*args, **kwargs):
139137 with mock .patch (f"google.cloud.bigtable_v2.BigtableAsyncClient.{ rpc_name } " , side_effect = error_list ):
140138 with pytest .raises ((NotFound , MutationsExceptionGroup )):
141139 await stub (retryable_errors = (ServiceUnavailable ,))
142- num_calls += 1
143- return num_calls
144140
145141
146142@pytest_asyncio .fixture (scope = "session" )
@@ -150,7 +146,7 @@ async def get_all_metrics(client, instance_id, table_id, project_id):
150146 # populate table with metrics
151147 start_time = Timestamp ()
152148 start_time .GetCurrentTime ()
153- total_rpcs = await _populate_calls (client , instance_id , table_id )
149+ await _populate_calls (client , instance_id , table_id )
154150
155151 # read them down and save to a list. Retry until ready
156152 @retry .Retry (predicate = retry .if_exception_type (NotFound ), maximum = 5 , timeout = 2 * 60 )
@@ -170,7 +166,6 @@ def _read_metrics():
170166 "retry_count" ,
171167 "connectivity_error_count"
172168 ]
173- expected_count = len (all_instruments ) * total_rpcs
174169
175170 for instrument in all_instruments :
176171 response = client .list_time_series (
@@ -185,35 +180,36 @@ def _read_metrics():
185180 print (f"no data for { instrument } " )
186181 raise NotFound ("No metrics found" )
187182 all_responses .extend (response )
188- if len (all_responses ) < expected_count :
189- print (f"Only found { len (all_responses )} of { expected_count } metrics. Retrying..." )
183+ if len (all_responses ) < EXPECTED_METRIC_COUNT :
184+ print (f"Only found { len (all_responses )} of { EXPECTED_METRIC_COUNT } metrics. Retrying..." )
190185 raise NotFound ("Not all metrics found" )
191- elif len (all_responses ) > expected_count :
192- raise ValueError ("Found more metrics than expected" )
186+ elif len (all_responses ) > EXPECTED_METRIC_COUNT :
187+ raise ValueError (f "Found more metrics than expected: { len ( all_responses ) } " )
193188 return all_responses
194189 print ("waiting for metrics to be ready..." )
195190 metrics = _read_metrics ()
196191 print ("metrics ready" )
197192 return metrics
198193
194+
199195@pytest .mark .asyncio
200196async def test_resource (get_all_metrics , instance_id , table_id , project_id ):
201197 """
202198 all metrics should have monitored resource populated consistently
203199 """
204- breakpoint ()
205- for metric in get_all_metrics :
206- assert metric . resource .type == "bigtable_table"
207- assert len (metric . resource .labels ) == 5
208- assert metric . resource .labels ["instance" ] == instance_id
209- assert metric . resource .labels ["table" ] == table_id
210- assert metric . resource .labels ["project_id" ] == project_id
211- if 'success' in metric .labels ["app_profile_id" ]:
200+ for m in get_all_metrics :
201+ resource = m . resource
202+ assert resource .type == "bigtable_table"
203+ assert len (resource .labels ) == 5
204+ assert resource .labels ["instance" ] == instance_id
205+ assert resource .labels ["table" ] == table_id
206+ assert resource .labels ["project_id" ] == project_id
207+ if 'success' in m . metric .labels ["app_profile_id" ]:
212208 # for attempts that succeeded, zone and cluster should be populated
213- assert metric . resource .labels ["zone" ] == TEST_ZONE
214- assert metric . resource .labels ["cluster" ] == TEST_CLUSTER
209+ assert resource .labels ["zone" ] == TEST_ZONE
210+ assert resource .labels ["cluster" ] == TEST_CLUSTER
215211 else :
216212 # others should fall back to defaults
217- assert metric . resource .labels ["zone" ] == "unspecified"
218- assert metric . resource .labels ["cluster" ] == "global"
213+ assert resource .labels ["zone" ] == "unspecified"
214+ assert resource .labels ["cluster" ] == "global"
219215
0 commit comments