2424import random
2525import time
2626
27- from gcp_devrel .testing import eventually_consistent
28- from flaky import flaky
27+ import backoff
2928import googleapiclient .discovery
3029import pytest
30+ from googleapiclient .errors import HttpError
3131
3232from custom_metric import create_custom_metric
3333from custom_metric import delete_metric_descriptor
3434from custom_metric import get_custom_metric
3535from custom_metric import read_timeseries
3636from custom_metric import write_timeseries_value
3737
38+
3839PROJECT = os .environ ['GCLOUD_PROJECT' ]
3940
4041""" Custom metric domain for all custom metrics"""
@@ -52,7 +53,6 @@ def client():
5253 return googleapiclient .discovery .build ('monitoring' , 'v3' )
5354
5455
55- @flaky
5656def test_custom_metric (client ):
5757 PROJECT_RESOURCE = "projects/{}" .format (PROJECT )
5858 # Use a constant seed so psuedo random number is known ahead of time
@@ -64,29 +64,42 @@ def test_custom_metric(client):
6464 INSTANCE_ID = "test_instance"
6565 METRIC_KIND = "GAUGE"
6666
67- custom_metric_descriptor = create_custom_metric (
68- client , PROJECT_RESOURCE , METRIC_RESOURCE , METRIC_KIND )
69-
70- # wait until metric has been created, use the get call to wait until
71- # a response comes back with the new metric
72- custom_metric = None
73- while not custom_metric :
74- time .sleep (1 )
75- custom_metric = get_custom_metric (
76- client , PROJECT_RESOURCE , METRIC_RESOURCE )
77-
78- write_timeseries_value (client , PROJECT_RESOURCE ,
79- METRIC_RESOURCE , INSTANCE_ID ,
80- METRIC_KIND )
81-
82- # Sometimes on new metric descriptors, writes have a delay in being
83- # read back. Use eventually_consistent to account for this.
84- @eventually_consistent .call
85- def _ ():
86- response = read_timeseries (client , PROJECT_RESOURCE , METRIC_RESOURCE )
87- value = int (
88- response ['timeSeries' ][0 ]['points' ][0 ]['value' ]['int64Value' ])
89- # using seed of 1 will create a value of 1
90- assert value == pseudo_random_value
91-
92- delete_metric_descriptor (client , custom_metric_descriptor ['name' ])
67+ try :
68+ custom_metric_descriptor = create_custom_metric (
69+ client , PROJECT_RESOURCE , METRIC_RESOURCE , METRIC_KIND )
70+
71+ # wait until metric has been created, use the get call to wait until
72+ # a response comes back with the new metric with 10 retries.
73+ custom_metric = None
74+ retry_count = 0
75+ while not custom_metric and retry_count < 10 :
76+ time .sleep (1 )
77+ retry_count += 1
78+ custom_metric = get_custom_metric (
79+ client , PROJECT_RESOURCE , METRIC_RESOURCE )
80+ # Make sure we get the custom metric
81+ assert custom_metric
82+
83+ write_timeseries_value (client , PROJECT_RESOURCE ,
84+ METRIC_RESOURCE , INSTANCE_ID ,
85+ METRIC_KIND )
86+
87+ # Sometimes on new metric descriptors, writes have a delay in being
88+ # read back. Use eventually_consistent to account for this.
89+ @backoff .on_exception (
90+ backoff .expo , (AssertionError , HttpError ), max_time = 120 )
91+ def eventually_consistent_test ():
92+ response = read_timeseries (
93+ client , PROJECT_RESOURCE , METRIC_RESOURCE )
94+ # Make sure the value is not empty.
95+ assert 'timeSeries' in response
96+ value = int (
97+ response ['timeSeries' ][0 ]['points' ][0 ]['value' ]['int64Value' ])
98+ # using seed of 1 will create a value of 1
99+ assert value == pseudo_random_value
100+
101+ eventually_consistent_test ()
102+
103+ finally :
104+ # cleanup
105+ delete_metric_descriptor (client , custom_metric_descriptor ['name' ])
0 commit comments