|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import aiomysql |
| 16 | +from testing_support.db_settings import mysql_settings |
| 17 | +from testing_support.util import instance_hostname |
| 18 | +from testing_support.validators.validate_database_trace_inputs import ( |
| 19 | + validate_database_trace_inputs, |
| 20 | +) |
| 21 | +from testing_support.validators.validate_transaction_metrics import ( |
| 22 | + validate_transaction_metrics, |
| 23 | +) |
| 24 | + |
| 25 | +from newrelic.api.background_task import background_task |
| 26 | + |
| 27 | +DB_SETTINGS = mysql_settings()[0] |
| 28 | +TABLE_NAME = f"datastore_aiomysql_{DB_SETTINGS['namespace']}" |
| 29 | +PROCEDURE_NAME = f"hello_{DB_SETTINGS['namespace']}" |
| 30 | + |
| 31 | +HOST = instance_hostname(DB_SETTINGS["host"]) |
| 32 | +PORT = DB_SETTINGS["port"] |
| 33 | + |
| 34 | + |
| 35 | +async def execute_db_calls_with_cursor(cursor): |
| 36 | + await cursor.execute(f"""drop table if exists {TABLE_NAME}""") |
| 37 | + |
| 38 | + await cursor.execute(f"create table {TABLE_NAME} (a integer, b real, c text)") |
| 39 | + |
| 40 | + await cursor.executemany( |
| 41 | + f"insert into {TABLE_NAME} values (%s, %s, %s)", |
| 42 | + [(1, 1.0, "1.0"), (2, 2.2, "2.2"), (3, 3.3, "3.3")], |
| 43 | + ) |
| 44 | + |
| 45 | + await cursor.execute(f"""select * from {TABLE_NAME}""") |
| 46 | + |
| 47 | + async for _ in cursor: |
| 48 | + pass |
| 49 | + |
| 50 | + await cursor.execute(f"update {TABLE_NAME} set a=%s, b=%s, c=%s where a=%s", (4, 4.0, "4.0", 1)) |
| 51 | + |
| 52 | + await cursor.execute(f"""delete from {TABLE_NAME} where a=2""") |
| 53 | + await cursor.execute(f"""drop procedure if exists {PROCEDURE_NAME}""") |
| 54 | + await cursor.execute( |
| 55 | + f"""CREATE PROCEDURE {PROCEDURE_NAME}() |
| 56 | + BEGIN |
| 57 | + SELECT 'Hello World!'; |
| 58 | + END""" |
| 59 | + ) |
| 60 | + |
| 61 | + await cursor.callproc(PROCEDURE_NAME) |
| 62 | + |
| 63 | + |
| 64 | +SCOPED_METRICS = [ |
| 65 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/select", 1), |
| 66 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/insert", 1), |
| 67 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/update", 1), |
| 68 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/delete", 1), |
| 69 | + ("Datastore/operation/MySQL/drop", 2), |
| 70 | + ("Datastore/operation/MySQL/create", 2), |
| 71 | + (f"Datastore/statement/MySQL/{PROCEDURE_NAME}/call", 1), |
| 72 | + ("Datastore/operation/MySQL/commit", 2), |
| 73 | + ("Datastore/operation/MySQL/rollback", 1), |
| 74 | +] |
| 75 | + |
| 76 | +ROLLUP_METRICS = [ |
| 77 | + ("Datastore/all", 13), |
| 78 | + ("Datastore/allOther", 13), |
| 79 | + ("Datastore/MySQL/all", 13), |
| 80 | + ("Datastore/MySQL/allOther", 13), |
| 81 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/select", 1), |
| 82 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/insert", 1), |
| 83 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/update", 1), |
| 84 | + (f"Datastore/statement/MySQL/{TABLE_NAME}/delete", 1), |
| 85 | + ("Datastore/operation/MySQL/select", 1), |
| 86 | + ("Datastore/operation/MySQL/insert", 1), |
| 87 | + ("Datastore/operation/MySQL/update", 1), |
| 88 | + ("Datastore/operation/MySQL/delete", 1), |
| 89 | + (f"Datastore/statement/MySQL/{PROCEDURE_NAME}/call", 1), |
| 90 | + ("Datastore/operation/MySQL/call", 1), |
| 91 | + ("Datastore/operation/MySQL/drop", 2), |
| 92 | + ("Datastore/operation/MySQL/create", 2), |
| 93 | + ("Datastore/operation/MySQL/commit", 2), |
| 94 | + ("Datastore/operation/MySQL/rollback", 1), |
| 95 | + (f"Datastore/instance/MySQL/{HOST}/{PORT}", 12), |
| 96 | +] |
| 97 | + |
| 98 | + |
| 99 | +@validate_transaction_metrics( |
| 100 | + "test_database:test_execute_via_connection", |
| 101 | + scoped_metrics=list(SCOPED_METRICS) + [("Function/aiomysql.connection:connect", 1)], |
| 102 | + rollup_metrics=list(ROLLUP_METRICS) + [("Function/aiomysql.connection:connect", 1)], |
| 103 | + background_task=True, |
| 104 | +) |
| 105 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 106 | +@background_task() |
| 107 | +def test_execute_via_connection(loop): |
| 108 | + async def _test(): |
| 109 | + connection = await aiomysql.connect( |
| 110 | + db=DB_SETTINGS["name"], |
| 111 | + user=DB_SETTINGS["user"], |
| 112 | + password=DB_SETTINGS["password"], |
| 113 | + host=DB_SETTINGS["host"], |
| 114 | + port=DB_SETTINGS["port"], |
| 115 | + ) |
| 116 | + |
| 117 | + async with connection: |
| 118 | + async with connection.cursor() as cursor: |
| 119 | + await execute_db_calls_with_cursor(cursor) |
| 120 | + |
| 121 | + await connection.commit() |
| 122 | + await connection.rollback() |
| 123 | + await connection.commit() |
| 124 | + |
| 125 | + loop.run_until_complete(_test()) |
| 126 | + |
| 127 | + |
| 128 | +@validate_transaction_metrics( |
| 129 | + "test_database:test_execute_via_pool", |
| 130 | + scoped_metrics=list(SCOPED_METRICS) + [("Function/aiomysql.pool:Pool._acquire", 1)], |
| 131 | + rollup_metrics=list(ROLLUP_METRICS) + [("Function/aiomysql.pool:Pool._acquire", 1)], |
| 132 | + background_task=True, |
| 133 | +) |
| 134 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 135 | +@background_task() |
| 136 | +def test_execute_via_pool(loop): |
| 137 | + async def _test(): |
| 138 | + pool = await aiomysql.create_pool( |
| 139 | + db=DB_SETTINGS["name"], |
| 140 | + user=DB_SETTINGS["user"], |
| 141 | + password=DB_SETTINGS["password"], |
| 142 | + host=DB_SETTINGS["host"], |
| 143 | + port=DB_SETTINGS["port"], |
| 144 | + loop=loop, |
| 145 | + ) |
| 146 | + async with pool.acquire() as connection: |
| 147 | + async with connection.cursor() as cursor: |
| 148 | + await execute_db_calls_with_cursor(cursor) |
| 149 | + |
| 150 | + await connection.commit() |
| 151 | + await connection.rollback() |
| 152 | + await connection.commit() |
| 153 | + |
| 154 | + pool.close() |
| 155 | + await pool.wait_closed() |
| 156 | + |
| 157 | + loop.run_until_complete(_test()) |
0 commit comments