Skip to content

Commit 9eb3dfb

Browse files
tammy-baylis-swixrmx
authored andcommitted
Update tests to work with SQLAlchemy 2 (open-telemetry#2976)
1 parent 870ff2a commit 9eb3dfb

File tree

6 files changed

+167
-33
lines changed

6 files changed

+167
-33
lines changed

Diff for: .github/workflows/test_1.yml

+108
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,24 @@ jobs:
14921492
- name: Run tests
14931493
run: tox -e py38-test-instrumentation-sqlalchemy-1 -- -ra
14941494

1495+
py38-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1496+
name: instrumentation-sqlalchemy-2 3.8 Ubuntu
1497+
runs-on: ubuntu-latest
1498+
steps:
1499+
- name: Checkout repo @ SHA - ${{ github.sha }}
1500+
uses: actions/checkout@v4
1501+
1502+
- name: Set up Python 3.8
1503+
uses: actions/setup-python@v5
1504+
with:
1505+
python-version: "3.8"
1506+
1507+
- name: Install tox
1508+
run: pip install tox
1509+
1510+
- name: Run tests
1511+
run: tox -e py38-test-instrumentation-sqlalchemy-2 -- -ra
1512+
14951513
py39-test-instrumentation-sqlalchemy-1_ubuntu-latest:
14961514
name: instrumentation-sqlalchemy-1 3.9 Ubuntu
14971515
runs-on: ubuntu-latest
@@ -1510,6 +1528,24 @@ jobs:
15101528
- name: Run tests
15111529
run: tox -e py39-test-instrumentation-sqlalchemy-1 -- -ra
15121530

1531+
py39-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1532+
name: instrumentation-sqlalchemy-2 3.9 Ubuntu
1533+
runs-on: ubuntu-latest
1534+
steps:
1535+
- name: Checkout repo @ SHA - ${{ github.sha }}
1536+
uses: actions/checkout@v4
1537+
1538+
- name: Set up Python 3.9
1539+
uses: actions/setup-python@v5
1540+
with:
1541+
python-version: "3.9"
1542+
1543+
- name: Install tox
1544+
run: pip install tox
1545+
1546+
- name: Run tests
1547+
run: tox -e py39-test-instrumentation-sqlalchemy-2 -- -ra
1548+
15131549
py310-test-instrumentation-sqlalchemy-1_ubuntu-latest:
15141550
name: instrumentation-sqlalchemy-1 3.10 Ubuntu
15151551
runs-on: ubuntu-latest
@@ -1528,6 +1564,24 @@ jobs:
15281564
- name: Run tests
15291565
run: tox -e py310-test-instrumentation-sqlalchemy-1 -- -ra
15301566

1567+
py310-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1568+
name: instrumentation-sqlalchemy-2 3.10 Ubuntu
1569+
runs-on: ubuntu-latest
1570+
steps:
1571+
- name: Checkout repo @ SHA - ${{ github.sha }}
1572+
uses: actions/checkout@v4
1573+
1574+
- name: Set up Python 3.10
1575+
uses: actions/setup-python@v5
1576+
with:
1577+
python-version: "3.10"
1578+
1579+
- name: Install tox
1580+
run: pip install tox
1581+
1582+
- name: Run tests
1583+
run: tox -e py310-test-instrumentation-sqlalchemy-2 -- -ra
1584+
15311585
py311-test-instrumentation-sqlalchemy-1_ubuntu-latest:
15321586
name: instrumentation-sqlalchemy-1 3.11 Ubuntu
15331587
runs-on: ubuntu-latest
@@ -1546,6 +1600,24 @@ jobs:
15461600
- name: Run tests
15471601
run: tox -e py311-test-instrumentation-sqlalchemy-1 -- -ra
15481602

1603+
py311-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1604+
name: instrumentation-sqlalchemy-2 3.11 Ubuntu
1605+
runs-on: ubuntu-latest
1606+
steps:
1607+
- name: Checkout repo @ SHA - ${{ github.sha }}
1608+
uses: actions/checkout@v4
1609+
1610+
- name: Set up Python 3.11
1611+
uses: actions/setup-python@v5
1612+
with:
1613+
python-version: "3.11"
1614+
1615+
- name: Install tox
1616+
run: pip install tox
1617+
1618+
- name: Run tests
1619+
run: tox -e py311-test-instrumentation-sqlalchemy-2 -- -ra
1620+
15491621
py312-test-instrumentation-sqlalchemy-1_ubuntu-latest:
15501622
name: instrumentation-sqlalchemy-1 3.12 Ubuntu
15511623
runs-on: ubuntu-latest
@@ -1564,6 +1636,24 @@ jobs:
15641636
- name: Run tests
15651637
run: tox -e py312-test-instrumentation-sqlalchemy-1 -- -ra
15661638

1639+
py312-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1640+
name: instrumentation-sqlalchemy-2 3.12 Ubuntu
1641+
runs-on: ubuntu-latest
1642+
steps:
1643+
- name: Checkout repo @ SHA - ${{ github.sha }}
1644+
uses: actions/checkout@v4
1645+
1646+
- name: Set up Python 3.12
1647+
uses: actions/setup-python@v5
1648+
with:
1649+
python-version: "3.12"
1650+
1651+
- name: Install tox
1652+
run: pip install tox
1653+
1654+
- name: Run tests
1655+
run: tox -e py312-test-instrumentation-sqlalchemy-2 -- -ra
1656+
15671657
pypy3-test-instrumentation-sqlalchemy-0_ubuntu-latest:
15681658
name: instrumentation-sqlalchemy-0 pypy-3.8 Ubuntu
15691659
runs-on: ubuntu-latest
@@ -1600,6 +1690,24 @@ jobs:
16001690
- name: Run tests
16011691
run: tox -e pypy3-test-instrumentation-sqlalchemy-1 -- -ra
16021692

1693+
pypy3-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1694+
name: instrumentation-sqlalchemy-2 pypy-3.8 Ubuntu
1695+
runs-on: ubuntu-latest
1696+
steps:
1697+
- name: Checkout repo @ SHA - ${{ github.sha }}
1698+
uses: actions/checkout@v4
1699+
1700+
- name: Set up Python pypy-3.8
1701+
uses: actions/setup-python@v5
1702+
with:
1703+
python-version: "pypy-3.8"
1704+
1705+
- name: Install tox
1706+
run: pip install tox
1707+
1708+
- name: Run tests
1709+
run: tox -e pypy3-test-instrumentation-sqlalchemy-2 -- -ra
1710+
16031711
py38-test-instrumentation-redis_ubuntu-latest:
16041712
name: instrumentation-redis 3.8 Ubuntu
16051713
runs-on: ubuntu-latest

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

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

1821
### Breaking changes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
aiosqlite==0.20.0
2+
asgiref==3.8.1
3+
Deprecated==1.2.14
4+
greenlet==3.1.1
5+
iniconfig==2.0.0
6+
packaging==24.1
7+
pluggy==1.5.0
8+
pytest==7.4.4
9+
SQLAlchemy==2.0.36
10+
typing_extensions==4.12.2
11+
wrapt==1.16.0
12+
-e opentelemetry-instrumentation
13+
-e instrumentation/opentelemetry-instrumentation-sqlalchemy

Diff for: instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import pytest
1919
import sqlalchemy
20-
from sqlalchemy import create_engine
20+
from sqlalchemy import (
21+
create_engine,
22+
text,
23+
)
2124

2225
from opentelemetry import trace
2326
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
@@ -43,12 +46,14 @@ def test_trace_integration(self):
4346
tracer_provider=self.tracer_provider,
4447
)
4548
cnx = engine.connect()
46-
cnx.execute("SELECT 1 + 1;").fetchall()
47-
cnx.execute("/* leading comment */ SELECT 1 + 1;").fetchall()
49+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
50+
cnx.execute(text("/* leading comment */ SELECT 1 + 1;")).fetchall()
4851
cnx.execute(
49-
"/* leading comment */ SELECT 1 + 1; /* trailing comment */"
52+
text(
53+
"/* leading comment */ SELECT 1 + 1; /* trailing comment */"
54+
)
5055
).fetchall()
51-
cnx.execute("SELECT 1 + 1; /* trailing comment */").fetchall()
56+
cnx.execute(text("SELECT 1 + 1; /* trailing comment */")).fetchall()
5257
spans = self.memory_exporter.get_finished_spans()
5358

5459
self.assertEqual(len(spans), 5)
@@ -76,9 +81,9 @@ def test_instrument_two_engines(self):
7681
)
7782

7883
cnx_1 = engine_1.connect()
79-
cnx_1.execute("SELECT 1 + 1;").fetchall()
84+
cnx_1.execute(text("SELECT 1 + 1;")).fetchall()
8085
cnx_2 = engine_2.connect()
81-
cnx_2.execute("SELECT 1 + 1;").fetchall()
86+
cnx_2.execute(text("SELECT 1 + 1;")).fetchall()
8287

8388
spans = self.memory_exporter.get_finished_spans()
8489
# 2 queries + 2 engine connect
@@ -111,7 +116,7 @@ async def run():
111116
engine=engine.sync_engine, tracer_provider=self.tracer_provider
112117
)
113118
async with engine.connect() as cnx:
114-
await cnx.execute(sqlalchemy.text("SELECT 1 + 1;"))
119+
await cnx.execute(text("SELECT 1 + 1;"))
115120
spans = self.memory_exporter.get_finished_spans()
116121
self.assertEqual(len(spans), 2)
117122
# first span - the connection to the db
@@ -144,7 +149,7 @@ def test_not_recording(self):
144149
tracer_provider=self.tracer_provider,
145150
)
146151
cnx = engine.connect()
147-
cnx.execute("SELECT 1 + 1;").fetchall()
152+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
148153
self.assertFalse(mock_span.is_recording())
149154
self.assertTrue(mock_span.is_recording.called)
150155
self.assertFalse(mock_span.set_attribute.called)
@@ -156,7 +161,7 @@ def test_create_engine_wrapper(self):
156161

157162
engine = create_engine("sqlite:///:memory:")
158163
cnx = engine.connect()
159-
cnx.execute("SELECT 1 + 1;").fetchall()
164+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
160165
spans = self.memory_exporter.get_finished_spans()
161166

162167
self.assertEqual(len(spans), 2)
@@ -187,7 +192,7 @@ def test_create_engine_wrapper_enable_commenter(self):
187192

188193
engine = create_engine("sqlite:///:memory:")
189194
cnx = engine.connect()
190-
cnx.execute("SELECT 1;").fetchall()
195+
cnx.execute(text("SELECT 1;")).fetchall()
191196
# sqlcommenter
192197
self.assertRegex(
193198
self.caplog.records[-2].getMessage(),
@@ -207,7 +212,7 @@ def test_create_engine_wrapper_enable_commenter_otel_values_false(self):
207212

208213
engine = create_engine("sqlite:///:memory:")
209214
cnx = engine.connect()
210-
cnx.execute("SELECT 1;").fetchall()
215+
cnx.execute(text("SELECT 1;")).fetchall()
211216
# sqlcommenter
212217
self.assertRegex(
213218
self.caplog.records[-2].getMessage(),
@@ -233,7 +238,7 @@ def test_custom_tracer_provider(self):
233238

234239
engine = create_engine("sqlite:///:memory:")
235240
cnx = engine.connect()
236-
cnx.execute("SELECT 1 + 1;").fetchall()
241+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
237242
spans = self.memory_exporter.get_finished_spans()
238243

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

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

300305
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
301306
async with engine.connect() as cnx:
302-
await cnx.execute(sqlalchemy.text("SELECT 1;"))
307+
await cnx.execute(text("SELECT 1;"))
303308
# sqlcommenter
304309
self.assertRegex(
305310
self.caplog.records[1].getMessage(),
@@ -330,7 +335,7 @@ async def run():
330335

331336
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
332337
async with engine.connect() as cnx:
333-
await cnx.execute(sqlalchemy.text("SELECT 1;"))
338+
await cnx.execute(text("SELECT 1;"))
334339
# sqlcommenter
335340
self.assertRegex(
336341
self.caplog.records[1].getMessage(),
@@ -346,7 +351,7 @@ def test_uninstrument(self):
346351
tracer_provider=self.tracer_provider,
347352
)
348353
cnx = engine.connect()
349-
cnx.execute("SELECT 1 + 1;").fetchall()
354+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
350355
spans = self.memory_exporter.get_finished_spans()
351356

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

360365
self.memory_exporter.clear()
361366
SQLAlchemyInstrumentor().uninstrument()
362-
cnx.execute("SELECT 1 + 1;").fetchall()
367+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
363368
engine2 = create_engine("sqlite:///:memory:")
364369
cnx2 = engine2.connect()
365-
cnx2.execute("SELECT 2 + 2;").fetchall()
370+
cnx2.execute(text("SELECT 2 + 2;")).fetchall()
366371
spans = self.memory_exporter.get_finished_spans()
367372
self.assertEqual(len(spans), 0)
368373

@@ -371,7 +376,7 @@ def test_uninstrument(self):
371376
tracer_provider=self.tracer_provider,
372377
)
373378
cnx = engine.connect()
374-
cnx.execute("SELECT 1 + 1;").fetchall()
379+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
375380
spans = self.memory_exporter.get_finished_spans()
376381
self.assertEqual(len(spans), 2)
377382

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

386391
cnx = engine.connect()
387-
cnx.execute("SELECT 1 + 1;").fetchall()
392+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
388393
spans = self.memory_exporter.get_finished_spans()
389394
self.assertEqual(len(spans), 2)
390395

391396
self.memory_exporter.clear()
392397
SQLAlchemyInstrumentor().uninstrument()
393-
cnx.execute("SELECT 1 + 1;").fetchall()
398+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
394399
spans = self.memory_exporter.get_finished_spans()
395400
self.assertEqual(len(spans), 0)
396401

@@ -401,7 +406,7 @@ def test_no_op_tracer_provider(self):
401406
tracer_provider=trace.NoOpTracerProvider(),
402407
)
403408
cnx = engine.connect()
404-
cnx.execute("SELECT 1 + 1;").fetchall()
409+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
405410
spans = self.memory_exporter.get_finished_spans()
406411
self.assertEqual(len(spans), 0)
407412

@@ -420,7 +425,7 @@ def make_shortlived_engine():
420425
# collection
421426
weakref.finalize(engine, callback)
422427
with engine.connect() as conn:
423-
conn.execute("SELECT 1 + 1;").fetchall()
428+
conn.execute(text("SELECT 1 + 1;")).fetchall()
424429

425430
for _ in range(0, 5):
426431
make_shortlived_engine()

0 commit comments

Comments
 (0)