Skip to content

Commit

Permalink
Update tests to work with SQLAlchemy 2 (#2976)
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi authored and lzchen committed Nov 7, 2024
1 parent d4c3900 commit 402155e
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 33 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/test_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,24 @@ jobs:
- name: Run tests
run: tox -e py38-test-instrumentation-sqlalchemy-1 -- -ra

py38-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 3.8 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v5
with:
python-version: "3.8"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e py38-test-instrumentation-sqlalchemy-2 -- -ra

py39-test-instrumentation-sqlalchemy-1_ubuntu-latest:
name: instrumentation-sqlalchemy-1 3.9 Ubuntu
runs-on: ubuntu-latest
Expand All @@ -1186,6 +1204,24 @@ jobs:
- name: Run tests
run: tox -e py39-test-instrumentation-sqlalchemy-1 -- -ra

py39-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 3.9 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: "3.9"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e py39-test-instrumentation-sqlalchemy-2 -- -ra

py310-test-instrumentation-sqlalchemy-1_ubuntu-latest:
name: instrumentation-sqlalchemy-1 3.10 Ubuntu
runs-on: ubuntu-latest
Expand All @@ -1204,6 +1240,24 @@ jobs:
- name: Run tests
run: tox -e py310-test-instrumentation-sqlalchemy-1 -- -ra

py310-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 3.10 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e py310-test-instrumentation-sqlalchemy-2 -- -ra

py311-test-instrumentation-sqlalchemy-1_ubuntu-latest:
name: instrumentation-sqlalchemy-1 3.11 Ubuntu
runs-on: ubuntu-latest
Expand All @@ -1222,6 +1276,24 @@ jobs:
- name: Run tests
run: tox -e py311-test-instrumentation-sqlalchemy-1 -- -ra

py311-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 3.11 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e py311-test-instrumentation-sqlalchemy-2 -- -ra

py312-test-instrumentation-sqlalchemy-1_ubuntu-latest:
name: instrumentation-sqlalchemy-1 3.12 Ubuntu
runs-on: ubuntu-latest
Expand All @@ -1240,6 +1312,24 @@ jobs:
- name: Run tests
run: tox -e py312-test-instrumentation-sqlalchemy-1 -- -ra

py312-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 3.12 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e py312-test-instrumentation-sqlalchemy-2 -- -ra

pypy3-test-instrumentation-sqlalchemy-0_ubuntu-latest:
name: instrumentation-sqlalchemy-0 pypy-3.8 Ubuntu
runs-on: ubuntu-latest
Expand Down Expand Up @@ -1276,6 +1366,24 @@ jobs:
- name: Run tests
run: tox -e pypy3-test-instrumentation-sqlalchemy-1 -- -ra

pypy3-test-instrumentation-sqlalchemy-2_ubuntu-latest:
name: instrumentation-sqlalchemy-2 pypy-3.8 Ubuntu
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python pypy-3.8
uses: actions/setup-python@v5
with:
python-version: "pypy-3.8"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e pypy3-test-instrumentation-sqlalchemy-2 -- -ra

py38-test-instrumentation-redis_ubuntu-latest:
name: instrumentation-redis 3.8 Ubuntu
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2
([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976))

### Fixed

### Breaking changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
aiosqlite==0.20.0
asgiref==3.8.1
Deprecated==1.2.14
greenlet==3.1.1
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==7.4.4
SQLAlchemy==2.0.36
typing_extensions==4.12.2
wrapt==1.16.0
-e opentelemetry-instrumentation
-e instrumentation/opentelemetry-instrumentation-sqlalchemy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import pytest
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import (
create_engine,
text,
)

from opentelemetry import trace
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
Expand All @@ -43,12 +46,14 @@ def test_trace_integration(self):
tracer_provider=self.tracer_provider,
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute("/* leading comment */ SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
cnx.execute(text("/* leading comment */ SELECT 1 + 1;")).fetchall()
cnx.execute(
"/* leading comment */ SELECT 1 + 1; /* trailing comment */"
text(
"/* leading comment */ SELECT 1 + 1; /* trailing comment */"
)
).fetchall()
cnx.execute("SELECT 1 + 1; /* trailing comment */").fetchall()
cnx.execute(text("SELECT 1 + 1; /* trailing comment */")).fetchall()
spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 5)
Expand Down Expand Up @@ -76,9 +81,9 @@ def test_instrument_two_engines(self):
)

cnx_1 = engine_1.connect()
cnx_1.execute("SELECT 1 + 1;").fetchall()
cnx_1.execute(text("SELECT 1 + 1;")).fetchall()
cnx_2 = engine_2.connect()
cnx_2.execute("SELECT 1 + 1;").fetchall()
cnx_2.execute(text("SELECT 1 + 1;")).fetchall()

spans = self.memory_exporter.get_finished_spans()
# 2 queries + 2 engine connect
Expand Down Expand Up @@ -111,7 +116,7 @@ async def run():
engine=engine.sync_engine, tracer_provider=self.tracer_provider
)
async with engine.connect() as cnx:
await cnx.execute(sqlalchemy.text("SELECT 1 + 1;"))
await cnx.execute(text("SELECT 1 + 1;"))
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
# first span - the connection to the db
Expand Down Expand Up @@ -144,7 +149,7 @@ def test_not_recording(self):
tracer_provider=self.tracer_provider,
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
Expand All @@ -156,7 +161,7 @@ def test_create_engine_wrapper(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 2)
Expand Down Expand Up @@ -187,7 +192,7 @@ def test_create_engine_wrapper_enable_commenter(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
# sqlcommenter
self.assertRegex(
self.caplog.records[-2].getMessage(),
Expand All @@ -207,7 +212,7 @@ def test_create_engine_wrapper_enable_commenter_otel_values_false(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
# sqlcommenter
self.assertRegex(
self.caplog.records[-2].getMessage(),
Expand All @@ -233,7 +238,7 @@ def test_custom_tracer_provider(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 2)
Expand All @@ -258,7 +263,7 @@ async def run():

engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.connect() as cnx:
await cnx.execute(sqlalchemy.text("SELECT 1 + 1;"))
await cnx.execute(text("SELECT 1 + 1;"))
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
# first span - the connection to the db
Expand Down Expand Up @@ -299,7 +304,7 @@ async def run():

engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.connect() as cnx:
await cnx.execute(sqlalchemy.text("SELECT 1;"))
await cnx.execute(text("SELECT 1;"))
# sqlcommenter
self.assertRegex(
self.caplog.records[1].getMessage(),
Expand Down Expand Up @@ -330,7 +335,7 @@ async def run():

engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.connect() as cnx:
await cnx.execute(sqlalchemy.text("SELECT 1;"))
await cnx.execute(text("SELECT 1;"))
# sqlcommenter
self.assertRegex(
self.caplog.records[1].getMessage(),
Expand All @@ -346,7 +351,7 @@ def test_uninstrument(self):
tracer_provider=self.tracer_provider,
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 2)
Expand All @@ -359,10 +364,10 @@ def test_uninstrument(self):

self.memory_exporter.clear()
SQLAlchemyInstrumentor().uninstrument()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
engine2 = create_engine("sqlite:///:memory:")
cnx2 = engine2.connect()
cnx2.execute("SELECT 2 + 2;").fetchall()
cnx2.execute(text("SELECT 2 + 2;")).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

Expand All @@ -371,7 +376,7 @@ def test_uninstrument(self):
tracer_provider=self.tracer_provider,
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)

Expand All @@ -384,13 +389,13 @@ def test_uninstrument_without_engine(self):
engine = create_engine("sqlite:///:memory:")

cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)

self.memory_exporter.clear()
SQLAlchemyInstrumentor().uninstrument()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

Expand All @@ -401,7 +406,7 @@ def test_no_op_tracer_provider(self):
tracer_provider=trace.NoOpTracerProvider(),
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
cnx.execute(text("SELECT 1 + 1;")).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

Expand All @@ -420,7 +425,7 @@ def make_shortlived_engine():
# collection
weakref.finalize(engine, callback)
with engine.connect() as conn:
conn.execute("SELECT 1 + 1;").fetchall()
conn.execute(text("SELECT 1 + 1;")).fetchall()

for _ in range(0, 5):
make_shortlived_engine()
Expand Down
Loading

0 comments on commit 402155e

Please sign in to comment.