14
14
15
15
import argparse
16
16
17
- from google .cloud .monitoring import (
18
- Aligner , Client , MetricKind , Reducer , ValueType )
17
+ from google .cloud import monitoring
19
18
20
19
21
- def create_metric_descriptor (client ):
20
+ def create_metric_descriptor ():
21
+ client = monitoring .Client ()
22
22
descriptor = client .metric_descriptor (
23
23
'custom.googleapis.com/my_metric' ,
24
- metric_kind = MetricKind .GAUGE ,
25
- value_type = ValueType .DOUBLE ,
24
+ metric_kind = monitoring . MetricKind .GAUGE ,
25
+ value_type = monitoring . ValueType .DOUBLE ,
26
26
description = 'This is a simple example of a custom metric.' )
27
27
descriptor .create ()
28
28
29
29
30
- def delete_metric_descriptor (client , descriptor ):
30
+ def delete_metric_descriptor (descriptor ):
31
+ client = monitoring .Client ()
31
32
descriptor = client .metric_descriptor (
32
- 'custom.googleapis.com/my_metric'
33
+ descriptor
33
34
)
34
35
descriptor .delete ()
35
- print 'Deleted metric.'
36
+ print 'Deleted metric descriptor .'
36
37
37
38
38
- def write_time_series (client ):
39
+ def write_time_series ():
40
+ client = monitoring .Client ()
39
41
resource = client .resource (
40
42
'gce_instance' ,
41
43
labels = {
@@ -53,47 +55,57 @@ def write_time_series(client):
53
55
client .write_point (metric , resource , 3.14 )
54
56
55
57
56
- def list_time_series (client ):
58
+ def list_time_series ():
59
+ client = monitoring .Client ()
57
60
metric = 'compute.googleapis.com/instance/cpu/utilization'
58
- print (list (client .query (metric , minutes = 5 )))
61
+ query_results = client .query (metric , minutes = 5 )
62
+ for result in query_results :
63
+ print result
59
64
60
65
61
- def list_time_series_header (client ):
66
+ def list_time_series_header ():
67
+ client = monitoring .Client ()
62
68
metric = 'compute.googleapis.com/instance/cpu/utilization'
63
- print (list (client .query (metric , minutes = 5 ).iter (headers_only = True )))
69
+ query_results = client .query (metric , minutes = 5 ).iter (headers_only = True )
70
+ for result in query_results :
71
+ print result
64
72
65
73
66
- def list_time_series_aggregate (client ):
74
+ def list_time_series_aggregate ():
75
+ client = monitoring .Client ()
67
76
metric = 'compute.googleapis.com/instance/cpu/utilization'
68
77
print (list (client .query (metric , hours = 1 ).align (
69
- Aligner .ALIGN_MEAN , minutes = 5 )))
78
+ monitoring . Aligner .ALIGN_MEAN , minutes = 5 )))
70
79
71
80
72
- def list_time_series_reduce (client ):
81
+ def list_time_series_reduce ():
82
+ client = monitoring .Client ()
73
83
metric = 'compute.googleapis.com/instance/cpu/utilization'
74
84
print (list (client .query (metric , hours = 1 ).align (
75
- Aligner .ALIGN_MEAN , minutes = 5 ).reduce (
76
- Reducer .REDUCE_MEAN , 'resource.zone' )))
85
+ monitoring . Aligner .ALIGN_MEAN , minutes = 5 ).reduce (
86
+ monitoring . Reducer .REDUCE_MEAN , 'resource.zone' )))
77
87
78
88
79
- def list_metric_descriptors (client ):
89
+ def list_metric_descriptors ():
90
+ client = monitoring .Client ()
80
91
for descriptor in client .list_metric_descriptors ():
81
92
print (descriptor .type )
82
93
83
94
84
- def list_monitored_resources (client ):
95
+ def list_monitored_resources ():
96
+ client = monitoring .Client ()
85
97
for descriptor in client .list_resource_descriptors ():
86
98
print (descriptor .type )
87
99
88
100
89
- def get_monitored_resource_descriptor (client , type ):
101
+ def get_monitored_resource_descriptor (type ):
102
+ client = monitoring .Client ()
90
103
print (client .fetch_resource_descriptor (type ))
91
104
92
105
93
106
if __name__ == '__main__' :
94
107
parser = argparse .ArgumentParser (
95
108
description = 'Demonstrates Monitoring API operations.' )
96
- parser .add_argument ('--project_id' , help = 'Your cloud project ID.' )
97
109
98
110
subparsers = parser .add_subparsers (dest = 'command' )
99
111
@@ -160,25 +172,24 @@ def get_monitored_resource_descriptor(client, type):
160
172
)
161
173
162
174
args = parser .parse_args ()
163
- client = Client (args .project_id )
164
175
165
176
if args .command == 'create-metric-descriptor' :
166
- create_metric_descriptor (client )
177
+ create_metric_descriptor ()
167
178
if args .command == 'list-metric-descriptors' :
168
- list_metric_descriptors (client )
179
+ list_metric_descriptors ()
169
180
if args .command == 'delete-metric-descriptor' :
170
- delete_metric_descriptor (client , args .metric )
181
+ delete_metric_descriptor (args .metric )
171
182
if args .command == 'list-resources' :
172
- list_monitored_resources (client )
183
+ list_monitored_resources ()
173
184
if args .command == 'get-resource' :
174
- get_monitored_resource_descriptor (client , args .resource )
185
+ get_monitored_resource_descriptor (args .resource )
175
186
if args .command == 'write-time-series' :
176
- write_time_series (client )
187
+ write_time_series ()
177
188
if args .command == 'list-time-series' :
178
- list_time_series (client )
189
+ list_time_series ()
179
190
if args .command == 'list-time-series-header' :
180
- list_time_series_header (client )
191
+ list_time_series_header ()
181
192
if args .command == 'list-time-series-reduce' :
182
- list_time_series_reduce (client )
193
+ list_time_series_reduce ()
183
194
if args .command == 'list-time-series-aggregate' :
184
- list_time_series_aggregate (client )
195
+ list_time_series_aggregate ()
0 commit comments