|
| 1 | +# BSD 3-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2019, Elasticsearch BV |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of the copyright holder nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +import pytest # isort:skip |
| 32 | + |
| 33 | +pytest.importorskip("elasticsearch._async") # isort:skip |
| 34 | + |
| 35 | +import os |
| 36 | + |
| 37 | +from elasticsearch import VERSION as ES_VERSION |
| 38 | +from elasticsearch import AsyncElasticsearch |
| 39 | + |
| 40 | +from elasticapm.conf.constants import TRANSACTION |
| 41 | + |
| 42 | +pytestmark = [pytest.mark.elasticsearch, pytest.mark.asyncio, pytest.mark.integrationtest] |
| 43 | + |
| 44 | +if "ES_URL" not in os.environ: |
| 45 | + pytestmark.append(pytest.mark.skip("Skipping elasticsearch test, no ES_URL environment variable")) |
| 46 | + |
| 47 | + |
| 48 | +document_type = "_doc" if ES_VERSION[0] >= 6 else "doc" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +async def async_elasticsearch(request): |
| 53 | + """AsyncElasticsearch client fixture.""" |
| 54 | + client = AsyncElasticsearch(hosts=os.environ["ES_URL"]) |
| 55 | + try: |
| 56 | + yield client |
| 57 | + finally: |
| 58 | + await client.indices.delete(index="*") |
| 59 | + await client.close() |
| 60 | + |
| 61 | + |
| 62 | +async def test_ping(instrument, elasticapm_client, async_elasticsearch): |
| 63 | + elasticapm_client.begin_transaction("test") |
| 64 | + result = await async_elasticsearch.ping() |
| 65 | + elasticapm_client.end_transaction("test", "OK") |
| 66 | + |
| 67 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 68 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 69 | + assert len(spans) == 1 |
| 70 | + span = spans[0] |
| 71 | + assert span["name"] == "ES HEAD /" |
| 72 | + assert span["type"] == "db" |
| 73 | + assert span["subtype"] == "elasticsearch" |
| 74 | + assert span["action"] == "query" |
| 75 | + assert span["sync"] is False |
| 76 | + |
| 77 | + |
| 78 | +async def test_info(instrument, elasticapm_client, async_elasticsearch): |
| 79 | + elasticapm_client.begin_transaction("test") |
| 80 | + result = await async_elasticsearch.info() |
| 81 | + elasticapm_client.end_transaction("test", "OK") |
| 82 | + |
| 83 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 84 | + |
| 85 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 86 | + assert len(spans) == 1 |
| 87 | + span = spans[0] |
| 88 | + assert span["name"] == "ES GET /" |
| 89 | + assert span["type"] == "db" |
| 90 | + assert span["subtype"] == "elasticsearch" |
| 91 | + assert span["action"] == "query" |
| 92 | + assert span["sync"] is False |
| 93 | + |
| 94 | + |
| 95 | +async def test_create(instrument, elasticapm_client, async_elasticsearch): |
| 96 | + elasticapm_client.begin_transaction("test") |
| 97 | + if ES_VERSION[0] < 5: |
| 98 | + r1 = await async_elasticsearch.create("tweets", document_type, {"user": "kimchy", "text": "hola"}, 1) |
| 99 | + elif ES_VERSION[0] < 7: |
| 100 | + r1 = await async_elasticsearch.create("tweets", document_type, 1, body={"user": "kimchy", "text": "hola"}) |
| 101 | + else: |
| 102 | + r1 = await async_elasticsearch.create("tweets", 1, body={"user": "kimchy", "text": "hola"}) |
| 103 | + r2 = await async_elasticsearch.create( |
| 104 | + index="tweets", doc_type=document_type, id=2, body={"user": "kimchy", "text": "hola"}, refresh=True |
| 105 | + ) |
| 106 | + elasticapm_client.end_transaction("test", "OK") |
| 107 | + |
| 108 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 109 | + |
| 110 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 111 | + assert len(spans) == 2 |
| 112 | + |
| 113 | + for i, span in enumerate(spans): |
| 114 | + if ES_VERSION[0] >= 5: |
| 115 | + assert span["name"] in ( |
| 116 | + "ES PUT /tweets/%s/%d/_create" % (document_type, i + 1), |
| 117 | + "ES PUT /tweets/_create/%d" % (i + 1), |
| 118 | + ) |
| 119 | + else: |
| 120 | + assert span["name"] == "ES PUT /tweets/%s/%d" % (document_type, i + 1) |
| 121 | + assert span["type"] == "db" |
| 122 | + assert span["subtype"] == "elasticsearch" |
| 123 | + assert span["action"] == "query" |
| 124 | + assert span["context"]["db"]["type"] == "elasticsearch" |
| 125 | + assert "statement" not in span["context"]["db"] |
| 126 | + |
| 127 | + |
| 128 | +async def test_search_body(instrument, elasticapm_client, async_elasticsearch): |
| 129 | + await async_elasticsearch.create( |
| 130 | + index="tweets", doc_type=document_type, id=1, body={"user": "kimchy", "text": "hola"}, refresh=True |
| 131 | + ) |
| 132 | + elasticapm_client.begin_transaction("test") |
| 133 | + search_query = {"query": {"term": {"user": "kimchy"}}} |
| 134 | + result = await async_elasticsearch.search(body=search_query, params=None) |
| 135 | + elasticapm_client.end_transaction("test", "OK") |
| 136 | + |
| 137 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 138 | + assert result["hits"]["hits"][0]["_source"] == {"user": "kimchy", "text": "hola"} |
| 139 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 140 | + assert len(spans) == 1 |
| 141 | + span = spans[0] |
| 142 | + # Depending on ES_VERSION, could be /_all/_search or /_search, and GET or POST |
| 143 | + assert span["name"] in ("ES GET /_search", "ES GET /_all/_search", "ES POST /_search") |
| 144 | + assert span["type"] == "db" |
| 145 | + assert span["subtype"] == "elasticsearch" |
| 146 | + assert span["action"] == "query" |
| 147 | + assert span["context"]["db"]["type"] == "elasticsearch" |
| 148 | + assert span["context"]["db"]["statement"] == '{"term": {"user": "kimchy"}}' |
| 149 | + assert span["sync"] is False |
| 150 | + |
| 151 | + |
| 152 | +async def test_count_body(instrument, elasticapm_client, async_elasticsearch): |
| 153 | + await async_elasticsearch.create( |
| 154 | + index="tweets", doc_type=document_type, id=1, body={"user": "kimchy", "text": "hola"}, refresh=True |
| 155 | + ) |
| 156 | + elasticapm_client.begin_transaction("test") |
| 157 | + search_query = {"query": {"term": {"user": "kimchy"}}} |
| 158 | + result = await async_elasticsearch.count(body=search_query) |
| 159 | + elasticapm_client.end_transaction("test", "OK") |
| 160 | + |
| 161 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 162 | + assert result["count"] == 1 |
| 163 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 164 | + assert len(spans) == 1 |
| 165 | + span = spans[0] |
| 166 | + assert span["name"] in ("ES GET /_count", "ES POST /_count", "ES GET /_all/_count") |
| 167 | + assert span["type"] == "db" |
| 168 | + assert span["subtype"] == "elasticsearch" |
| 169 | + assert span["action"] == "query" |
| 170 | + assert span["context"]["db"]["type"] == "elasticsearch" |
| 171 | + assert span["context"]["db"]["statement"] == '{"term": {"user": "kimchy"}}' |
| 172 | + assert span["sync"] is False |
| 173 | + |
| 174 | + |
| 175 | +async def test_delete_by_query_body(instrument, elasticapm_client, async_elasticsearch): |
| 176 | + await async_elasticsearch.create( |
| 177 | + index="tweets", doc_type=document_type, id=1, body={"user": "kimchy", "text": "hola"}, refresh=True |
| 178 | + ) |
| 179 | + elasticapm_client.begin_transaction("test") |
| 180 | + result = await async_elasticsearch.delete_by_query(index="tweets", body={"query": {"term": {"user": "kimchy"}}}) |
| 181 | + elasticapm_client.end_transaction("test", "OK") |
| 182 | + |
| 183 | + transaction = elasticapm_client.events[TRANSACTION][0] |
| 184 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 185 | + |
| 186 | + span = spans[0] |
| 187 | + assert span["name"] == "ES POST /tweets/_delete_by_query" |
| 188 | + assert span["type"] == "db" |
| 189 | + assert span["subtype"] == "elasticsearch" |
| 190 | + assert span["action"] == "query" |
| 191 | + assert span["context"]["db"]["type"] == "elasticsearch" |
| 192 | + assert span["context"]["db"]["statement"] == '{"term": {"user": "kimchy"}}' |
| 193 | + assert span["sync"] is False |
0 commit comments