1515import argparse
1616import os
1717import pprint
18- import random
1918import time
19+ import uuid
2020
2121from google .cloud import monitoring_v3
2222
2323
24- # Avoid collisions with other runs
25- RANDOM_SUFFIX = str (random .randint (1000 , 9999 ))
24+ PROJECT_ID = os .environ ['GCLOUD_PROJECT' ]
2625
2726
2827def create_metric_descriptor (project_id ):
2928 # [START monitoring_create_metric]
3029 client = monitoring_v3 .MetricServiceClient ()
3130 project_name = client .project_path (project_id )
3231 descriptor = monitoring_v3 .types .MetricDescriptor ()
33- descriptor .type = 'custom.googleapis.com/my_metric' + RANDOM_SUFFIX
32+ descriptor .type = 'custom.googleapis.com/my_metric' + str ( uuid . uuid4 ())
3433 descriptor .metric_kind = (
3534 monitoring_v3 .enums .MetricDescriptor .MetricKind .GAUGE )
3635 descriptor .value_type = (
@@ -55,7 +54,7 @@ def write_time_series(project_id):
5554 project_name = client .project_path (project_id )
5655
5756 series = monitoring_v3 .types .TimeSeries ()
58- series .metric .type = 'custom.googleapis.com/my_metric' + RANDOM_SUFFIX
57+ series .metric .type = 'custom.googleapis.com/my_metric' + str ( uuid . uuid4 ())
5958 series .resource .type = 'gce_instance'
6059 series .resource .labels ['instance_id' ] = '1234567890123456789'
6160 series .resource .labels ['zone' ] = 'us-central1-f'
@@ -205,29 +204,6 @@ def get_metric_descriptor(metric_name):
205204 # [END monitoring_get_descriptor]
206205
207206
208- class MissingProjectIdError (Exception ):
209- pass
210-
211-
212- def project_id ():
213- """Retreives the project id from the environment variable.
214-
215- Raises:
216- MissingProjectIdError -- When not set.
217-
218- Returns:
219- str -- the project name
220- """
221- project_id = (os .environ ['GOOGLE_CLOUD_PROJECT' ] or
222- os .environ ['GCLOUD_PROJECT' ])
223-
224- if not project_id :
225- raise MissingProjectIdError (
226- 'Set the environment variable ' +
227- 'GCLOUD_PROJECT to your Google Cloud Project Id.' )
228- return project_id
229-
230-
231207if __name__ == '__main__' :
232208 parser = argparse .ArgumentParser (
233209 description = 'Demonstrates Monitoring API operations.' )
@@ -310,25 +286,25 @@ def project_id():
310286 args = parser .parse_args ()
311287
312288 if args .command == 'create-metric-descriptor' :
313- create_metric_descriptor (project_id () )
289+ create_metric_descriptor (PROJECT_ID )
314290 if args .command == 'list-metric-descriptors' :
315- list_metric_descriptors (project_id () )
291+ list_metric_descriptors (PROJECT_ID )
316292 if args .command == 'get-metric-descriptor' :
317293 get_metric_descriptor (args .metric_type_name )
318294 if args .command == 'delete-metric-descriptor' :
319295 delete_metric_descriptor (args .metric_descriptor_name )
320296 if args .command == 'list-resources' :
321- list_monitored_resources (project_id () )
297+ list_monitored_resources (PROJECT_ID )
322298 if args .command == 'get-resource' :
323299 get_monitored_resource_descriptor (
324- project_id () , args .resource_type_name )
300+ PROJECT_ID , args .resource_type_name )
325301 if args .command == 'write-time-series' :
326- write_time_series (project_id () )
302+ write_time_series (PROJECT_ID )
327303 if args .command == 'list-time-series' :
328- list_time_series (project_id () )
304+ list_time_series (PROJECT_ID )
329305 if args .command == 'list-time-series-header' :
330- list_time_series_header (project_id () )
306+ list_time_series_header (PROJECT_ID )
331307 if args .command == 'list-time-series-reduce' :
332- list_time_series_reduce (project_id () )
308+ list_time_series_reduce (PROJECT_ID )
333309 if args .command == 'list-time-series-aggregate' :
334- list_time_series_aggregate (project_id () )
310+ list_time_series_aggregate (PROJECT_ID )
0 commit comments