From 1f7b17b39451e1d57b0047b9abb06a836380b668 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 28 May 2020 19:15:23 +0000 Subject: [PATCH 1/3] [monitoring] fix: use the same random value for retry fixes #3875 --- monitoring/api/v3/api-client/custom_metric.py | 7 +++++++ monitoring/api/v3/api-client/custom_metric_test.py | 9 +++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/monitoring/api/v3/api-client/custom_metric.py b/monitoring/api/v3/api-client/custom_metric.py index 6cf83980357..35c591aefcc 100644 --- a/monitoring/api/v3/api-client/custom_metric.py +++ b/monitoring/api/v3/api-client/custom_metric.py @@ -30,6 +30,7 @@ # [START all] import argparse import datetime +import os import pprint import random import time @@ -103,6 +104,12 @@ def get_custom_data_point(): """Dummy method to return a mock measurement for demonstration purposes. Returns a random number between 0 and 10""" length = random.randint(0, 10) + # [END all] + # Just in case, terminate the region tag. + # If there's an envvar "MONITORING_TEST_REPORT_VALUE" is set, use it. + if 'MONITORING_TEST_REPORT_VALUE' in os.environ: + length = int(os.environ.get('MONITORING_TEST_REPORT_VALUE')) + # [START all] print("reporting timeseries value {}".format(str(length))) return length diff --git a/monitoring/api/v3/api-client/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py index 1a80408c4ad..51b8f296866 100644 --- a/monitoring/api/v3/api-client/custom_metric_test.py +++ b/monitoring/api/v3/api-client/custom_metric_test.py @@ -80,11 +80,8 @@ def custom_metric(client): def test_custom_metric(client, custom_metric): - # Use a constant seed so psuedo random number is known ahead of time - random.seed(1) pseudo_random_value = random.randint(0, 10) - # Reseed it - random.seed(1) + os.environ['MONITORING_TEST_REPORT_VALUE'] = str(pseudo_random_value) INSTANCE_ID = "test_instance" @@ -107,7 +104,7 @@ def eventually_consistent_test(): assert 'timeSeries' in response value = int( response['timeSeries'][0]['points'][0]['value']['int64Value']) - # using seed of 1 will create a value of 1 - assert value == pseudo_random_value + # We override the report value with MONITORING_TEST_REPORT_VALUE env var + assert pseudo_random_value == value eventually_consistent_test() From e12e7c7633e21600edeb0e4ca7f2b0e6ee204f1f Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 28 May 2020 20:14:11 +0000 Subject: [PATCH 2/3] Just reseed in `write_value()` --- monitoring/api/v3/api-client/custom_metric.py | 7 ------- monitoring/api/v3/api-client/custom_metric_test.py | 6 +++++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/monitoring/api/v3/api-client/custom_metric.py b/monitoring/api/v3/api-client/custom_metric.py index 35c591aefcc..6cf83980357 100644 --- a/monitoring/api/v3/api-client/custom_metric.py +++ b/monitoring/api/v3/api-client/custom_metric.py @@ -30,7 +30,6 @@ # [START all] import argparse import datetime -import os import pprint import random import time @@ -104,12 +103,6 @@ def get_custom_data_point(): """Dummy method to return a mock measurement for demonstration purposes. Returns a random number between 0 and 10""" length = random.randint(0, 10) - # [END all] - # Just in case, terminate the region tag. - # If there's an envvar "MONITORING_TEST_REPORT_VALUE" is set, use it. - if 'MONITORING_TEST_REPORT_VALUE' in os.environ: - length = int(os.environ.get('MONITORING_TEST_REPORT_VALUE')) - # [START all] print("reporting timeseries value {}".format(str(length))) return length diff --git a/monitoring/api/v3/api-client/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py index 51b8f296866..608554f3d97 100644 --- a/monitoring/api/v3/api-client/custom_metric_test.py +++ b/monitoring/api/v3/api-client/custom_metric_test.py @@ -80,14 +80,18 @@ def custom_metric(client): def test_custom_metric(client, custom_metric): + # Use a constant seed so psuedo random number is known ahead of time + random.seed(1) pseudo_random_value = random.randint(0, 10) - os.environ['MONITORING_TEST_REPORT_VALUE'] = str(pseudo_random_value) INSTANCE_ID = "test_instance" # It's rare, but write can fail with HttpError 500, so we retry. @backoff.on_exception(backoff.expo, HttpError, max_time=120) def write_value(): + # Reseed it to make sure the sample code will pick the same + # value. + random.seed(1) write_timeseries_value(client, PROJECT_RESOURCE, METRIC_RESOURCE, INSTANCE_ID, METRIC_KIND) From 24c166a22216b069608e42eadef8c30638357170 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 28 May 2020 20:16:14 +0000 Subject: [PATCH 3/3] revert comment --- monitoring/api/v3/api-client/custom_metric_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/api/v3/api-client/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py index 608554f3d97..e2daa096413 100644 --- a/monitoring/api/v3/api-client/custom_metric_test.py +++ b/monitoring/api/v3/api-client/custom_metric_test.py @@ -108,7 +108,7 @@ def eventually_consistent_test(): assert 'timeSeries' in response value = int( response['timeSeries'][0]['points'][0]['value']['int64Value']) - # We override the report value with MONITORING_TEST_REPORT_VALUE env var + # using seed of 1 will create a value of 1 assert pseudo_random_value == value eventually_consistent_test()