|
| 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 | +from newrelic.api.database_trace import register_database_client |
| 16 | +from newrelic.common.object_wrapper import wrap_object |
| 17 | +from newrelic.hooks.database_dbapi2 import ConnectionFactory as DBAPI2ConnectionFactory |
| 18 | +from newrelic.hooks.database_dbapi2 import ConnectionWrapper as DBAPI2ConnectionWrapper |
| 19 | +from newrelic.hooks.database_dbapi2 import CursorWrapper as DBAPI2CursorWrapper |
| 20 | +from newrelic.hooks.database_dbapi2_async import AsyncConnectionFactory as DBAPI2AsyncConnectionFactory |
| 21 | +from newrelic.hooks.database_dbapi2_async import AsyncConnectionWrapper as DBAPI2AsyncConnectionWrapper |
| 22 | +from newrelic.hooks.database_dbapi2_async import AsyncCursorWrapper as DBAPI2AsyncCursorWrapper |
| 23 | + |
| 24 | + |
| 25 | +class CursorWrapper(DBAPI2CursorWrapper): |
| 26 | + def __enter__(self): |
| 27 | + self.__wrapped__.__enter__() |
| 28 | + return self |
| 29 | + |
| 30 | + |
| 31 | +class ConnectionWrapper(DBAPI2ConnectionWrapper): |
| 32 | + __cursor_wrapper__ = CursorWrapper |
| 33 | + |
| 34 | + def __enter__(self): |
| 35 | + self.__wrapped__.__enter__() |
| 36 | + return self |
| 37 | + |
| 38 | + |
| 39 | +class ConnectionFactory(DBAPI2ConnectionFactory): |
| 40 | + __connection_wrapper__ = ConnectionWrapper |
| 41 | + |
| 42 | + |
| 43 | +class AsyncCursorWrapper(DBAPI2AsyncCursorWrapper): |
| 44 | + async def __aenter__(self): |
| 45 | + await self.__wrapped__.__aenter__() |
| 46 | + return self |
| 47 | + |
| 48 | + |
| 49 | +class AsyncConnectionWrapper(DBAPI2AsyncConnectionWrapper): |
| 50 | + __cursor_wrapper__ = AsyncCursorWrapper |
| 51 | + |
| 52 | + async def __aenter__(self): |
| 53 | + await self.__wrapped__.__aenter__() |
| 54 | + return self |
| 55 | + |
| 56 | + def __await__(self): |
| 57 | + # Handle bidirectional generator protocol using code from generator_wrapper |
| 58 | + g = self.__wrapped__.__await__() |
| 59 | + try: |
| 60 | + yielded = g.send(None) |
| 61 | + while True: |
| 62 | + try: |
| 63 | + sent = yield yielded |
| 64 | + except GeneratorExit: |
| 65 | + g.close() |
| 66 | + raise |
| 67 | + except BaseException as e: |
| 68 | + yielded = g.throw(e) |
| 69 | + else: |
| 70 | + yielded = g.send(sent) |
| 71 | + except StopIteration as e: |
| 72 | + # Catch the StopIteration and return the wrapped connection instead of the unwrapped. |
| 73 | + if e.value is self.__wrapped__: |
| 74 | + connection = self |
| 75 | + else: |
| 76 | + connection = e.value |
| 77 | + |
| 78 | + # Return here instead of raising StopIteration to properly follow generator protocol |
| 79 | + return connection |
| 80 | + |
| 81 | + |
| 82 | +class AsyncConnectionFactory(DBAPI2AsyncConnectionFactory): |
| 83 | + __connection_wrapper__ = AsyncConnectionWrapper |
| 84 | + |
| 85 | + # Use the synchronous __call__ method as connection_async() is synchronous in oracledb. |
| 86 | + __call__ = DBAPI2ConnectionFactory.__call__ |
| 87 | + |
| 88 | + |
| 89 | +def instance_info(args, kwargs): |
| 90 | + from oracledb import ConnectParams |
| 91 | + |
| 92 | + dsn = args[0] if args else None |
| 93 | + |
| 94 | + host = None |
| 95 | + port = None |
| 96 | + service_name = None |
| 97 | + |
| 98 | + params_from_kwarg = kwargs.pop("params", None) |
| 99 | + |
| 100 | + params_from_dsn = None |
| 101 | + if dsn: |
| 102 | + try: |
| 103 | + params_from_dsn = ConnectParams() |
| 104 | + if "@" in dsn: |
| 105 | + _, _, connect_string = params_from_dsn.parse_dsn_with_credentials(dsn) |
| 106 | + else: |
| 107 | + connect_string = dsn |
| 108 | + params_from_dsn.parse_connect_string(connect_string) |
| 109 | + except Exception: |
| 110 | + params_from_dsn = None |
| 111 | + |
| 112 | + host = ( |
| 113 | + getattr(params_from_kwarg, "host", None) |
| 114 | + or kwargs.get("host", None) |
| 115 | + or getattr(params_from_dsn, "host", None) |
| 116 | + or "unknown" |
| 117 | + ) |
| 118 | + port = str( |
| 119 | + getattr(params_from_kwarg, "port", None) |
| 120 | + or kwargs.get("port", None) |
| 121 | + or getattr(params_from_dsn, "port", None) |
| 122 | + or "1521" |
| 123 | + ) |
| 124 | + service_name = ( |
| 125 | + getattr(params_from_kwarg, "service_name", None) |
| 126 | + or kwargs.get("service_name", None) |
| 127 | + or getattr(params_from_dsn, "service_name", None) |
| 128 | + or "unknown" |
| 129 | + ) |
| 130 | + |
| 131 | + return host, port, service_name |
| 132 | + |
| 133 | + |
| 134 | +def instrument_oracledb(module): |
| 135 | + register_database_client( |
| 136 | + module, database_product="Oracle", quoting_style="single+oracle", instance_info=instance_info |
| 137 | + ) |
| 138 | + |
| 139 | + if hasattr(module, "connect"): |
| 140 | + wrap_object(module, "connect", ConnectionFactory, (module,)) |
| 141 | + |
| 142 | + if hasattr(module, "connect_async"): |
| 143 | + wrap_object(module, "connect_async", AsyncConnectionFactory, (module,)) |
0 commit comments