forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoring.py
372 lines (296 loc) · 12.5 KB
/
monitoring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from google.cloud.exceptions import BadRequest
from google.cloud.exceptions import InternalServerError
from google.cloud.exceptions import NotFound
from google.cloud.exceptions import ServiceUnavailable
from google.cloud import monitoring
from retry import RetryErrors
from retry import RetryResult
from system_test_utils import unique_resource_id
retry_404 = RetryErrors(NotFound, max_tries=5)
retry_404_500 = RetryErrors((NotFound, InternalServerError))
retry_500 = RetryErrors(InternalServerError)
retry_503 = RetryErrors(ServiceUnavailable)
class TestMonitoring(unittest.TestCase):
def test_fetch_metric_descriptor(self):
METRIC_TYPE = (
'pubsub.googleapis.com/topic/send_message_operation_count')
METRIC_KIND = monitoring.MetricKind.DELTA
VALUE_TYPE = monitoring.ValueType.INT64
client = monitoring.Client()
descriptor = client.fetch_metric_descriptor(METRIC_TYPE)
expected_name = 'projects/{project}/metricDescriptors/{type}'.format(
project=client.project,
type=METRIC_TYPE,
)
self.assertEqual(descriptor.name, expected_name)
self.assertEqual(descriptor.type, METRIC_TYPE)
self.assertEqual(descriptor.metric_kind, METRIC_KIND)
self.assertEqual(descriptor.value_type, VALUE_TYPE)
self.assertTrue(descriptor.description)
self.assertTrue(descriptor.labels)
for label in descriptor.labels:
self.assertTrue(label.key)
self.assertTrue(label.value_type)
self.assertTrue(label.description)
def test_list_metric_descriptors(self):
METRIC_TYPE = (
'pubsub.googleapis.com/topic/send_message_operation_count')
METRIC_KIND = monitoring.MetricKind.DELTA
VALUE_TYPE = monitoring.ValueType.INT64
client = monitoring.Client()
descriptor = None
for item in client.list_metric_descriptors():
if item.type == METRIC_TYPE:
descriptor = item
self.assertIsNotNone(descriptor)
expected_name = 'projects/{project}/{what}/{type}'.format(
project=client.project,
what='metricDescriptors',
type=METRIC_TYPE,
)
self.assertEqual(descriptor.name, expected_name)
self.assertEqual(descriptor.type, METRIC_TYPE)
self.assertEqual(descriptor.metric_kind, METRIC_KIND)
self.assertEqual(descriptor.value_type, VALUE_TYPE)
self.assertTrue(descriptor.description)
self.assertTrue(descriptor.labels)
for label in descriptor.labels:
self.assertTrue(label.key)
self.assertTrue(label.value_type)
self.assertTrue(label.description)
def test_list_metric_descriptors_filtered(self):
client = monitoring.Client()
PREFIX = 'compute.googleapis.com/'
descriptors = client.list_metric_descriptors(type_prefix=PREFIX)
# There are currently 18 types with this prefix, but that may change.
self.assertGreater(len(descriptors), 10)
for descriptor in descriptors:
self.assertTrue(descriptor.type.startswith(PREFIX))
def test_fetch_resource_descriptor(self):
RESOURCE_TYPE = 'pubsub_topic'
client = monitoring.Client()
descriptor = client.fetch_resource_descriptor(RESOURCE_TYPE)
expected_name = 'projects/{project}/{what}/{type}'.format(
project=client.project,
what='monitoredResourceDescriptors',
type=RESOURCE_TYPE,
)
self.assertEqual(descriptor.name, expected_name)
self.assertEqual(descriptor.type, RESOURCE_TYPE)
self.assertTrue(descriptor.display_name)
self.assertTrue(descriptor.description)
self.assertTrue(descriptor.labels)
for label in descriptor.labels:
self.assertTrue(label.key)
self.assertTrue(label.value_type)
self.assertTrue(label.description)
def test_list_resource_descriptors(self):
RESOURCE_TYPE = 'pubsub_topic'
client = monitoring.Client()
descriptor = None
for item in client.list_resource_descriptors():
if item.type == RESOURCE_TYPE:
descriptor = item
self.assertIsNotNone(descriptor)
expected_name = 'projects/{project}/{what}/{type}'.format(
project=client.project,
what='monitoredResourceDescriptors',
type=RESOURCE_TYPE,
)
self.assertEqual(descriptor.name, expected_name)
self.assertEqual(descriptor.type, RESOURCE_TYPE)
self.assertTrue(descriptor.display_name)
self.assertTrue(descriptor.description)
self.assertTrue(descriptor.labels)
for label in descriptor.labels:
self.assertTrue(label.key)
self.assertTrue(label.value_type)
self.assertTrue(label.description)
def test_query(self):
METRIC_TYPE = (
'pubsub.googleapis.com/topic/send_message_operation_count')
client = monitoring.Client()
query = client.query(METRIC_TYPE, hours=1)
# There may be no data, but we can ask anyway.
for _ in query:
pass # Not necessarily reached.
def test_create_and_delete_metric_descriptor(self):
METRIC_TYPE = ('custom.googleapis.com/tmp/system_test_example' +
unique_resource_id())
METRIC_KIND = monitoring.MetricKind.GAUGE
VALUE_TYPE = monitoring.ValueType.DOUBLE
DESCRIPTION = 'System test example -- DELETE ME!'
client = monitoring.Client()
descriptor = client.metric_descriptor(
METRIC_TYPE,
metric_kind=METRIC_KIND,
value_type=VALUE_TYPE,
description=DESCRIPTION,
)
retry_500(descriptor.create)()
retry_404_500(descriptor.delete)()
def test_write_point(self):
METRIC_TYPE = ('custom.googleapis.com/tmp/system_test_example' +
unique_resource_id())
METRIC_KIND = monitoring.MetricKind.GAUGE
VALUE_TYPE = monitoring.ValueType.DOUBLE
DESCRIPTION = 'System test example -- DELETE ME!'
VALUE = 3.14
client = monitoring.Client()
descriptor = client.metric_descriptor(
METRIC_TYPE,
metric_kind=METRIC_KIND,
value_type=VALUE_TYPE,
description=DESCRIPTION,
)
descriptor.create()
metric = client.metric(METRIC_TYPE, {})
resource = client.resource('global', {})
retry_500(client.write_point)(metric, resource, VALUE)
def _query_timeseries_with_retries():
MAX_RETRIES = 7
def _has_timeseries(result):
return len(list(result)) > 0
retry_result = RetryResult(_has_timeseries,
max_tries=MAX_RETRIES)(client.query)
return RetryErrors(BadRequest, max_tries=MAX_RETRIES)(retry_result)
query = _query_timeseries_with_retries()(METRIC_TYPE, minutes=5)
timeseries_list = list(query)
self.assertEqual(len(timeseries_list), 1)
timeseries = timeseries_list[0]
self.assertEqual(timeseries.metric, metric)
# project_id label only exists on output.
del timeseries.resource.labels['project_id']
self.assertEqual(timeseries.resource, resource)
descriptor.delete()
with self.assertRaises(NotFound):
descriptor.delete()
class TestMonitoringGroups(unittest.TestCase):
def setUp(self):
self.to_delete = []
self.DISPLAY_NAME = 'Testing: New group'
self.FILTER = 'resource.type = "gce_instance"'
self.IS_CLUSTER = True
def tearDown(self):
for group in self.to_delete:
retry_404(group.delete)()
def test_create_group(self):
client = monitoring.Client()
group = client.group(
display_name=self.DISPLAY_NAME,
filter_string=self.FILTER,
is_cluster=self.IS_CLUSTER,
)
retry_503(group.create)()
self.to_delete.append(group)
self.assertTrue(group.exists())
def test_list_groups(self):
client = monitoring.Client()
new_group = client.group(
display_name=self.DISPLAY_NAME,
filter_string=self.FILTER,
is_cluster=self.IS_CLUSTER,
)
before_groups = client.list_groups()
before_names = set(group.name for group in before_groups)
retry_503(new_group.create)()
self.to_delete.append(new_group)
self.assertTrue(new_group.exists())
after_groups = client.list_groups()
after_names = set(group.name for group in after_groups)
self.assertEqual(after_names - before_names,
set([new_group.name]))
def test_reload_group(self):
client = monitoring.Client()
group = client.group(
display_name=self.DISPLAY_NAME,
filter_string=self.FILTER,
is_cluster=self.IS_CLUSTER,
)
retry_503(group.create)()
self.to_delete.append(group)
group.filter = 'resource.type = "aws_ec2_instance"'
group.display_name = 'locally changed name'
group.reload()
self.assertEqual(group.filter, self.FILTER)
self.assertEqual(group.display_name, self.DISPLAY_NAME)
def test_update_group(self):
NEW_FILTER = 'resource.type = "aws_ec2_instance"'
NEW_DISPLAY_NAME = 'updated'
client = monitoring.Client()
group = client.group(
display_name=self.DISPLAY_NAME,
filter_string=self.FILTER,
is_cluster=self.IS_CLUSTER,
)
retry_503(group.create)()
self.to_delete.append(group)
group.filter = NEW_FILTER
group.display_name = NEW_DISPLAY_NAME
group.update()
after = client.fetch_group(group.id)
self.assertEqual(after.filter, NEW_FILTER)
self.assertEqual(after.display_name, NEW_DISPLAY_NAME)
def test_list_group_members(self):
client = monitoring.Client()
group = client.group(
display_name=self.DISPLAY_NAME,
filter_string=self.FILTER,
is_cluster=self.IS_CLUSTER,
)
retry_503(group.create)()
self.to_delete.append(group)
for member in group.list_members():
self.assertIsInstance(member, monitoring.Resource)
def test_group_hierarchy(self):
client = monitoring.Client()
root_group = client.group(
display_name='Testing: Root group',
filter_string=self.FILTER,
)
retry_503(root_group.create)()
self.to_delete.insert(0, root_group)
middle_group = client.group(
display_name='Testing: Middle group',
filter_string=self.FILTER,
parent_id=root_group.id,
)
retry_503(middle_group.create)()
self.to_delete.insert(0, middle_group)
leaf_group = client.group(
display_name='Testing: Leaf group',
filter_string=self.FILTER,
parent_id=middle_group.id,
)
retry_503(leaf_group.create)()
self.to_delete.insert(0, leaf_group)
# Test for parent.
actual_parent = middle_group.fetch_parent()
self.assertTrue(actual_parent.name, root_group.name)
# Test for children.
actual_children = middle_group.list_children()
children_names = [group.name for group in actual_children]
self.assertEqual(children_names, [leaf_group.name])
# Test for descendants.
actual_descendants = root_group.list_descendants()
descendant_names = {group.name for group in actual_descendants}
self.assertEqual(descendant_names,
set([middle_group.name, leaf_group.name]))
# Test for ancestors.
actual_ancestors = leaf_group.list_ancestors()
ancestor_names = [group.name for group in actual_ancestors]
self.assertEqual(ancestor_names, [middle_group.name, root_group.name])