From 414b6099f4352763028539fc7ecb9998738934f5 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Wed, 1 Feb 2017 15:40:49 -0800 Subject: [PATCH] Cleanup metric descriptors [(#776)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/776) --- samples/snippets/v3/custom_metric.py | 9 ++++++++- samples/snippets/v3/custom_metric_test.py | 12 +++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/samples/snippets/v3/custom_metric.py b/samples/snippets/v3/custom_metric.py index 50153ab9..20f7722c 100644 --- a/samples/snippets/v3/custom_metric.py +++ b/samples/snippets/v3/custom_metric.py @@ -74,10 +74,17 @@ def create_custom_metric(client, project_id, "displayName": "Custom Metric" } - client.projects().metricDescriptors().create( + return client.projects().metricDescriptors().create( name=project_id, body=metrics_descriptor).execute() +def delete_metric_descriptor( + client, custom_metric_name): + """Delete a custom metric descriptor.""" + client.projects().metricDescriptors().delete( + name=custom_metric_name).execute() + + def get_custom_metric(client, project_id, custom_metric_type): """Retrieve the custom metric we created""" request = client.projects().metricDescriptors().list( diff --git a/samples/snippets/v3/custom_metric_test.py b/samples/snippets/v3/custom_metric_test.py index 6fdfd635..c2c72bb7 100644 --- a/samples/snippets/v3/custom_metric_test.py +++ b/samples/snippets/v3/custom_metric_test.py @@ -26,8 +26,12 @@ from gcp.testing import eventually_consistent from gcp.testing.flaky import flaky -from custom_metric import create_custom_metric, get_custom_metric -from custom_metric import read_timeseries, write_timeseries_value +from custom_metric import create_custom_metric +from custom_metric import delete_metric_descriptor +from custom_metric import get_custom_metric +from custom_metric import read_timeseries +from custom_metric import write_timeseries_value + import list_resources """ Custom metric domain for all custom metrics""" @@ -53,7 +57,7 @@ def test_custom_metric(cloud_config): INSTANCE_ID = "test_instance" METRIC_KIND = "GAUGE" - create_custom_metric( + custom_metric_descriptor = create_custom_metric( client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND) # wait until metric has been created, use the get call to wait until @@ -77,3 +81,5 @@ def _(): response['timeSeries'][0]['points'][0]['value']['int64Value']) # using seed of 1 will create a value of 1 assert value == pseudo_random_value + + delete_metric_descriptor(client, custom_metric_descriptor['name'])