|
| 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 os |
| 32 | + |
| 33 | +import pytest |
| 34 | + |
| 35 | +from elasticapm.conf.constants import TRANSACTION |
| 36 | +from elasticapm.utils import default_ports |
| 37 | + |
| 38 | +aiomysql = pytest.importorskip("aiomysql") |
| 39 | + |
| 40 | +pytestmark = [pytest.mark.asyncio, pytest.mark.aiomysql] |
| 41 | + |
| 42 | + |
| 43 | +if "MYSQL_HOST" not in os.environ: |
| 44 | + pytestmark.append(pytest.mark.skip("Skipping aiomysql tests, no MYSQL_HOST environment variable set")) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(scope="function") |
| 48 | +async def aiomysql_connection(request, event_loop): |
| 49 | + assert event_loop.is_running() |
| 50 | + pool = await aiomysql.create_pool( |
| 51 | + host=os.environ.get("MYSQL_HOST", "localhost"), |
| 52 | + user=os.environ.get("MYSQL_USER", "eapm"), |
| 53 | + password=os.environ.get("MYSQL_PASSWORD", ""), |
| 54 | + db=os.environ.get("MYSQL_DATABASE", "eapm_tests"), |
| 55 | + loop=event_loop, |
| 56 | + ) |
| 57 | + |
| 58 | + try: |
| 59 | + async with pool.acquire() as conn: |
| 60 | + async with conn.cursor() as cursor: |
| 61 | + await cursor.execute("CREATE TABLE `test` (`id` INT, `name` VARCHAR(5))") |
| 62 | + await cursor.execute("INSERT INTO `test` (`id`, `name`) VALUES (1, 'one'), (2, 'two'), (3, 'three')") |
| 63 | + |
| 64 | + yield conn |
| 65 | + |
| 66 | + finally: |
| 67 | + # Drop the testing table and close the connection pool after testcase |
| 68 | + async with pool.acquire() as conn: |
| 69 | + async with conn.cursor() as cursor: |
| 70 | + await cursor.execute("DROP TABLE `test`") |
| 71 | + |
| 72 | + pool.close() |
| 73 | + await pool.wait_closed() |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.integrationtest |
| 77 | +async def test_aiomysql_select(instrument, aiomysql_connection, elasticapm_client): |
| 78 | + try: |
| 79 | + elasticapm_client.begin_transaction("web.django") |
| 80 | + |
| 81 | + async with aiomysql_connection.cursor() as cursor: |
| 82 | + query = "SELECT * FROM test WHERE `name` LIKE 't%' ORDER BY id" |
| 83 | + await cursor.execute(query) |
| 84 | + assert await cursor.fetchall() == ((2, "two"), (3, "three")) |
| 85 | + |
| 86 | + elasticapm_client.end_transaction(None, "test-transaction") |
| 87 | + finally: |
| 88 | + transactions = elasticapm_client.events[TRANSACTION] |
| 89 | + spans = elasticapm_client.spans_for_transaction(transactions[0]) |
| 90 | + span = spans[0] |
| 91 | + assert span["name"] == "SELECT FROM test" |
| 92 | + assert span["type"] == "db" |
| 93 | + assert span["subtype"] == "mysql" |
| 94 | + assert span["action"] == "query" |
| 95 | + assert "db" in span["context"] |
| 96 | + assert span["context"]["db"]["type"] == "sql" |
| 97 | + assert span["context"]["db"]["statement"] == query |
| 98 | + assert span["context"]["destination"] == { |
| 99 | + "address": os.environ.get("MYSQL_HOST", "localhost"), |
| 100 | + "port": default_ports.get("mysql"), |
| 101 | + "service": {"name": "mysql", "resource": "mysql", "type": "db"}, |
| 102 | + } |
0 commit comments