Skip to content

Commit 9f40f5d

Browse files
committed
wrap sqlalchemy.engine.create.create_engine
sqlalchemy.engine_from_config directly calls create_engine imported from that path; if we don't wrap that copy of the `create_engine` call then engines created via engine_from_config are not instrumented.
1 parent 1ad9d90 commit 9f40f5d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ def _instrument(self, **kwargs):
180180
tracer, connections_usage, enable_commenter, commenter_options
181181
),
182182
)
183+
_w(
184+
"sqlalchemy.engine.create",
185+
"create_engine",
186+
_wrap_create_engine(
187+
tracer, connections_usage, enable_commenter, commenter_options
188+
),
189+
)
183190
_w(
184191
"sqlalchemy.engine.base",
185192
"Engine.connect",
@@ -223,6 +230,7 @@ def _instrument(self, **kwargs):
223230
def _uninstrument(self, **kwargs):
224231
unwrap(sqlalchemy, "create_engine")
225232
unwrap(sqlalchemy.engine, "create_engine")
233+
unwrap(sqlalchemy.engine.create, "create_engine")
226234
unwrap(Engine, "connect")
227235
if parse_version(sqlalchemy.__version__).release >= (1, 4):
228236
unwrap(sqlalchemy.ext.asyncio, "create_async_engine")

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

+11
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ def test_create_engine_wrapper(self):
177177
"opentelemetry.instrumentation.sqlalchemy",
178178
)
179179

180+
def test_instrument_engine_from_config(self):
181+
SQLAlchemyInstrumentor().instrument()
182+
from sqlalchemy import engine_from_config # pylint: disable-all
183+
184+
engine = engine_from_config({"sqlalchemy.url": "sqlite:///:memory:"})
185+
cnx = engine.connect()
186+
cnx.execute("SELECT 1 + 1;").fetchall()
187+
spans = self.memory_exporter.get_finished_spans()
188+
189+
self.assertEqual(len(spans), 2)
190+
180191
def test_create_engine_wrapper_enable_commenter(self):
181192
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
182193
SQLAlchemyInstrumentor().instrument(

0 commit comments

Comments
 (0)