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 SQLAlchemy 1.4 #568

Merged
merged 21 commits into from
Aug 4, 2021
Merged

Conversation

mnbbrown
Copy link
Contributor

@mnbbrown mnbbrown commented Jul 6, 2021

Description

This adds support for SQLAlchemy 1.4. SQLAlchemy 1.4 (and ultimately SA 2.0) introduces a new async variant of the Engine and Session classes (called AsyncEngine and AsyncSession respectively). More details are available at https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html

The current implementation uses thread locals to maintain a mapping between spans and threads. As thread locals are not available in an async/await environment this approach does not support the new AsyncEngine.

By attaching the span to the execution context state we avoid any interaction with concurrency primitives and instead rely on SQLAlchemy's underlying implementation.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

This uses the existing test suite for SQLAlchemy + an additional test to cover the async engine.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 6, 2021

CLA Signed

The committers are authorized under a signed CLA.

@mnbbrown mnbbrown marked this pull request as ready for review July 6, 2021 16:48
@mnbbrown mnbbrown requested review from a team, codeboten and srikanthccv and removed request for a team July 6, 2021 16:48
Copy link
Contributor

@owais owais left a comment

Choose a reason for hiding this comment

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

The explanation and goal sounds very good. Took a quick look and left some initial comments.

span = self.cursor_mapping.get(cursor, None)
if span is None:
return
context._span = span
Copy link
Contributor

Choose a reason for hiding this comment

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

use _otel_span instead?

if span.is_recording():
span.set_status(
Status(StatusCode.ERROR, str(context.original_exception),)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we expect the str() call to fail in some situations? if not, why put this under a try block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went back through history and it seems to have always been that way - but I can't imagine why. Even if it's None it will just become the "None". I've moved it out of the try catch block but want to check what should be passed into Status if no exception is available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -13,4 +13,4 @@
# limitations under the License.


_instruments = ("sqlalchemy",)
_instruments = ("sqlalchemy >= 1.3",)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the instrumentation not work with <1.3 after this change? That's a deal breaker IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

anything < 1.3 is marked EOL by SQLAlchemy (https://www.sqlalchemy.org/download.html). That said, I tested it with 1.2 and it worked so have removed the constraint.

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have a policy that drops support for unsupported software. Companies can easily get paid support for software that'd otherwise EOL'ed for example or continue to run old software without support. Personally I think we should not drop support until it gets painful to do it for very old versions. If it works for free, let's not drop support for it.

@@ -242,4 +243,6 @@ def insert_players(session):
close_all_sessions()

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 5)
self.assertEqual(
len(spans), 5 if self.VENDOR not in ["postgresql"] else 3
Copy link
Contributor

Choose a reason for hiding this comment

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

why did this change in this PR? Can we document the difference between vendors as a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mnbbrown
Copy link
Contributor Author

@owais i think I've addressed all your comments now :)

@codeboten codeboten requested a review from owais July 12, 2021 17:14
Copy link
Contributor

@owais owais left a comment

Choose a reason for hiding this comment

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

LGTM. Just one comment about testing the oldest and newest supported versions instead of the two latest ones.

tox.ini Outdated
@@ -177,6 +177,9 @@ deps =
elasticsearch6: elasticsearch>=6.0,<7.0
elasticsearch7: elasticsearch-dsl>=7.0,<8.0
elasticsearch7: elasticsearch>=7.0,<8.0
sqlalchemy13: sqlalchemy~=1.3,<1.4
Copy link
Contributor

Choose a reason for hiding this comment

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

can we change this to 1.2 or 1.1 so we test oldest reasonably supported version and the newest (1.4)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use 1.1.

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.

Code looks great, just one question around how the version is checked.

@@ -76,6 +89,13 @@ def _instrument(self, **kwargs):
"""
_w("sqlalchemy", "create_engine", _wrap_create_engine)
_w("sqlalchemy.engine", "create_engine", _wrap_create_engine)
if sqlalchemy.__version__.startswith("1.4"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this only work for version 1.4, what happens with version 1.5 is released? Would it make sense to use packaging to compare the version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - I've updated it to use packaging.

@mnbbrown mnbbrown requested review from codeboten and owais July 24, 2021 10:44
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.

👍

@codeboten codeboten closed this Jul 26, 2021
@codeboten codeboten reopened this Jul 26, 2021
@lzchen lzchen merged commit c8b6de6 into open-telemetry:main Aug 4, 2021
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.

5 participants