-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtest_integration.py
413 lines (336 loc) · 14.5 KB
/
test_integration.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import logging
import pytest
import requests
from packaging import version
from datadog_checks.dev.utils import get_metadata_metrics
from datadog_checks.elastic import ESCheck
from datadog_checks.elastic.metrics import (
CAT_ALLOCATION_METRICS,
CLUSTER_PENDING_TASKS,
INDEX_SEARCH_STATS,
STATS_METRICS,
health_stats_for_version,
index_stats_for_version,
pshard_stats_for_version,
slm_stats_for_version,
)
from .common import CLUSTER_TAG, ELASTIC_VERSION, IS_OPENSEARCH, JVM_RATES, PASSWORD, URL, USER, _test_check
log = logging.getLogger('test_elastic')
pytestmark = pytest.mark.integration
def test_custom_queries_valid_metrics(dd_environment, dd_run_check, instance, aggregator):
custom_queries = [
{
'endpoint': '/_nodes',
'data_path': '_nodes',
'columns': [
{
'value_path': 'total',
'name': 'elasticsearch.custom.metric',
},
{'value_path': 'total', 'name': 'elasticsearch.custom.metric2', 'type': 'monotonic_count'},
],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
dd_run_check(check)
aggregator.assert_metric('elasticsearch.custom.metric2', metric_type=aggregator.MONOTONIC_COUNT)
aggregator.assert_metric('elasticsearch.custom.metric', metric_type=aggregator.GAUGE)
def test_custom_queries_one_invalid(dd_environment, dd_run_check, instance, aggregator):
custom_queries = [
{
# Wrong endpoint
'endpoint': '/_nodes2',
'data_path': '_nodes',
'columns': [
{
'value_path': 'total',
'name': 'elasticsearch.custom.metric2',
},
],
},
{
# Good endpoint
'endpoint': '/_nodes',
'data_path': '_nodes',
'columns': [
{
'value_path': 'total',
'name': 'elasticsearch.custom.metric',
},
],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
dd_run_check(check)
aggregator.assert_metric('elasticsearch.custom.metric', metric_type=aggregator.GAUGE)
def test_custom_queries_with_payload(dd_environment, dd_run_check, instance, aggregator, cluster_tags):
custom_queries = [
{
'endpoint': '/_search',
'data_path': 'hits.total',
'payload': {"query": {"match": {"phrase": {"query": ""}}}},
'columns': [
{
'value_path': 'value',
'name': 'elasticsearch.custom.metric',
},
{'value_path': 'relation', 'name': 'dynamic_tag', 'type': 'tag'},
],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
dd_run_check(check)
tags = cluster_tags + ['dynamic_tag:eq']
aggregator.assert_metric('elasticsearch.custom.metric', metric_type=aggregator.GAUGE, tags=tags)
@pytest.mark.skipif(
version.parse(ELASTIC_VERSION) < version.parse('8.0.0'), reason='Test unavailable for Elasticsearch < 8.0.0'
)
def test_custom_queries_with_payload_multiterm(dd_environment, dd_run_check, instance, aggregator, cluster_tags):
response = requests.put(
"{}/multiterm_test".format(instance['url']),
json={"mappings": {"properties": {"field0": {"type": "keyword"}, "field1": {"type": "keyword"}}}},
)
response.raise_for_status()
response = requests.post(
"{}/multiterm_test/_doc?refresh=wait_for".format(instance['url']), json={"field0": "foo", "field1": "bar"}
)
response.raise_for_status()
custom_queries = [
{
'endpoint': '/_search',
'data_path': 'aggregations.values.buckets',
'payload': {
"size": 0,
"aggs": {"values": {"multi_terms": {"terms": [{"field": "field0"}, {"field": "field1"}]}}},
},
'columns': [
{
'value_path': 'doc_count',
'name': 'elasticsearch.custom.metric',
},
{'value_path': 'key.0', 'name': 'dynamic_tag0', 'type': 'tag'},
{'value_path': 'key.1', 'name': 'dynamic_tag1', 'type': 'tag'},
],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
dd_run_check(check)
tags = cluster_tags + ['dynamic_tag0:foo', "dynamic_tag1:bar"]
aggregator.assert_metric('elasticsearch.custom.metric', metric_type=aggregator.GAUGE, tags=tags)
def test_custom_queries_valid_tags(dd_environment, dd_run_check, instance, aggregator, cluster_tags):
custom_queries = [
{
'endpoint': '/_nodes',
'data_path': '_nodes',
'columns': [
{
'value_path': 'total',
'name': 'elasticsearch.custom.metric',
},
{'value_path': 'total', 'name': 'dynamic_tag', 'type': 'tag'},
],
'tags': ['custom_tag:1'],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
dd_run_check(check)
tags = cluster_tags + ['custom_tag:1'] + ['dynamic_tag:1']
aggregator.assert_metric('elasticsearch.custom.metric', metric_type=aggregator.GAUGE, tags=tags)
def test_custom_queries_non_existent_metrics(caplog, dd_environment, dd_run_check, instance, aggregator):
custom_queries = [
{
'endpoint': '/_nodes',
'data_path': '_nodes',
'columns': [
{
'value_path': 'totals', # nonexistent elasticsearch metric
'name': 'elasticsearch.custom.metric',
},
],
'tags': ['custom_tag:1'],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
caplog.clear()
with caplog.at_level(logging.DEBUG):
dd_run_check(check)
aggregator.assert_metric('elasticsearch.custom.metric', count=0)
assert 'Metric not found: _nodes.totals -> elasticsearch.custom.metric' in caplog.text
def test_custom_queries_non_existent_tags(caplog, dd_environment, dd_run_check, instance, aggregator, cluster_tags):
custom_queries = [
{
'endpoint': '/_nodes',
'data_path': '_nodes',
'columns': [
{
'value_path': 'total',
'name': 'elasticsearch.custom.metric',
},
{
'value_path': 'totals', # nonexistent elasticsearch metric as tag
'name': 'nonexistent_tag',
'type': 'tag',
},
],
},
]
instance['custom_queries'] = custom_queries
check = ESCheck('elastic', {}, instances=[instance])
caplog.clear()
with caplog.at_level(logging.DEBUG):
dd_run_check(check)
aggregator.assert_metric('elasticsearch.custom.metric', count=1, tags=cluster_tags)
assert 'Dynamic tag is null: _nodes.total -> nonexistent_tag' in caplog.text
def test_check(dd_environment, elastic_check, instance, aggregator, cluster_tags, node_tags):
elastic_check.check(None)
_test_check(elastic_check, instance, aggregator, cluster_tags, node_tags)
@pytest.mark.skipif(IS_OPENSEARCH, reason='Test unavailable for OpenSearch')
def test_check_slm_stats(dd_environment, instance, aggregator, cluster_tags, node_tags, slm_tags):
instance['slm_stats'] = True
elastic_check = ESCheck('elastic', {}, instances=[instance])
elastic_check.check(None)
_test_check(elastic_check, instance, aggregator, cluster_tags, node_tags)
# SLM stats
slm_metrics = slm_stats_for_version(elastic_check._get_es_version())
for m_name in slm_metrics:
aggregator.assert_metric(m_name, at_least=1, tags=slm_tags)
def test_disable_cluster_tag(dd_environment, instance, aggregator, new_cluster_tags):
instance['disable_legacy_cluster_tag'] = True
elastic_check = ESCheck('elastic', {}, instances=[instance])
elastic_check.check(None)
es_version = elastic_check._get_es_version()
# cluster stats
expected_metrics = health_stats_for_version(es_version)
expected_metrics.update(CLUSTER_PENDING_TASKS)
for m_name in expected_metrics:
aggregator.assert_metric(m_name, at_least=1, tags=new_cluster_tags)
def test_jvm_gc_rate_metrics(dd_environment, instance, aggregator, cluster_tags, node_tags):
instance['gc_collectors_as_rate'] = True
check = ESCheck('elastic', {}, instances=[instance])
check.check(instance)
for metric in JVM_RATES:
aggregator.assert_metric(metric, at_least=1, tags=node_tags)
_test_check(check, instance, aggregator, cluster_tags, node_tags)
def test_node_name_as_host(dd_environment, instance_normalize_hostname, aggregator, node_tags):
elastic_check = ESCheck('elastic', {}, instances=[instance_normalize_hostname])
elastic_check.check(None)
node_name = node_tags[-1].split(':')[1]
for m_name in STATS_METRICS:
aggregator.assert_metric(m_name, count=1, tags=node_tags, hostname=node_name)
def test_pshard_metrics(dd_environment, aggregator):
instance = {'url': URL, 'pshard_stats': True, 'username': USER, 'password': PASSWORD, 'tls_verify': False}
elastic_check = ESCheck('elastic', {}, instances=[instance])
es_version = elastic_check._get_es_version()
elastic_check.check(None)
pshard_stats_metrics = pshard_stats_for_version(es_version)
for m_name, desc in pshard_stats_metrics.items():
if desc[0] == 'gauge':
aggregator.assert_metric(m_name)
# Our pshard metrics are getting sent, let's check that they're accurate
# Note: please make sure you don't install Maven on the CI for future
# elastic search CI integrations. It would make the line below fail :/
aggregator.assert_metric('elasticsearch.primaries.docs.count')
def test_detailed_index_stats(dd_environment, aggregator):
instance = {
"url": URL,
"cluster_stats": True,
"pshard_stats": True,
"detailed_index_stats": True,
"tls_verify": False,
}
elastic_check = ESCheck('elastic', {}, instances=[instance])
es_version = elastic_check._get_es_version()
elastic_check.check(None)
pshard_stats_metrics = pshard_stats_for_version(es_version)
for m_name, desc in pshard_stats_metrics.items():
if desc[0] == 'gauge' and desc[1].startswith('_all.'):
aggregator.assert_metric(m_name)
aggregator.assert_metric_has_tag('elasticsearch.primaries.docs.count', tag='index_name:_all')
aggregator.assert_metric_has_tag('elasticsearch.primaries.docs.count', tag='index_name:testindex')
aggregator.assert_metric_has_tag('elasticsearch.primaries.docs.count', tag='index_name:.testindex')
aggregator.assert_metrics_using_metadata(
get_metadata_metrics(),
check_metric_type=False,
exclude=[
"system.cpu.idle",
"system.load.1",
"system.load.15",
"system.load.5",
"system.mem.free",
"system.mem.total",
"system.mem.usable",
"system.mem.used",
"system.net.bytes_rcvd",
"system.net.bytes_sent",
"system.swap.free",
"system.swap.total",
"system.swap.used",
],
)
def test_index_metrics(dd_environment, aggregator, instance, cluster_tags):
instance['index_stats'] = True
elastic_check = ESCheck('elastic', {}, instances=[instance])
es_version = elastic_check._get_es_version()
if es_version < [1, 0, 0]:
pytest.skip("Index metrics are only tested in version 1.0.0+")
elastic_check.check(None)
expected_metrics = list(index_stats_for_version(es_version)) + [name for name, _ in INDEX_SEARCH_STATS]
for m_name in expected_metrics:
aggregator.assert_metric(m_name, tags=cluster_tags + ['index_name:testindex'])
aggregator.assert_metric(m_name, tags=cluster_tags + ['index_name:.testindex'])
aggregator.assert_metrics_using_metadata(
get_metadata_metrics(), exclude=['elasticsearch.custom.metric'], check_submission_type=True
)
def test_cat_allocation_metrics(dd_environment, aggregator, instance, cluster_tags):
instance['cat_allocation_stats'] = True
elastic_check = ESCheck('elastic', {}, instances=[instance])
elastic_check.check(None)
for m_name in CAT_ALLOCATION_METRICS:
aggregator.assert_metric(m_name)
def test_health_event(dd_environment, aggregator):
dummy_tags = ['elastique:recherche']
instance = {'url': URL, 'username': USER, 'password': PASSWORD, 'tags': dummy_tags, 'tls_verify': False}
elastic_check = ESCheck('elastic', {}, instances=[instance])
es_version = elastic_check._get_es_version()
# Should be yellow at first
requests.put(URL + '/_settings', data='{"index": {"number_of_replicas": 100}', verify=False)
elastic_check.check(None)
if es_version < [2, 0, 0]:
assert len(aggregator.events) == 1
assert sorted(aggregator.events[0]['tags']) == sorted(set(['url:{}'.format(URL)] + dummy_tags + CLUSTER_TAG))
else:
aggregator.assert_service_check('elasticsearch.cluster_health')
def test_health_event_disabled(dd_environment, aggregator):
"""
Don't submit an event if user disables event submission.
"""
dummy_tags = ['elastique:recherche']
instance = {
'url': URL,
'username': USER,
'password': PASSWORD,
'tags': dummy_tags,
'tls_verify': False,
'submit_events': False,
}
elastic_check = ESCheck('elastic', {}, instances=[instance])
elastic_check._get_es_version()
# Should be yellow at first
requests.put(URL + '/_settings', data='{"index": {"number_of_replicas": 100}', verify=False)
elastic_check.check(None)
assert not aggregator.events
aggregator.assert_service_check('elasticsearch.cluster_health')
def test_metadata(dd_environment, aggregator, elastic_check, instance, version_metadata, datadog_agent):
elastic_check.check_id = 'test:123'
elastic_check.check(None)
datadog_agent.assert_metadata('test:123', version_metadata)
datadog_agent.assert_metadata_count(len(version_metadata))