Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conn.commit() raise InterfaceError because connection instrument wrapper has no _sock member #1353

Closed
kchow1985 opened this issue Sep 26, 2022 · 5 comments · Fixed by #1424
Closed
Assignees
Labels
bug Something isn't working

Comments

@kchow1985
Copy link

kchow1985 commented Sep 26, 2022

Describe your environment Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.

instrumentation version: opentelemetry-instrumentation-pymysql 0.33b0

Steps to reproduce
When using opentelemetry-instrument command to automatically instrument pymysql

conn = pymysql.connect(...)

with conn.cursor() as cursor:
  cursor.execute(...) # run any insert statement
  conn.commit() # this line will trigger InterfaceError

Current bandaid solution:

conn = pymysql.connect(...)
conn._sock = conn._connection._sock # add this line to work around

with conn.cursor() as cursor:
  cursor.execute(...)
  conn.commit() # this line will no longer trigger InterfaceError

Tracing down the stack will show that on line 792 of the pymysql/connections/.py, the Exception will be thrown because self now refers to the wrapper instead of the internal connection object:

    def _execute_command(self, command, sql):
        """
        :raise InterfaceError: If the connection is closed.
        :raise ValueError: If no username was specified.
        """
        if not self._sock:
            raise err.InterfaceError(0, "")
        ...

If I do not instrument pymysql then this will not happen.

@kchow1985 kchow1985 added the bug Something isn't working label Sep 26, 2022
@rogersd
Copy link
Contributor

rogersd commented Oct 6, 2022

@ocelotl I'm having this same issue and it appears to be caused by #1097

This may also be the cause of #1213

Also, should this be changed?

diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py
index 0645d4c5..8bca74dd 100644
--- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py
@@ -197,7 +197,7 @@ def instrument_connection(
     Returns:
         An instrumented connection.
     """
-    if isinstance(connection, wrapt.ObjectProxy):
+    if isinstance(connection, _TracedConnectionProxy):
         _logger.warning("Connection already instrumented")
         return connection

@yabigfish
Copy link

PyMySQL specific

# file: PyMySQL/pymysql/connections.py

class Connection:
    # ...
    # class attribute
    _sock = None
    # ...

    def __init__(self):
        # ...
        # instance attribute
        # self._sock assigned to new socket by self.connect()
        # ...

_sock is a class attribute, also an instance attribute, which is used by the method connection.commit and connection.close and others

Before #1097

# file: opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

class TracedConnectionProxy(wrapt.ObjectProxy):
    def __init__(self, connection, *args, **kwargs):
        wrapt.ObjectProxy.__init__(self, connection)

accessing self._sock via wrapt.ObjectProxy to the underlying instance attribute connection._sock, which is good.

After #1097

class TracedConnectionProxy(type(connection), _TracedConnectionProxy):
    def __init__(self, connection):
        self._connection = connection

self._sock is the class attribute Connection._sock (by inheriting type(connection)), which is bad, because Connection._sock is None

@rogersd
Copy link
Contributor

rogersd commented Oct 10, 2022

@srikanthccv Any thoughts here?

@ocelotl
Copy link
Contributor

ocelotl commented Oct 24, 2022

Will look into this, thanks for reporting ✌️

@ocelotl ocelotl self-assigned this Oct 24, 2022
@rogersd
Copy link
Contributor

rogersd commented Oct 24, 2022

@ocelotl Would be great if the fix could make it in to the next release. I (and I'm sure many others) am blocked from upgrading until this is fixed.

rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Oct 31, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Oct 31, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Oct 31, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Nov 3, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Nov 3, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Nov 3, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Nov 4, 2022
Fixes open-telemetry#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.
rogersd pushed a commit to rogersd/opentelemetry-python-contrib that referenced this issue Nov 7, 2022
Fixes open-telemetry#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.
ocelotl pushed a commit that referenced this issue Nov 7, 2022
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.
saartochner-lumigo pushed a commit to lumigo-io/opentelemetry-python-contrib that referenced this issue Nov 13, 2022
…emetry#1424)

Fixes open-telemetry#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.
saartochner-lumigo pushed a commit to lumigo-io/opentelemetry-python-contrib that referenced this issue Nov 13, 2022
…emetry#1424)

Fixes open-telemetry#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants