24
24
import random
25
25
import time
26
26
27
- from gcp_devrel .testing import eventually_consistent
28
- from flaky import flaky
27
+ import backoff
29
28
import googleapiclient .discovery
30
29
import pytest
30
+ from googleapiclient .errors import HttpError
31
31
32
32
from custom_metric import create_custom_metric
33
33
from custom_metric import delete_metric_descriptor
34
34
from custom_metric import get_custom_metric
35
35
from custom_metric import read_timeseries
36
36
from custom_metric import write_timeseries_value
37
37
38
+
38
39
PROJECT = os .environ ['GCLOUD_PROJECT' ]
39
40
40
41
""" Custom metric domain for all custom metrics"""
@@ -52,7 +53,6 @@ def client():
52
53
return googleapiclient .discovery .build ('monitoring' , 'v3' )
53
54
54
55
55
- @flaky
56
56
def test_custom_metric (client ):
57
57
PROJECT_RESOURCE = "projects/{}" .format (PROJECT )
58
58
# Use a constant seed so psuedo random number is known ahead of time
@@ -64,29 +64,42 @@ def test_custom_metric(client):
64
64
INSTANCE_ID = "test_instance"
65
65
METRIC_KIND = "GAUGE"
66
66
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