Skip to content

Commit 402155e

Browse files
tammy-baylis-swilzchen
authored andcommitted
Update tests to work with SQLAlchemy 2 (#2976)
1 parent d4c3900 commit 402155e

File tree

6 files changed

+167
-33
lines changed

6 files changed

+167
-33
lines changed

.github/workflows/test_1.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,24 @@ jobs:
11681168
- name: Run tests
11691169
run: tox -e py38-test-instrumentation-sqlalchemy-1 -- -ra
11701170

1171+
py38-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1172+
name: instrumentation-sqlalchemy-2 3.8 Ubuntu
1173+
runs-on: ubuntu-latest
1174+
steps:
1175+
- name: Checkout repo @ SHA - ${{ github.sha }}
1176+
uses: actions/checkout@v4
1177+
1178+
- name: Set up Python 3.8
1179+
uses: actions/setup-python@v5
1180+
with:
1181+
python-version: "3.8"
1182+
1183+
- name: Install tox
1184+
run: pip install tox
1185+
1186+
- name: Run tests
1187+
run: tox -e py38-test-instrumentation-sqlalchemy-2 -- -ra
1188+
11711189
py39-test-instrumentation-sqlalchemy-1_ubuntu-latest:
11721190
name: instrumentation-sqlalchemy-1 3.9 Ubuntu
11731191
runs-on: ubuntu-latest
@@ -1186,6 +1204,24 @@ jobs:
11861204
- name: Run tests
11871205
run: tox -e py39-test-instrumentation-sqlalchemy-1 -- -ra
11881206

1207+
py39-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1208+
name: instrumentation-sqlalchemy-2 3.9 Ubuntu
1209+
runs-on: ubuntu-latest
1210+
steps:
1211+
- name: Checkout repo @ SHA - ${{ github.sha }}
1212+
uses: actions/checkout@v4
1213+
1214+
- name: Set up Python 3.9
1215+
uses: actions/setup-python@v5
1216+
with:
1217+
python-version: "3.9"
1218+
1219+
- name: Install tox
1220+
run: pip install tox
1221+
1222+
- name: Run tests
1223+
run: tox -e py39-test-instrumentation-sqlalchemy-2 -- -ra
1224+
11891225
py310-test-instrumentation-sqlalchemy-1_ubuntu-latest:
11901226
name: instrumentation-sqlalchemy-1 3.10 Ubuntu
11911227
runs-on: ubuntu-latest
@@ -1204,6 +1240,24 @@ jobs:
12041240
- name: Run tests
12051241
run: tox -e py310-test-instrumentation-sqlalchemy-1 -- -ra
12061242

1243+
py310-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1244+
name: instrumentation-sqlalchemy-2 3.10 Ubuntu
1245+
runs-on: ubuntu-latest
1246+
steps:
1247+
- name: Checkout repo @ SHA - ${{ github.sha }}
1248+
uses: actions/checkout@v4
1249+
1250+
- name: Set up Python 3.10
1251+
uses: actions/setup-python@v5
1252+
with:
1253+
python-version: "3.10"
1254+
1255+
- name: Install tox
1256+
run: pip install tox
1257+
1258+
- name: Run tests
1259+
run: tox -e py310-test-instrumentation-sqlalchemy-2 -- -ra
1260+
12071261
py311-test-instrumentation-sqlalchemy-1_ubuntu-latest:
12081262
name: instrumentation-sqlalchemy-1 3.11 Ubuntu
12091263
runs-on: ubuntu-latest
@@ -1222,6 +1276,24 @@ jobs:
12221276
- name: Run tests
12231277
run: tox -e py311-test-instrumentation-sqlalchemy-1 -- -ra
12241278

1279+
py311-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1280+
name: instrumentation-sqlalchemy-2 3.11 Ubuntu
1281+
runs-on: ubuntu-latest
1282+
steps:
1283+
- name: Checkout repo @ SHA - ${{ github.sha }}
1284+
uses: actions/checkout@v4
1285+
1286+
- name: Set up Python 3.11
1287+
uses: actions/setup-python@v5
1288+
with:
1289+
python-version: "3.11"
1290+
1291+
- name: Install tox
1292+
run: pip install tox
1293+
1294+
- name: Run tests
1295+
run: tox -e py311-test-instrumentation-sqlalchemy-2 -- -ra
1296+
12251297
py312-test-instrumentation-sqlalchemy-1_ubuntu-latest:
12261298
name: instrumentation-sqlalchemy-1 3.12 Ubuntu
12271299
runs-on: ubuntu-latest
@@ -1240,6 +1312,24 @@ jobs:
12401312
- name: Run tests
12411313
run: tox -e py312-test-instrumentation-sqlalchemy-1 -- -ra
12421314

1315+
py312-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1316+
name: instrumentation-sqlalchemy-2 3.12 Ubuntu
1317+
runs-on: ubuntu-latest
1318+
steps:
1319+
- name: Checkout repo @ SHA - ${{ github.sha }}
1320+
uses: actions/checkout@v4
1321+
1322+
- name: Set up Python 3.12
1323+
uses: actions/setup-python@v5
1324+
with:
1325+
python-version: "3.12"
1326+
1327+
- name: Install tox
1328+
run: pip install tox
1329+
1330+
- name: Run tests
1331+
run: tox -e py312-test-instrumentation-sqlalchemy-2 -- -ra
1332+
12431333
pypy3-test-instrumentation-sqlalchemy-0_ubuntu-latest:
12441334
name: instrumentation-sqlalchemy-0 pypy-3.8 Ubuntu
12451335
runs-on: ubuntu-latest
@@ -1276,6 +1366,24 @@ jobs:
12761366
- name: Run tests
12771367
run: tox -e pypy3-test-instrumentation-sqlalchemy-1 -- -ra
12781368

1369+
pypy3-test-instrumentation-sqlalchemy-2_ubuntu-latest:
1370+
name: instrumentation-sqlalchemy-2 pypy-3.8 Ubuntu
1371+
runs-on: ubuntu-latest
1372+
steps:
1373+
- name: Checkout repo @ SHA - ${{ github.sha }}
1374+
uses: actions/checkout@v4
1375+
1376+
- name: Set up Python pypy-3.8
1377+
uses: actions/setup-python@v5
1378+
with:
1379+
python-version: "pypy-3.8"
1380+
1381+
- name: Install tox
1382+
run: pip install tox
1383+
1384+
- name: Run tests
1385+
run: tox -e pypy3-test-instrumentation-sqlalchemy-2 -- -ra
1386+
12791387
py38-test-instrumentation-redis_ubuntu-latest:
12801388
name: instrumentation-redis 3.8 Ubuntu
12811389
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
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
Lines changed: 13 additions & 0 deletions
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

instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py

Lines changed: 29 additions & 24 deletions
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)