mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-29 21:23:55 +08:00
elasticsearch: test against elasticsearch 7 (#2431)
* Update core repo SHA * elasticsearch: test against elasticsearch 7 --------- Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
This commit is contained in:

committed by
GitHub

parent
bd4a22a0d9
commit
3291f38e8d
@ -50,6 +50,23 @@ Article = helpers.Article
|
||||
# pylint: disable=too-many-public-methods
|
||||
|
||||
|
||||
def normalize_arguments(doc_type, body=None):
|
||||
if major_version == 7:
|
||||
return {"document": body} if body else {}
|
||||
return (
|
||||
{"body": body, "doc_type": doc_type}
|
||||
if body
|
||||
else {"doc_type": doc_type}
|
||||
)
|
||||
|
||||
|
||||
def get_elasticsearch_client(*args, **kwargs):
|
||||
client = Elasticsearch(*args, **kwargs)
|
||||
if major_version == 7:
|
||||
client.transport._verified_elasticsearch = True
|
||||
return client
|
||||
|
||||
|
||||
@mock.patch(
|
||||
"elasticsearch.connection.http_urllib3.Urllib3HttpConnection.perform_request"
|
||||
)
|
||||
@ -79,10 +96,14 @@ class TestElasticsearchIntegration(TestBase):
|
||||
ElasticsearchInstrumentor().uninstrument()
|
||||
|
||||
def test_instrumentor(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
|
||||
es = Elasticsearch()
|
||||
es.index(index="sw", doc_type="_doc", id=1, body={"name": "adam"})
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
es.index(
|
||||
index="sw",
|
||||
id=1,
|
||||
**normalize_arguments(body={"name": "adam"}, doc_type="_doc"),
|
||||
)
|
||||
|
||||
spans_list = self.get_finished_spans()
|
||||
self.assertEqual(len(spans_list), 1)
|
||||
@ -97,20 +118,24 @@ class TestElasticsearchIntegration(TestBase):
|
||||
# check that no spans are generated after uninstrument
|
||||
ElasticsearchInstrumentor().uninstrument()
|
||||
|
||||
es.index(index="sw", doc_type="_doc", id=1, body={"name": "adam"})
|
||||
es.index(
|
||||
index="sw",
|
||||
id=1,
|
||||
**normalize_arguments(body={"name": "adam"}, doc_type="_doc"),
|
||||
)
|
||||
|
||||
spans_list = self.get_finished_spans()
|
||||
self.assertEqual(len(spans_list), 1)
|
||||
|
||||
def test_span_not_recording(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
mock_tracer = mock.Mock()
|
||||
mock_span = mock.Mock()
|
||||
mock_span.is_recording.return_value = False
|
||||
mock_tracer.start_span.return_value = mock_span
|
||||
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
|
||||
tracer.return_value = mock_tracer
|
||||
Elasticsearch()
|
||||
get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
self.assertFalse(mock_span.is_recording())
|
||||
self.assertTrue(mock_span.is_recording.called)
|
||||
self.assertFalse(mock_span.set_attribute.called)
|
||||
@ -122,7 +147,7 @@ class TestElasticsearchIntegration(TestBase):
|
||||
prefix = "prefix-from-env"
|
||||
ElasticsearchInstrumentor().uninstrument()
|
||||
ElasticsearchInstrumentor(span_name_prefix=prefix).instrument()
|
||||
request_mock.return_value = (1, {}, {})
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
self._test_prefix(prefix)
|
||||
|
||||
def test_prefix_env(self, request_mock):
|
||||
@ -131,13 +156,17 @@ class TestElasticsearchIntegration(TestBase):
|
||||
os.environ[env_var] = prefix
|
||||
ElasticsearchInstrumentor().uninstrument()
|
||||
ElasticsearchInstrumentor().instrument()
|
||||
request_mock.return_value = (1, {}, {})
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
del os.environ[env_var]
|
||||
self._test_prefix(prefix)
|
||||
|
||||
def _test_prefix(self, prefix):
|
||||
es = Elasticsearch()
|
||||
es.index(index="sw", doc_type="_doc", id=1, body={"name": "adam"})
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
es.index(
|
||||
index="sw",
|
||||
id=1,
|
||||
**normalize_arguments(body={"name": "adam"}, doc_type="_doc"),
|
||||
)
|
||||
|
||||
spans_list = self.get_finished_spans()
|
||||
self.assertEqual(len(spans_list), 1)
|
||||
@ -150,8 +179,10 @@ class TestElasticsearchIntegration(TestBase):
|
||||
{},
|
||||
'{"found": false, "timed_out": true, "took": 7}',
|
||||
)
|
||||
es = Elasticsearch()
|
||||
es.get(index="test-index", doc_type="_doc", id=1)
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
es.get(
|
||||
index="test-index", **normalize_arguments(doc_type="_doc"), id=1
|
||||
)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
|
||||
@ -171,14 +202,18 @@ class TestElasticsearchIntegration(TestBase):
|
||||
def test_trace_error_not_found(self, request_mock):
|
||||
msg = "record not found"
|
||||
exc = elasticsearch.exceptions.NotFoundError(404, msg)
|
||||
request_mock.return_value = (1, {}, {})
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
request_mock.side_effect = exc
|
||||
self._test_trace_error(StatusCode.ERROR, exc)
|
||||
|
||||
def _test_trace_error(self, code, exc):
|
||||
es = Elasticsearch()
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
try:
|
||||
es.get(index="test-index", doc_type="_doc", id=1)
|
||||
es.get(
|
||||
index="test-index",
|
||||
**normalize_arguments(doc_type="_doc"),
|
||||
id=1,
|
||||
)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
pass
|
||||
|
||||
@ -192,10 +227,14 @@ class TestElasticsearchIntegration(TestBase):
|
||||
)
|
||||
|
||||
def test_parent(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
es = Elasticsearch()
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
with self.tracer.start_as_current_span("parent"):
|
||||
es.index(index="sw", doc_type="_doc", id=1, body={"name": "adam"})
|
||||
es.index(
|
||||
index="sw",
|
||||
**normalize_arguments(doc_type="_doc", body={"name": "adam"}),
|
||||
id=1,
|
||||
)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
self.assertEqual(len(spans), 2)
|
||||
@ -206,8 +245,8 @@ class TestElasticsearchIntegration(TestBase):
|
||||
self.assertEqual(child.parent.span_id, parent.context.span_id)
|
||||
|
||||
def test_multithread(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
es = Elasticsearch()
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
ev = threading.Event()
|
||||
|
||||
# 1. Start tracing from thread-1; make thread-2 wait
|
||||
@ -215,13 +254,21 @@ class TestElasticsearchIntegration(TestBase):
|
||||
# 3. Check the spans got different parents, and are in the expected order.
|
||||
def target1(parent_span):
|
||||
with trace.use_span(parent_span):
|
||||
es.get(index="test-index", doc_type="_doc", id=1)
|
||||
es.get(
|
||||
index="test-index",
|
||||
**normalize_arguments(doc_type="_doc"),
|
||||
id=1,
|
||||
)
|
||||
ev.set()
|
||||
ev.wait()
|
||||
|
||||
def target2():
|
||||
ev.wait()
|
||||
es.get(index="test-index", doc_type="_doc", id=2)
|
||||
es.get(
|
||||
index="test-index",
|
||||
**normalize_arguments(doc_type="_doc"),
|
||||
id=2,
|
||||
)
|
||||
ev.set()
|
||||
|
||||
with self.tracer.start_as_current_span("parent") as span:
|
||||
@ -247,7 +294,7 @@ class TestElasticsearchIntegration(TestBase):
|
||||
def test_dsl_search(self, request_mock):
|
||||
request_mock.return_value = (1, {}, '{"hits": {"hits": []}}')
|
||||
|
||||
client = Elasticsearch()
|
||||
client = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
search = Search(using=client, index="test-index").filter(
|
||||
"term", author="testing"
|
||||
)
|
||||
@ -264,7 +311,7 @@ class TestElasticsearchIntegration(TestBase):
|
||||
|
||||
def test_dsl_search_sanitized(self, request_mock):
|
||||
request_mock.return_value = (1, {}, '{"hits": {"hits": []}}')
|
||||
client = Elasticsearch()
|
||||
client = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
search = Search(using=client, index="test-index").filter(
|
||||
"term", author="testing"
|
||||
)
|
||||
@ -280,8 +327,8 @@ class TestElasticsearchIntegration(TestBase):
|
||||
)
|
||||
|
||||
def test_dsl_create(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
client = Elasticsearch()
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
client = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
Article.init(using=client)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
@ -307,8 +354,8 @@ class TestElasticsearchIntegration(TestBase):
|
||||
)
|
||||
|
||||
def test_dsl_create_sanitized(self, request_mock):
|
||||
request_mock.return_value = (1, {}, {})
|
||||
client = Elasticsearch()
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
client = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
Article.init(using=client)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
@ -323,9 +370,9 @@ class TestElasticsearchIntegration(TestBase):
|
||||
)
|
||||
|
||||
def test_dsl_index(self, request_mock):
|
||||
request_mock.return_value = helpers.dsl_index_result
|
||||
request_mock.return_value = (1, {}, helpers.dsl_index_result[2])
|
||||
|
||||
client = Elasticsearch()
|
||||
client = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
article = Article(
|
||||
meta={"id": 2},
|
||||
title="About searching",
|
||||
@ -374,11 +421,16 @@ class TestElasticsearchIntegration(TestBase):
|
||||
{},
|
||||
'{"found": false, "timed_out": true, "took": 7}',
|
||||
)
|
||||
es = Elasticsearch()
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
index = "test-index"
|
||||
doc_id = 1
|
||||
kwargs = {"params": {"test": True}}
|
||||
es.get(index=index, doc_type="_doc", id=doc_id, **kwargs)
|
||||
kwargs = {"params": {"refresh": True, "realtime": True}}
|
||||
es.get(
|
||||
index=index,
|
||||
id=doc_id,
|
||||
**normalize_arguments(doc_type="_doc"),
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
|
||||
@ -386,12 +438,21 @@ class TestElasticsearchIntegration(TestBase):
|
||||
self.assertEqual(
|
||||
"GET", spans[0].attributes[request_hook_method_attribute]
|
||||
)
|
||||
expected_url = f"/{index}/_doc/{doc_id}"
|
||||
self.assertEqual(
|
||||
f"/{index}/_doc/{doc_id}",
|
||||
expected_url,
|
||||
spans[0].attributes[request_hook_url_attribute],
|
||||
)
|
||||
|
||||
if major_version == 7:
|
||||
expected_kwargs = {
|
||||
**kwargs,
|
||||
"headers": {"accept": "application/json"},
|
||||
}
|
||||
else:
|
||||
expected_kwargs = {**kwargs}
|
||||
self.assertEqual(
|
||||
json.dumps(kwargs),
|
||||
json.dumps(expected_kwargs),
|
||||
spans[0].attributes[request_hook_kwargs_attribute],
|
||||
)
|
||||
|
||||
@ -431,13 +492,11 @@ class TestElasticsearchIntegration(TestBase):
|
||||
},
|
||||
}
|
||||
|
||||
request_mock.return_value = (
|
||||
1,
|
||||
{},
|
||||
json.dumps(response_payload),
|
||||
request_mock.return_value = (1, {}, json.dumps(response_payload))
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
es.get(
|
||||
index="test-index", **normalize_arguments(doc_type="_doc"), id=1
|
||||
)
|
||||
es = Elasticsearch()
|
||||
es.get(index="test-index", doc_type="_doc", id=1)
|
||||
|
||||
spans = self.get_finished_spans()
|
||||
|
||||
@ -453,13 +512,11 @@ class TestElasticsearchIntegration(TestBase):
|
||||
tracer_provider=trace.NoOpTracerProvider()
|
||||
)
|
||||
response_payload = '{"found": false, "timed_out": true, "took": 7}'
|
||||
request_mock.return_value = (
|
||||
1,
|
||||
{},
|
||||
response_payload,
|
||||
request_mock.return_value = (1, {}, response_payload)
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
res = es.get(
|
||||
index="test-index", **normalize_arguments(doc_type="_doc"), id=1
|
||||
)
|
||||
es = Elasticsearch()
|
||||
res = es.get(index="test-index", doc_type="_doc", id=1)
|
||||
self.assertEqual(
|
||||
res.get("found"), json.loads(response_payload).get("found")
|
||||
)
|
||||
@ -486,11 +543,11 @@ class TestElasticsearchIntegration(TestBase):
|
||||
)
|
||||
|
||||
def test_bulk(self, request_mock):
|
||||
request_mock.return_value = (1, {}, "")
|
||||
request_mock.return_value = (1, {}, "{}")
|
||||
|
||||
es = Elasticsearch()
|
||||
es = get_elasticsearch_client(hosts=["http://localhost:9200"])
|
||||
es.bulk(
|
||||
[
|
||||
body=[
|
||||
{
|
||||
"_op_type": "index",
|
||||
"_index": "sw",
|
||||
|
Reference in New Issue
Block a user