Skip to content

Commit 7f3d422

Browse files
author
Daniel Rogers
committed
Fix dbapi connection instrument wrapper has no _sock member
Fixes #1353 Also: Fix the check for the connection already being instrumented in instrument_connection() Add tests for commit() and rollback() Add a couple missing docstring items. Add basepython to docker-tests to fix running the tests on macOS.
1 parent f58d16b commit 7f3d422

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
([#1245](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1245))
4040
- Add metric exporter for Prometheus Remote Write
4141
([#1359](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1359))
42+
- `opentelemetry-instrumentation-pymysql` Add tests for commit() and rollback().
43+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
4244

4345
### Fixed
4446

@@ -50,6 +52,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5052
([#1333](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1333))
5153
- Use resp.text instead of resp.body for Falcon 3 to avoid a deprecation warning.
5254
([#1412](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1412))
55+
- `opentelemetry-instrumentation-pymysql` Fix dbapi connection instrument wrapper has no _sock member.
56+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
57+
- `opentelemetry-instrumentation-dbapi` Fix the check for the connection already being instrumented in instrument_connection().
58+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
5359

5460
## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0-0.34b0) - 2022-09-26
5561

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def trace_integration(
7979
tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
8080
use. If omitted the current configured one is used.
8181
capture_parameters: Configure if db.statement.parameters should be captured.
82+
enable_commenter: Flag to enable/disable sqlcommenter.
83+
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
84+
default one is used.
8285
"""
8386
wrap_connect(
8487
__name__,
@@ -121,6 +124,8 @@ def wrap_connect(
121124
use. If omitted the current configured one is used.
122125
capture_parameters: Configure if db.statement.parameters should be captured.
123126
enable_commenter: Flag to enable/disable sqlcommenter.
127+
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
128+
default one is used.
124129
commenter_options: Configurations for tags to be appended at the sql query.
125130
126131
"""
@@ -197,7 +202,7 @@ def instrument_connection(
197202
Returns:
198203
An instrumented connection.
199204
"""
200-
if isinstance(connection, wrapt.ObjectProxy):
205+
if isinstance(connection, _TracedConnectionProxy):
201206
_logger.warning("Connection already instrumented")
202207
return connection
203208

@@ -326,6 +331,15 @@ class TracedConnectionProxy(type(connection), _TracedConnectionProxy):
326331
def __init__(self, connection):
327332
self._connection = connection
328333

334+
if hasattr(connection, "_sock"):
335+
self._sock = connection._sock
336+
if hasattr(connection, "_auth_plugin_name"):
337+
self._auth_plugin_name = connection._auth_plugin_name
338+
if hasattr(connection, "_closed"):
339+
self._closed = connection._closed
340+
if hasattr(connection, "_secure"):
341+
self._secure = connection._secure
342+
329343
def __getattr__(self, name):
330344
return object.__getattribute__(
331345
object.__getattribute__(self, "_connection"), name

tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py

+16
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,19 @@ def test_callproc(self):
109109
):
110110
self._cursor.callproc("test", ())
111111
self.validate_spans("test")
112+
113+
def test_commit(self):
114+
stmt = "INSERT INTO test (id) VALUES (%s)"
115+
with self._tracer.start_as_current_span("rootSpan"):
116+
data = (("4",), ("5",), ("6",))
117+
self._cursor.executemany(stmt, data)
118+
self._connection.commit()
119+
self.validate_spans("INSERT")
120+
121+
def test_rollback(self):
122+
stmt = "INSERT INTO test (id) VALUES (%s)"
123+
with self._tracer.start_as_current_span("rootSpan"):
124+
data = (("7",), ("8",), ("9",))
125+
self._cursor.executemany(stmt, data)
126+
self._connection.rollback()
127+
self.validate_spans("INSERT")

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ commands =
515515
python scripts/eachdist.py lint --check-only
516516

517517
[testenv:docker-tests]
518+
basepython: python3.9
518519
deps =
519520
pip >= 20.3.3
520521
pytest

0 commit comments

Comments
 (0)