-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest_async_elasticsearch.py
238 lines (198 loc) · 9.29 KB
/
test_async_elasticsearch.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
# Copyright 2010 New Relic, 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 pytest
import elasticsearch._async.client as client
from testing_support.fixtures import override_application_settings
from testing_support.fixture.event_loop import event_loop as loop # noqa: F401
from testing_support.util import instance_hostname
from testing_support.validators.validate_transaction_errors import validate_transaction_errors
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from newrelic.api.background_task import background_task
from conftest import ES_VERSION, ES_SETTINGS
# Settings
_enable_instance_settings = {"datastore_tracer.instance_reporting.enabled": True}
_disable_instance_settings = {"datastore_tracer.instance_reporting.enabled": False}
# Metrics
_base_scoped_metrics = [
("Datastore/statement/Elasticsearch/_all/cluster.health", 1),
("Datastore/statement/Elasticsearch/_all/search", 2),
("Datastore/statement/Elasticsearch/address/index", 2),
("Datastore/statement/Elasticsearch/address/search", 1),
("Datastore/statement/Elasticsearch/contacts/index", 3),
("Datastore/statement/Elasticsearch/contacts/indices.refresh", 1),
("Datastore/statement/Elasticsearch/contacts/search", 2),
("Datastore/statement/Elasticsearch/other/search", 2),
]
_base_rollup_metrics = [
("Datastore/operation/Elasticsearch/cluster.health", 1),
("Datastore/operation/Elasticsearch/index", 5),
("Datastore/operation/Elasticsearch/indices.refresh", 1),
("Datastore/operation/Elasticsearch/search", 7),
("Datastore/statement/Elasticsearch/_all/cluster.health", 1),
("Datastore/statement/Elasticsearch/_all/search", 2),
("Datastore/statement/Elasticsearch/address/index", 2),
("Datastore/statement/Elasticsearch/address/search", 1),
("Datastore/statement/Elasticsearch/contacts/index", 3),
("Datastore/statement/Elasticsearch/contacts/indices.refresh", 1),
("Datastore/statement/Elasticsearch/contacts/search", 2),
("Datastore/statement/Elasticsearch/other/search", 2),
]
# Version support
def is_importable(module_path):
try:
__import__(module_path)
return True
except ImportError:
return False
_all_count = 17
if is_importable("elasticsearch._async.client.cat") or is_importable("elasticsearch._async.client.cat"):
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/cat.health", 1))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/cat.health", 1))
_all_count += 1
else:
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/cat.health", None))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/cat.health", None))
if is_importable("elasticsearch._async.client.nodes") or is_importable("elasticsearch._async.client.nodes"):
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/nodes.info", 1))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/nodes.info", 1))
_all_count += 1
else:
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/nodes.info", None))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/nodes.info", None))
if hasattr(client, "SnapshotClient") and hasattr(client.SnapshotClient, "status"):
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/snapshot.status", 1))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/snapshot.status", 1))
_all_count += 1
else:
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/snapshot.status", None))
_base_rollup_metrics.append(("Datastore/operation/Elasticsearch/snapshot.status", None))
if hasattr(client.IndicesClient, "status"):
_base_scoped_metrics.append(("Datastore/statement/Elasticsearch/_all/indices.status", 1))
_base_rollup_metrics.extend(
[
("Datastore/operation/Elasticsearch/indices.status", 1),
("Datastore/statement/Elasticsearch/_all/indices.status", 1),
]
)
_all_count += 1
else:
_base_scoped_metrics.append(("Datastore/operation/Elasticsearch/indices.status", None))
_base_rollup_metrics.extend(
[
("Datastore/operation/Elasticsearch/indices.status", None),
("Datastore/statement/Elasticsearch/_all/indices.status", None),
]
)
_base_rollup_metrics.extend(
[
("Datastore/all", _all_count),
("Datastore/allOther", _all_count),
("Datastore/Elasticsearch/all", _all_count),
("Datastore/Elasticsearch/allOther", _all_count),
]
)
# Instance info
_disable_scoped_metrics = list(_base_scoped_metrics)
_disable_rollup_metrics = list(_base_rollup_metrics)
_enable_scoped_metrics = list(_base_scoped_metrics)
_enable_rollup_metrics = list(_base_rollup_metrics)
_host = instance_hostname(ES_SETTINGS["host"])
_port = ES_SETTINGS["port"]
_instance_metric_name = f"Datastore/instance/Elasticsearch/{_host}/{_port}"
_enable_rollup_metrics.append((_instance_metric_name, _all_count))
_disable_rollup_metrics.append((_instance_metric_name, None))
# Query
async def _exercise_es_v7(es):
await es.index(
index="contacts", doc_type="person", body={"name": "Joe Tester", "age": 25, "title": "QA Engineer"}, id=1
)
await es.index(
index="contacts", doc_type="person", body={"name": "Jessica Coder", "age": 32, "title": "Programmer"}, id=2
)
await es.index(
index="contacts", doc_type="person", body={"name": "Freddy Tester", "age": 29, "title": "Assistant"}, id=3
)
await es.indices.refresh("contacts")
await es.index(
index="address", doc_type="employee", body={"name": "Sherlock", "address": "221B Baker Street, London"}, id=1
)
await es.index(
index="address",
doc_type="employee",
body={"name": "Bilbo", "address": "Bag End, Bagshot row, Hobbiton, Shire"},
id=2,
)
await es.search(index="contacts", q="name:Joe")
await es.search(index="contacts", q="name:jessica")
await es.search(index="address", q="name:Sherlock")
await es.search(index=["contacts", "address"], q="name:Bilbo")
await es.search(index="contacts,address", q="name:Bilbo")
await es.search(index="*", q="name:Bilbo")
await es.search(q="name:Bilbo")
await es.cluster.health()
if hasattr(es, "cat"):
await es.cat.health()
if hasattr(es, "nodes"):
await es.nodes.info()
if hasattr(es, "snapshot") and hasattr(es.snapshot, "status"):
await es.snapshot.status()
if hasattr(es.indices, "status"):
await es.indices.status()
async def _exercise_es_v8(es):
await es.index(index="contacts", body={"name": "Joe Tester", "age": 25, "title": "QA Engineer"}, id=1)
await es.index(index="contacts", body={"name": "Jessica Coder", "age": 32, "title": "Programmer"}, id=2)
await es.index(index="contacts", body={"name": "Freddy Tester", "age": 29, "title": "Assistant"}, id=3)
await es.indices.refresh(index="contacts")
await es.index(index="address", body={"name": "Sherlock", "address": "221B Baker Street, London"}, id=1)
await es.index(index="address", body={"name": "Bilbo", "address": "Bag End, Bagshot row, Hobbiton, Shire"}, id=2)
await es.search(index="contacts", q="name:Joe")
await es.search(index="contacts", q="name:jessica")
await es.search(index="address", q="name:Sherlock")
await es.search(index=["contacts", "address"], q="name:Bilbo")
await es.search(index="contacts,address", q="name:Bilbo")
await es.search(index="*", q="name:Bilbo")
await es.search(q="name:Bilbo")
await es.cluster.health()
if hasattr(es, "cat"):
await es.cat.health()
if hasattr(es, "nodes"):
await es.nodes.info()
if hasattr(es, "snapshot") and hasattr(es.snapshot, "status"):
await es.snapshot.status()
if hasattr(es.indices, "status"):
await es.indices.status()
_exercise_es = _exercise_es_v7 if ES_VERSION < (8, 0, 0) else _exercise_es_v8
# Test
@validate_transaction_errors(errors=[])
@validate_transaction_metrics(
"test_async_elasticsearch:test_async_elasticsearch_operation_disabled",
scoped_metrics=_disable_scoped_metrics,
rollup_metrics=_disable_rollup_metrics,
background_task=True,
)
@override_application_settings(_disable_instance_settings)
@background_task()
def test_async_elasticsearch_operation_disabled(async_client, loop):
loop.run_until_complete(_exercise_es(async_client))
@validate_transaction_errors(errors=[])
@validate_transaction_metrics(
"test_async_elasticsearch:test_async_elasticsearch_operation_enabled",
scoped_metrics=_enable_scoped_metrics,
rollup_metrics=_enable_rollup_metrics,
background_task=True,
)
@override_application_settings(_enable_instance_settings)
@background_task()
def test_async_elasticsearch_operation_enabled(async_client, loop):
loop.run_until_complete(_exercise_es(async_client))