Skip to content

Commit 40e4e2e

Browse files
author
Dan Rogers
authored
Fix dbapi connection instrument wrapper has no _sock member (#1424)
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 f994e14 commit 40e4e2e

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- `opentelemetry-instrumentation-pymysql` Add tests for commit() and rollback().
13+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
14+
1015
### Fixed
1116

1217
- Fix bug in Urllib instrumentation - add status code to span attributes only if the status code is not None.
1318
([#1430](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1430))
19+
- `opentelemetry-instrumentation-pymysql` Fix dbapi connection instrument wrapper has no _sock member.
20+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
21+
- `opentelemetry-instrumentation-dbapi` Fix the check for the connection already being instrumented in instrument_connection().
22+
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
1423

1524
## Version 1.14.0/0.35b0 (2022-11-03)
1625

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