Skip to content

Commit 7e6e190

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 5c67dee commit 7e6e190

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

CHANGELOG.md

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

4749
### Fixed
4850

@@ -54,6 +56,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5456
([#1333](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1333))
5557
- Use resp.text instead of resp.body for Falcon 3 to avoid a deprecation warning.
5658
([#1412](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1412))
59+
- `opentelemetry-instrumentation-pymysql` Fix dbapi connection instrument wrapper has no _sock member.
60+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
61+
- `opentelemetry-instrumentation-dbapi` Fix the check for the connection already being instrumented in instrument_connection().
62+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
5763

5864
## Version 1.13.0/0.34b0 (2022-09-26)
5965

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

+14-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

@@ -331,6 +336,14 @@ def __getattr__(self, name):
331336
object.__getattribute__(self, "_connection"), name
332337
)
333338

339+
def __getattribute__(self, name):
340+
if object.__getattribute__(self, name):
341+
return object.__getattribute__(self, name)
342+
else:
343+
return object.__getattribute__(
344+
object.__getattribute__(self, "_connection"), name
345+
)
346+
334347
def cursor(self, *args, **kwargs):
335348
return get_traced_cursor_proxy(
336349
self._connection.cursor(*args, **kwargs), db_api_integration

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)