Skip to content

Commit

Permalink
Fix dbapi connection instrument wrapper has no _sock member
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Daniel Rogers committed Oct 31, 2022
1 parent f58d16b commit 7f3d422
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1245](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1245))
- Add metric exporter for Prometheus Remote Write
([#1359](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1359))
- `opentelemetry-instrumentation-pymysql` Add tests for commit() and rollback().
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))

### Fixed

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def trace_integration(
tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
use. If omitted the current configured one is used.
capture_parameters: Configure if db.statement.parameters should be captured.
enable_commenter: Flag to enable/disable sqlcommenter.
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
default one is used.
"""
wrap_connect(
__name__,
Expand Down Expand Up @@ -121,6 +124,8 @@ def wrap_connect(
use. If omitted the current configured one is used.
capture_parameters: Configure if db.statement.parameters should be captured.
enable_commenter: Flag to enable/disable sqlcommenter.
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
default one is used.
commenter_options: Configurations for tags to be appended at the sql query.
"""
Expand Down Expand Up @@ -197,7 +202,7 @@ def instrument_connection(
Returns:
An instrumented connection.
"""
if isinstance(connection, wrapt.ObjectProxy):
if isinstance(connection, _TracedConnectionProxy):
_logger.warning("Connection already instrumented")
return connection

Expand Down Expand Up @@ -326,6 +331,15 @@ class TracedConnectionProxy(type(connection), _TracedConnectionProxy):
def __init__(self, connection):
self._connection = connection

if hasattr(connection, "_sock"):
self._sock = connection._sock
if hasattr(connection, "_auth_plugin_name"):
self._auth_plugin_name = connection._auth_plugin_name
if hasattr(connection, "_closed"):
self._closed = connection._closed
if hasattr(connection, "_secure"):
self._secure = connection._secure

def __getattr__(self, name):
return object.__getattribute__(
object.__getattribute__(self, "_connection"), name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,19 @@ def test_callproc(self):
):
self._cursor.callproc("test", ())
self.validate_spans("test")

def test_commit(self):
stmt = "INSERT INTO test (id) VALUES (%s)"
with self._tracer.start_as_current_span("rootSpan"):
data = (("4",), ("5",), ("6",))
self._cursor.executemany(stmt, data)
self._connection.commit()
self.validate_spans("INSERT")

def test_rollback(self):
stmt = "INSERT INTO test (id) VALUES (%s)"
with self._tracer.start_as_current_span("rootSpan"):
data = (("7",), ("8",), ("9",))
self._cursor.executemany(stmt, data)
self._connection.rollback()
self.validate_spans("INSERT")
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ commands =
python scripts/eachdist.py lint --check-only

[testenv:docker-tests]
basepython: python3.9
deps =
pip >= 20.3.3
pytest
Expand Down

0 comments on commit 7f3d422

Please sign in to comment.