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

Add support for db cursors and connections in context managers #1028

Merged
merged 11 commits into from
Sep 2, 2020

Conversation

ffe4
Copy link
Contributor

@ffe4 ffe4 commented Aug 20, 2020

Description

This PR is a replacement for #821 with added docker tests.

I also added the -v flag to docker-compose down in the tox config to clean up anonymous volumes that get left behind and not reused.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Here is an example snippet that will not report tracing without this patch:

with psycopg2.connect(...) as conn, conn.cursor() as cursor:
    cursor.execute("select 1;")
@ffe4 ffe4 requested a review from a team August 20, 2020 18:32
@ffe4
Copy link
Contributor Author

ffe4 commented Aug 20, 2020

Kind of forgot there were other drivers I might want to add the same test for. However, running into a test fail with that, so I'll mark as draft until I've looked into that.

@ffe4 ffe4 marked this pull request as draft August 20, 2020 19:16
ffe4 added 4 commits August 21, 2020 16:42
Since anonymous volumes do not get reused, each test run left behind new volumes.
The same function exists in the aiopg tests under a different name.
@ffe4 ffe4 force-pushed the pr-821-docker-test branch from 735a7f0 to 7f43a85 Compare August 21, 2020 14:48
@ffe4
Copy link
Contributor Author

ffe4 commented Aug 21, 2020

Turns out the error was just due to mysql and pymysql differing in whether they actually call close() during __exit__.

The wrapper is pretty straight-forward, so I was not sure whether to add the tests to all packages and stopped half-way.

I also removed some unused imports and renamed a helper function for consistency.

@ffe4 ffe4 marked this pull request as ready for review August 21, 2020 14:59
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thanks for picking this up. Could you add a note in the dbapi changelog?

@ffe4
Copy link
Contributor Author

ffe4 commented Aug 21, 2020

Done.

@codeboten
Copy link
Contributor

Awesome! Thanks @ffe4

Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR just for dbapi? If you're willing, looks like asyncpg also supports context managers for transactions https://magicstack.github.io/asyncpg/current/api/index.html?highlight=context%20manager#transactions

@ffe4
Copy link
Contributor Author

ffe4 commented Sep 1, 2020

Is this PR just for dbapi? If you're willing, looks like asyncpg also supports context managers for transactions https://magicstack.github.io/asyncpg/current/api/index.html?highlight=context%20manager#transactions

The asyncpg docker test uses a context manager, so I assume that should work as intended already.

L77

async with self._connection.transaction():
    await self._connection.execute("SELECT 42;")

@codeboten
Copy link
Contributor

@aabmass if you can confirm you're ok with @ffe4's responses, I can merge this

@aabmass
Copy link
Member

aabmass commented Sep 2, 2020

@codeboten LGTM

The asyncpg docker test uses a context manager, so I assume that should work as intended already.

Looks like this is covered already since it just executes the BEGIN; and COMMIT; which is already instrumented 👍

@codeboten codeboten merged commit 2245558 into open-telemetry:master Sep 2, 2020
@carlbordum
Copy link
Contributor

Thank you for finishing this, @ffe4

@ffe4 ffe4 deleted the pr-821-docker-test branch September 2, 2020 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants