diff --git a/monitoring/api/v3/custom_metric.py b/monitoring/api/v3/custom_metric.py index c1b2fc97740..9f420b1aafd 100644 --- a/monitoring/api/v3/custom_metric.py +++ b/monitoring/api/v3/custom_metric.py @@ -56,12 +56,12 @@ def get_now_rfc3339(): def create_custom_metric(client, project_id, - custom_metric_name, metric_kind): + custom_metric_type, metric_kind): """Create custom metric descriptor""" metrics_descriptor = { "name": "projects/{}/metricDescriptors/{}".format( - project_id, custom_metric_name), - "type": custom_metric_name, + project_id, custom_metric_type), + "type": custom_metric_type, "labels": [ { "key": "environment", @@ -80,11 +80,11 @@ def create_custom_metric(client, project_id, name=project_id, body=metrics_descriptor).execute() -def get_custom_metric(client, project_id, custom_metric_name): +def get_custom_metric(client, project_id, custom_metric_type): """Retrieve the custom metric we created""" request = client.projects().metricDescriptors().list( name=project_id, - filter='metric.type=starts_with("{}")'.format(custom_metric_name)) + filter='metric.type=starts_with("{}")'.format(custom_metric_type)) response = request.execute() print('ListCustomMetrics response:') pprint.pprint(response) @@ -103,14 +103,14 @@ def get_custom_data_point(): def write_timeseries_value(client, project_resource, - custom_metric_name, instance_id, metric_kind): + custom_metric_type, instance_id, metric_kind): """Write the custom metric obtained by get_custom_data_point at a point in time.""" # Specify a new data point for the time series. now = get_now_rfc3339() timeseries_data = { "metric": { - "type": custom_metric_name, + "type": custom_metric_type, "labels": { "environment": "STAGING" } @@ -142,7 +142,7 @@ def write_timeseries_value(client, project_resource, request.execute() -def read_timeseries(client, project_resource, custom_metric_name): +def read_timeseries(client, project_resource, custom_metric_type): """Reads all of the CUSTOM_METRICS that we have written between START_TIME and END_TIME :param project_resource: Resource of the project to read the timeseries @@ -151,7 +151,7 @@ def read_timeseries(client, project_resource, custom_metric_name): """ request = client.projects().timeSeries().list( name=project_resource, - filter='metric.type="{0}"'.format(custom_metric_name), + filter='metric.type="{0}"'.format(custom_metric_type), pageSize=3, interval_startTime=get_start_time(), interval_endTime=get_now_rfc3339()) @@ -163,28 +163,28 @@ def main(project_id): # This is the namespace for all custom metrics CUSTOM_METRIC_DOMAIN = "custom.googleapis.com" # This is our specific metric name - CUSTOM_METRIC_NAME = "{}/custom_measurement".format(CUSTOM_METRIC_DOMAIN) + CUSTOM_METRIC_TYPE = "{}/custom_measurement".format(CUSTOM_METRIC_DOMAIN) INSTANCE_ID = "test_instance" METRIC_KIND = "GAUGE" project_resource = "projects/{0}".format(project_id) client = list_resources.get_client() create_custom_metric(client, project_resource, - CUSTOM_METRIC_NAME, METRIC_KIND) + CUSTOM_METRIC_TYPE, METRIC_KIND) custom_metric = None while not custom_metric: # wait until it's created time.sleep(1) custom_metric = get_custom_metric( - client, project_resource, CUSTOM_METRIC_NAME) + client, project_resource, CUSTOM_METRIC_TYPE) write_timeseries_value(client, project_resource, - CUSTOM_METRIC_NAME, INSTANCE_ID, METRIC_KIND) + CUSTOM_METRIC_TYPE, INSTANCE_ID, METRIC_KIND) # Sometimes on new metric descriptors, writes have a delay in being read # back. 3 seconds should be enough to make sure our read call picks up the # write time.sleep(3) - timeseries = read_timeseries(client, project_resource, CUSTOM_METRIC_NAME) + timeseries = read_timeseries(client, project_resource, CUSTOM_METRIC_TYPE) print('read_timeseries response:\n{}'.format(pprint.pformat(timeseries))) diff --git a/monitoring/api/v3/custom_metric_test.py b/monitoring/api/v3/custom_metric_test.py index 7f3a3ecea9a..3d457de4d39 100644 --- a/monitoring/api/v3/custom_metric_test.py +++ b/monitoring/api/v3/custom_metric_test.py @@ -27,19 +27,9 @@ from custom_metric import read_timeseries, write_timeseries_value import list_resources -""" Change this to run against other prjoects - GOOGLE_APPLICATION_CREDENTIALS must be the service account for this project -""" - -# temporarily hard code to whitelisted project -TEST_PROJECT_ID = 'cloud-monitoring-dev' -# TEST_PROJECT_ID = os.getenv("GCLOUD_PROJECT", 'cloud-monitoring-dev') - -""" Custom metric domain for all cusotm metrics""" +""" Custom metric domain for all custom metrics""" CUSTOM_METRIC_DOMAIN = "custom.googleapis.com" -PROJECT_RESOURCE = "projects/{}".format(TEST_PROJECT_ID) - METRIC = 'compute.googleapis.com/instance/cpu/usage_time' METRIC_NAME = ''.join( random.choice('0123456789ABCDEF') for i in range(16)) @@ -47,7 +37,8 @@ CUSTOM_METRIC_DOMAIN, METRIC_NAME) -def test_custom_metric(): +def test_custom_metric(cloud_config): + PROJECT_RESOURCE = "projects/{}".format(cloud_config.project) client = list_resources.get_client() # Use a constant seed so psuedo random number is known ahead of time random.seed(1) diff --git a/monitoring/api/v3/list_resources_test.py b/monitoring/api/v3/list_resources_test.py index ed7b8f43706..3669aa9f2c2 100644 --- a/monitoring/api/v3/list_resources_test.py +++ b/monitoring/api/v3/list_resources_test.py @@ -24,14 +24,11 @@ import list_resources -# temporarily hard code to whitelisted project -TEST_PROJECT_ID = 'cloud-monitoring-dev' -# TEST_PROJECT_ID = os.getenv("GCLOUD_PROJECT", 'cloud-monitoring-dev') -PROJECT_RESOURCE = "projects/{}".format(TEST_PROJECT_ID) METRIC = 'compute.googleapis.com/instance/cpu/usage_time' -def test_list_monitored_resources(capsys): +def test_list_monitored_resources(cloud_config, capsys): + PROJECT_RESOURCE = "projects/{}".format(cloud_config.project) client = list_resources.get_client() list_resources.list_monitored_resource_descriptors( client, PROJECT_RESOURCE) @@ -41,7 +38,8 @@ def test_list_monitored_resources(capsys): assert regex.search(stdout) is not None -def test_list_metrics(capsys): +def test_list_metrics(cloud_config, capsys): + PROJECT_RESOURCE = "projects/{}".format(cloud_config.project) client = list_resources.get_client() list_resources.list_metric_descriptors( client, PROJECT_RESOURCE, METRIC) @@ -51,7 +49,8 @@ def test_list_metrics(capsys): assert regex.search(stdout) is not None -def test_list_timeseries(capsys): +def test_list_timeseries(cloud_config, capsys): + PROJECT_RESOURCE = "projects/{}".format(cloud_config.project) client = list_resources.get_client() list_resources.list_timeseries( client, PROJECT_RESOURCE, METRIC)