15
15
import argparse
16
16
import os
17
17
import pprint
18
- import random
19
18
import time
19
+ import uuid
20
20
21
21
from google .cloud import monitoring_v3
22
22
23
23
24
- # Avoid collisions with other runs
25
- RANDOM_SUFFIX = str (random .randint (1000 , 9999 ))
24
+ PROJECT_ID = os .environ ['GCLOUD_PROJECT' ]
26
25
27
26
28
27
def create_metric_descriptor (project_id ):
29
28
# [START monitoring_create_metric]
30
29
client = monitoring_v3 .MetricServiceClient ()
31
30
project_name = client .project_path (project_id )
32
31
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 ())
34
33
descriptor .metric_kind = (
35
34
monitoring_v3 .enums .MetricDescriptor .MetricKind .GAUGE )
36
35
descriptor .value_type = (
@@ -55,7 +54,7 @@ def write_time_series(project_id):
55
54
project_name = client .project_path (project_id )
56
55
57
56
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 ())
59
58
series .resource .type = 'gce_instance'
60
59
series .resource .labels ['instance_id' ] = '1234567890123456789'
61
60
series .resource .labels ['zone' ] = 'us-central1-f'
@@ -205,29 +204,6 @@ def get_metric_descriptor(metric_name):
205
204
# [END monitoring_get_descriptor]
206
205
207
206
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
-
231
207
if __name__ == '__main__' :
232
208
parser = argparse .ArgumentParser (
233
209
description = 'Demonstrates Monitoring API operations.' )
@@ -310,25 +286,25 @@ def project_id():
310
286
args = parser .parse_args ()
311
287
312
288
if args .command == 'create-metric-descriptor' :
313
- create_metric_descriptor (project_id () )
289
+ create_metric_descriptor (PROJECT_ID )
314
290
if args .command == 'list-metric-descriptors' :
315
- list_metric_descriptors (project_id () )
291
+ list_metric_descriptors (PROJECT_ID )
316
292
if args .command == 'get-metric-descriptor' :
317
293
get_metric_descriptor (args .metric_type_name )
318
294
if args .command == 'delete-metric-descriptor' :
319
295
delete_metric_descriptor (args .metric_descriptor_name )
320
296
if args .command == 'list-resources' :
321
- list_monitored_resources (project_id () )
297
+ list_monitored_resources (PROJECT_ID )
322
298
if args .command == 'get-resource' :
323
299
get_monitored_resource_descriptor (
324
- project_id () , args .resource_type_name )
300
+ PROJECT_ID , args .resource_type_name )
325
301
if args .command == 'write-time-series' :
326
- write_time_series (project_id () )
302
+ write_time_series (PROJECT_ID )
327
303
if args .command == 'list-time-series' :
328
- list_time_series (project_id () )
304
+ list_time_series (PROJECT_ID )
329
305
if args .command == 'list-time-series-header' :
330
- list_time_series_header (project_id () )
306
+ list_time_series_header (PROJECT_ID )
331
307
if args .command == 'list-time-series-reduce' :
332
- list_time_series_reduce (project_id () )
308
+ list_time_series_reduce (PROJECT_ID )
333
309
if args .command == 'list-time-series-aggregate' :
334
- list_time_series_aggregate (project_id () )
310
+ list_time_series_aggregate (PROJECT_ID )
0 commit comments