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

Update tests to work with SQLAlchemy 2 #2976

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import logging

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

from opentelemetry import context
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
Expand All @@ -37,7 +40,7 @@ def test_sqlcommenter_disabled(self):
engine=engine, tracer_provider=self.tracer_provider
)
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()

self.assertEqual(self.caplog.records[-2].getMessage(), "SELECT 1;")

Expand All @@ -50,7 +53,7 @@ def test_sqlcommenter_enabled(self):
commenter_options={"db_framework": False},
)
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
self.assertRegex(
self.caplog.records[-2].getMessage(),
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
Expand All @@ -68,7 +71,7 @@ def test_sqlcommenter_enabled_otel_values_false(self):
},
)
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
self.assertRegex(
self.caplog.records[-2].getMessage(),
r"SELECT 1 /\*db_driver='(.*)'\*/;",
Expand All @@ -90,7 +93,7 @@ def test_sqlcommenter_flask_integration(self):
)
context.attach(sqlcommenter_context)

cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
self.assertRegex(
self.caplog.records[-2].getMessage(),
r"SELECT 1 /\*db_driver='(.*)',flask=1,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
Expand All @@ -105,7 +108,7 @@ def test_sqlcommenter_enabled_create_engine_after_instrumentation(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
self.assertRegex(
self.caplog.records[-2].getMessage(),
r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
Expand All @@ -120,5 +123,5 @@ def test_sqlcommenter_disabled_create_engine_after_instrumentation(self):

engine = create_engine("sqlite:///:memory:")
cnx = engine.connect()
cnx.execute("SELECT 1;").fetchall()
cnx.execute(text("SELECT 1;")).fetchall()
self.assertEqual(self.caplog.records[-2].getMessage(), "SELECT 1;")