13
13
# limitations under the License.
14
14
15
15
import argparse
16
+ import os
17
+ import pprint
18
+ import time
16
19
17
- from google .cloud import monitoring
20
+ from google .cloud import monitoring_v3
18
21
19
22
20
- def create_metric_descriptor ():
23
+ def create_metric_descriptor (project_id ):
21
24
# [START monitoring_create_metric]
22
- client = monitoring .Client ()
23
- descriptor = client .metric_descriptor (
24
- 'custom.googleapis.com/my_metric' ,
25
- metric_kind = monitoring .MetricKind .GAUGE ,
26
- value_type = monitoring .ValueType .DOUBLE ,
27
- description = 'This is a simple example of a custom metric.' )
28
- descriptor .create ()
25
+ client = monitoring_v3 .MetricServiceClient ()
26
+ project_name = client .project_path (project_id )
27
+ descriptor = monitoring_v3 .types .MetricDescriptor ()
28
+ descriptor .type = 'custom.googleapis.com/my_metric'
29
+ descriptor .metric_kind = (
30
+ monitoring_v3 .enums .MetricDescriptor .MetricKind .GAUGE )
31
+ descriptor .value_type = (
32
+ monitoring_v3 .enums .MetricDescriptor .ValueType .DOUBLE )
33
+ descriptor .description = 'This is a simple example of a custom metric.'
34
+ descriptor = client .create_metric_descriptor (project_name , descriptor )
35
+ print ('Created {}.' .format (descriptor .name ))
29
36
# [END monitoring_create_metric]
30
37
31
38
32
39
def delete_metric_descriptor (descriptor_name ):
33
40
# [START monitoring_delete_metric]
34
- client = monitoring .Client ()
35
-
36
- descriptor = client .metric_descriptor (descriptor_name )
37
- descriptor .delete ()
38
-
41
+ client = monitoring_v3 .MetricServiceClient ()
42
+ client .delete_metric_descriptor (descriptor_name )
39
43
print ('Deleted metric descriptor {}.' .format (descriptor_name ))
40
44
# [END monitoring_delete_metric]
41
45
42
46
43
- def write_time_series ():
47
+ def write_time_series (project_id ):
44
48
# [START monitoring_write_timeseries]
45
- client = monitoring . Client ()
46
- resource = client .resource (
47
- 'gce_instance' ,
48
- labels = {
49
- 'instance_id' : '1234567890123456789' ,
50
- 'zone' : 'us-central1-f' ,
51
- }
52
- )
53
-
54
- metric = client . metric (
55
- type_ = 'custom.googleapis.com/my_metric' ,
56
- labels = {
57
- }
58
- )
59
- client .write_point ( metric , resource , 3.14 )
49
+ client = monitoring_v3 . MetricServiceClient ()
50
+ project_name = client .project_path ( project_id )
51
+
52
+ series = monitoring_v3 . types . TimeSeries ()
53
+ series . metric . type = 'custom.googleapis.com/my_metric'
54
+ series . resource . type = 'gce_instance'
55
+ series . resource . labels [ 'instance_id' ] = '1234567890123456789'
56
+ series . resource . labels [ 'zone' ] = 'us-central1-f'
57
+ point = series . points . add ()
58
+ point . value . double_value = 3.14
59
+ now = time . time ()
60
+ point . interval . end_time . seconds = int ( now )
61
+ point . interval . end_time . nanos = int (
62
+ ( now - point . interval . end_time . seconds ) * 10 ** 9 )
63
+ client .create_time_series ( project_name , [ series ] )
60
64
# [END monitoring_write_timeseries]
61
65
62
66
63
- def list_time_series ():
67
+ def list_time_series (project_id ):
64
68
# [START monitoring_read_timeseries_simple]
65
- client = monitoring .Client ()
66
- metric = 'compute.googleapis.com/instance/cpu/utilization'
67
- query_results = client .query (metric , minutes = 5 )
68
- for result in query_results :
69
+ client = monitoring_v3 .MetricServiceClient ()
70
+ project_name = client .project_path (project_id )
71
+ interval = monitoring_v3 .types .TimeInterval ()
72
+ now = time .time ()
73
+ interval .end_time .seconds = int (now )
74
+ interval .end_time .nanos = int (
75
+ (now - interval .end_time .seconds ) * 10 ** 9 )
76
+ interval .start_time .seconds = int (now - 300 )
77
+ interval .start_time .nanos = interval .end_time .nanos
78
+ results = client .list_time_series (
79
+ project_name ,
80
+ 'metric.type = "compute.googleapis.com/instance/cpu/utilization"' ,
81
+ interval ,
82
+ monitoring_v3 .enums .ListTimeSeriesRequest .TimeSeriesView .FULL )
83
+ for result in results :
69
84
print (result )
70
85
# [END monitoring_read_timeseries_simple]
71
86
72
87
73
- def list_time_series_header ():
88
+ def list_time_series_header (project_id ):
74
89
# [START monitoring_read_timeseries_fields]
75
- client = monitoring .Client ()
76
- metric = 'compute.googleapis.com/instance/cpu/utilization'
77
- query_results = client .query (metric , minutes = 5 ).iter (headers_only = True )
78
- for result in query_results :
90
+ client = monitoring_v3 .MetricServiceClient ()
91
+ project_name = client .project_path (project_id )
92
+ interval = monitoring_v3 .types .TimeInterval ()
93
+ now = time .time ()
94
+ interval .end_time .seconds = int (now )
95
+ interval .end_time .nanos = int (
96
+ (now - interval .end_time .seconds ) * 10 ** 9 )
97
+ interval .start_time .seconds = int (now - 300 )
98
+ interval .start_time .nanos = interval .end_time .nanos
99
+ results = client .list_time_series (
100
+ project_name ,
101
+ 'metric.type = "compute.googleapis.com/instance/cpu/utilization"' ,
102
+ interval ,
103
+ monitoring_v3 .enums .ListTimeSeriesRequest .TimeSeriesView .HEADERS )
104
+ for result in results :
79
105
print (result )
80
106
# [END monitoring_read_timeseries_fields]
81
107
82
108
83
- def list_time_series_aggregate ():
109
+ def list_time_series_aggregate (project_id ):
84
110
# [START monitoring_read_timeseries_align]
85
- client = monitoring .Client ()
86
- metric = 'compute.googleapis.com/instance/cpu/utilization'
87
- query_results = client .query (metric , hours = 1 ).align (
88
- monitoring .Aligner .ALIGN_MEAN , minutes = 5 )
89
- for result in query_results :
111
+ client = monitoring_v3 .MetricServiceClient ()
112
+ project_name = client .project_path (project_id )
113
+ interval = monitoring_v3 .types .TimeInterval ()
114
+ now = time .time ()
115
+ interval .end_time .seconds = int (now )
116
+ interval .end_time .nanos = int (
117
+ (now - interval .end_time .seconds ) * 10 ** 9 )
118
+ interval .start_time .seconds = int (now - 3600 )
119
+ interval .start_time .nanos = interval .end_time .nanos
120
+ aggregation = monitoring_v3 .types .Aggregation ()
121
+ aggregation .alignment_period .seconds = 300 # 5 minutes
122
+ aggregation .per_series_aligner = (
123
+ monitoring_v3 .enums .Aggregation .Aligner .ALIGN_MEAN )
124
+
125
+ results = client .list_time_series (
126
+ project_name ,
127
+ 'metric.type = "compute.googleapis.com/instance/cpu/utilization"' ,
128
+ interval ,
129
+ monitoring_v3 .enums .ListTimeSeriesRequest .TimeSeriesView .FULL ,
130
+ aggregation )
131
+ for result in results :
90
132
print (result )
91
133
# [END monitoring_read_timeseries_align]
92
134
93
135
94
- def list_time_series_reduce ():
136
+ def list_time_series_reduce (project_id ):
95
137
# [START monitoring_read_timeseries_reduce]
96
- client = monitoring .Client ()
97
- metric = 'compute.googleapis.com/instance/cpu/utilization'
98
- query_results = client .query (metric , hours = 1 ).align (
99
- monitoring .Aligner .ALIGN_MEAN , minutes = 5 ).reduce (
100
- monitoring .Reducer .REDUCE_MEAN , 'resource.zone' )
101
- for result in query_results :
138
+ client = monitoring_v3 .MetricServiceClient ()
139
+ project_name = client .project_path (project_id )
140
+ interval = monitoring_v3 .types .TimeInterval ()
141
+ now = time .time ()
142
+ interval .end_time .seconds = int (now )
143
+ interval .end_time .nanos = int (
144
+ (now - interval .end_time .seconds ) * 10 ** 9 )
145
+ interval .start_time .seconds = int (now - 3600 )
146
+ interval .start_time .nanos = interval .end_time .nanos
147
+ aggregation = monitoring_v3 .types .Aggregation ()
148
+ aggregation .alignment_period .seconds = 300 # 5 minutes
149
+ aggregation .per_series_aligner = (
150
+ monitoring_v3 .enums .Aggregation .Aligner .ALIGN_MEAN )
151
+ aggregation .cross_series_reducer = (
152
+ monitoring_v3 .enums .Aggregation .Reducer .REDUCE_MEAN )
153
+ aggregation .group_by_fields .append ('resource.zone' )
154
+
155
+ results = client .list_time_series (
156
+ project_name ,
157
+ 'metric.type = "compute.googleapis.com/instance/cpu/utilization"' ,
158
+ interval ,
159
+ monitoring_v3 .enums .ListTimeSeriesRequest .TimeSeriesView .FULL ,
160
+ aggregation )
161
+ for result in results :
102
162
print (result )
103
163
# [END monitoring_read_timeseries_reduce]
104
164
105
165
106
- def list_metric_descriptors ():
166
+ def list_metric_descriptors (project_id ):
107
167
# [START monitoring_list_descriptors]
108
- client = monitoring .Client ()
109
- for descriptor in client .list_metric_descriptors ():
168
+ client = monitoring_v3 .MetricServiceClient ()
169
+ project_name = client .project_path (project_id )
170
+ for descriptor in client .list_metric_descriptors (project_name ):
110
171
print (descriptor .type )
111
172
# [END monitoring_list_descriptors]
112
173
113
174
114
- def list_monitored_resources ():
175
+ def list_monitored_resources (project_id ):
115
176
# [START monitoring_list_resources]
116
- client = monitoring .Client ()
117
- for descriptor in client .list_resource_descriptors ():
177
+ client = monitoring_v3 .MetricServiceClient ()
178
+ project_name = client .project_path (project_id )
179
+ resource_descriptors = (
180
+ client .list_monitored_resource_descriptors (project_name ))
181
+ for descriptor in resource_descriptors :
118
182
print (descriptor .type )
119
183
# [END monitoring_list_resources]
120
184
121
185
122
- def get_monitored_resource_descriptor (resource_type_name ):
186
+ def get_monitored_resource_descriptor (project_id , resource_type_name ):
123
187
# [START monitoring_get_resource]
124
- client = monitoring .Client ()
125
- print (client .fetch_resource_descriptor (resource_type_name ))
188
+ client = monitoring_v3 .MetricServiceClient ()
189
+ resource_path = client .monitored_resource_descriptor_path (
190
+ project_id , resource_type_name )
191
+ pprint .pprint (client .get_monitored_resource_descriptor (resource_path ))
126
192
# [END monitoring_get_resource]
127
193
128
194
129
- def get_metric_descriptor (metric_type_name ):
195
+ def get_metric_descriptor (metric_name ):
130
196
# [START monitoring_get_descriptor]
131
- client = monitoring .Client ()
132
- print (client .fetch_metric_descriptor (metric_type_name ))
197
+ client = monitoring_v3 .MetricServiceClient ()
198
+ descriptor = client .get_metric_descriptor (metric_name )
199
+ pprint .pprint (descriptor )
133
200
# [END monitoring_get_descriptor]
134
201
135
202
203
+ class MissingProjectIdError (Exception ):
204
+ pass
205
+
206
+
207
+ def project_id ():
208
+ """Retreives the project id from the environment variable.
209
+
210
+ Raises:
211
+ MissingProjectIdError -- When not set.
212
+
213
+ Returns:
214
+ str -- the project name
215
+ """
216
+ project_id = (os .environ ['GOOGLE_CLOUD_PROJECT' ] or
217
+ os .environ ['GCLOUD_PROJECT' ])
218
+
219
+ if not project_id :
220
+ raise MissingProjectIdError (
221
+ 'Set the environment variable ' +
222
+ 'GCLOUD_PROJECT to your Google Cloud Project Id.' )
223
+ return project_id
224
+
225
+
136
226
if __name__ == '__main__' :
137
227
parser = argparse .ArgumentParser (
138
228
description = 'Demonstrates Monitoring API operations.' )
@@ -215,24 +305,25 @@ def get_metric_descriptor(metric_type_name):
215
305
args = parser .parse_args ()
216
306
217
307
if args .command == 'create-metric-descriptor' :
218
- create_metric_descriptor ()
308
+ create_metric_descriptor (project_id () )
219
309
if args .command == 'list-metric-descriptors' :
220
- list_metric_descriptors ()
310
+ list_metric_descriptors (project_id () )
221
311
if args .command == 'get-metric-descriptor' :
222
312
get_metric_descriptor (args .metric_type_name )
223
313
if args .command == 'delete-metric-descriptor' :
224
314
delete_metric_descriptor (args .metric_descriptor_name )
225
315
if args .command == 'list-resources' :
226
- list_monitored_resources ()
316
+ list_monitored_resources (project_id () )
227
317
if args .command == 'get-resource' :
228
- get_monitored_resource_descriptor (args .resource_type_name )
318
+ get_monitored_resource_descriptor (
319
+ project_id (), args .resource_type_name )
229
320
if args .command == 'write-time-series' :
230
- write_time_series ()
321
+ write_time_series (project_id () )
231
322
if args .command == 'list-time-series' :
232
- list_time_series ()
323
+ list_time_series (project_id () )
233
324
if args .command == 'list-time-series-header' :
234
- list_time_series_header ()
325
+ list_time_series_header (project_id () )
235
326
if args .command == 'list-time-series-reduce' :
236
- list_time_series_reduce ()
327
+ list_time_series_reduce (project_id () )
237
328
if args .command == 'list-time-series-aggregate' :
238
- list_time_series_aggregate ()
329
+ list_time_series_aggregate (project_id () )
0 commit comments