|
| 1 | +# Copyright The OpenTelemetry Authors |
| 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 | +""" |
| 16 | +The integration with pymssql supports the `pymssql`_ library and can be enabled |
| 17 | +by using ``PyMSSQLInstrumentor``. |
| 18 | +
|
| 19 | +.. _pymssql: https://pypi.org/project/pymssql/ |
| 20 | +
|
| 21 | +Usage |
| 22 | +----- |
| 23 | +
|
| 24 | +.. code:: python |
| 25 | +
|
| 26 | + import pymssql |
| 27 | + from opentelemetry.instrumentation.pymssql import PyMSSQLInstrumentor |
| 28 | +
|
| 29 | + PyMSSQLInstrumentor().instrument() |
| 30 | +
|
| 31 | + cnx = pymssql.connect(database="MSSQL_Database") |
| 32 | + cursor = cnx.cursor() |
| 33 | + cursor.execute("INSERT INTO test (testField) VALUES (123)" |
| 34 | + cnx.commit() |
| 35 | + cursor.close() |
| 36 | + cnx.close() |
| 37 | +
|
| 38 | +API |
| 39 | +--- |
| 40 | +""" |
| 41 | +from typing import Any, Callable, Collection, Dict, NamedTuple, Tuple |
| 42 | + |
| 43 | +import pymssql |
| 44 | + |
| 45 | +from opentelemetry.instrumentation import dbapi |
| 46 | +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor |
| 47 | +from opentelemetry.instrumentation.pymssql.package import _instruments |
| 48 | +from opentelemetry.instrumentation.pymssql.version import __version__ |
| 49 | + |
| 50 | +_DATABASE_SYSTEM = "mssql" |
| 51 | + |
| 52 | + |
| 53 | +class PyMSSQLConnectMethodArgsTuple(NamedTuple): |
| 54 | + server: str = None |
| 55 | + user: str = None |
| 56 | + password: str = None |
| 57 | + database: str = None |
| 58 | + timeout: int = None |
| 59 | + login_timeout: int = None |
| 60 | + charset: str = None |
| 61 | + as_dict: bool = None |
| 62 | + host: str = None |
| 63 | + appname: str = None |
| 64 | + port: str = None |
| 65 | + conn_properties: str = None |
| 66 | + autocommit: bool = None |
| 67 | + tds_version: str = None |
| 68 | + |
| 69 | + |
| 70 | +class PyMSSQLDatabaseApiIntegration(dbapi.DatabaseApiIntegration): |
| 71 | + def wrapped_connection( |
| 72 | + self, |
| 73 | + connect_method: Callable[..., Any], |
| 74 | + args: Tuple[Any, Any], |
| 75 | + kwargs: Dict[Any, Any], |
| 76 | + ): |
| 77 | + """Add object proxy to connection object.""" |
| 78 | + connection = connect_method(*args, **kwargs) |
| 79 | + connect_method_args = PyMSSQLConnectMethodArgsTuple(*args) |
| 80 | + |
| 81 | + self.name = self.database_system |
| 82 | + self.database = kwargs.get("database") or connect_method_args.database |
| 83 | + |
| 84 | + user = kwargs.get("user") or connect_method_args.user |
| 85 | + if user is not None: |
| 86 | + self.span_attributes["db.user"] = user |
| 87 | + |
| 88 | + port = kwargs.get("port") or connect_method_args.port |
| 89 | + host = kwargs.get("server") or connect_method_args.server |
| 90 | + if host is None: |
| 91 | + host = kwargs.get("host") or connect_method_args.host |
| 92 | + if host is not None: |
| 93 | + # The host string can include the port, separated by either a coma or |
| 94 | + # a column |
| 95 | + for sep in (":", ","): |
| 96 | + if sep in host: |
| 97 | + tokens = host.rsplit(sep) |
| 98 | + host = tokens[0] |
| 99 | + if len(tokens) > 1: |
| 100 | + port = tokens[1] |
| 101 | + if host is not None: |
| 102 | + self.span_attributes["net.peer.name"] = host |
| 103 | + if port is not None: |
| 104 | + self.span_attributes["net.peer.port"] = port |
| 105 | + |
| 106 | + charset = kwargs.get("charset") or connect_method_args.charset |
| 107 | + if charset is not None: |
| 108 | + self.span_attributes["db.charset"] = charset |
| 109 | + |
| 110 | + tds_version = ( |
| 111 | + kwargs.get("tds_version") or connect_method_args.tds_version |
| 112 | + ) |
| 113 | + if tds_version is not None: |
| 114 | + self.span_attributes["db.protocol.tds.version"] = tds_version |
| 115 | + |
| 116 | + return dbapi.get_traced_connection_proxy(connection, self) |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +class PyMSSQLInstrumentor(BaseInstrumentor): |
| 121 | + |
| 122 | + def instrumentation_dependencies(self) -> Collection[str]: |
| 123 | + return _instruments |
| 124 | + |
| 125 | + def _instrument(self, **kwargs): |
| 126 | + """Integrate with the pymssql library. |
| 127 | + https://github.com/pymssql/pymssql/ |
| 128 | + """ |
| 129 | + tracer_provider = kwargs.get("tracer_provider") |
| 130 | + |
| 131 | + dbapi.wrap_connect( |
| 132 | + __name__, |
| 133 | + pymssql, |
| 134 | + "connect", |
| 135 | + _DATABASE_SYSTEM, |
| 136 | + version=__version__, |
| 137 | + tracer_provider=tracer_provider, |
| 138 | + # pymssql does not keep the connection attributes in its connection object; |
| 139 | + # instead, we get the attributes from the connect method (which is done |
| 140 | + # via PyMSSQLDatabaseApiIntegration.wrapped_connection) |
| 141 | + db_api_integration_factory=PyMSSQLDatabaseApiIntegration, |
| 142 | + ) |
| 143 | + |
| 144 | + def _uninstrument(self, **kwargs): |
| 145 | + """ "Disable pymssql instrumentation""" |
| 146 | + dbapi.unwrap_connect(pymssql, "connect") |
| 147 | + |
| 148 | + @staticmethod |
| 149 | + def instrument_connection(connection, tracer_provider=None): |
| 150 | + """Enable instrumentation in a pymssql connection. |
| 151 | +
|
| 152 | + Args: |
| 153 | + connection: The connection to instrument. |
| 154 | + tracer_provider: The optional tracer provider to use. If omitted |
| 155 | + the current globally configured one is used. |
| 156 | +
|
| 157 | + Returns: |
| 158 | + An instrumented connection. |
| 159 | + """ |
| 160 | + |
| 161 | + return dbapi.instrument_connection( |
| 162 | + __name__, |
| 163 | + connection, |
| 164 | + _DATABASE_SYSTEM, |
| 165 | + version=__version__, |
| 166 | + tracer_provider=tracer_provider, |
| 167 | + ) |
| 168 | + |
| 169 | + @staticmethod |
| 170 | + def uninstrument_connection(connection): |
| 171 | + """Disable instrumentation in a pymssql connection. |
| 172 | +
|
| 173 | + Args: |
| 174 | + connection: The connection to uninstrument. |
| 175 | +
|
| 176 | + Returns: |
| 177 | + An uninstrumented connection. |
| 178 | + """ |
| 179 | + return dbapi.uninstrument_connection(connection) |
| 180 | + |
0 commit comments