Skip to content

Commit 4023475

Browse files
moredipemdnetolzchenshalevrtammy-baylis-swi
committed
Fix instrumentation of SQLAlchemy when using sqlalchemy.engine_from_config (open-telemetry#2816)
* 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. * add changelog entry * Update CHANGELOG.md Co-authored-by: Emídio Neto <[email protected]> * Update instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py Co-authored-by: Tammy Baylis <[email protected]> * make some monkey-patching conditional on the version of SQLAlchemy * lint * fix changelog Signed-off-by: emdneto <[email protected]> * Update CHANGELOG.md --------- Signed-off-by: emdneto <[email protected]> Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Shalev Roda <[email protected]> Co-authored-by: Tammy Baylis <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent ea33f24 commit 4023475

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4646
([#3022](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3022))
4747
- Replace all instrumentor unit test `assertEqualSpanInstrumentationInfo` calls with `assertEqualSpanInstrumentationScope` calls
4848
([#3037](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3037))
49+
- `opentelemetry-instrumentation-sqlalchemy` Fixes engines from `sqlalchemy.engine_from_config` not being fully instrumented
50+
([#2816](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2816))
4951
- `opentelemetry-instrumentation-sqlalchemy`: Fix a remaining memory leak in EngineTracer
5052
([#3053](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3053))
5153

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

+14
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ def _instrument(self, **kwargs):
181181
tracer, connections_usage, enable_commenter, commenter_options
182182
),
183183
)
184+
# sqlalchemy.engine.create is not present in earlier versions of sqlalchemy (which we support)
185+
if parse_version(sqlalchemy.__version__).release >= (1, 4):
186+
_w(
187+
"sqlalchemy.engine.create",
188+
"create_engine",
189+
_wrap_create_engine(
190+
tracer,
191+
connections_usage,
192+
enable_commenter,
193+
commenter_options,
194+
),
195+
)
184196
_w(
185197
"sqlalchemy.engine.base",
186198
"Engine.connect",
@@ -224,6 +236,8 @@ def _instrument(self, **kwargs):
224236
def _uninstrument(self, **kwargs):
225237
unwrap(sqlalchemy, "create_engine")
226238
unwrap(sqlalchemy.engine, "create_engine")
239+
if parse_version(sqlalchemy.__version__).release >= (1, 4):
240+
unwrap(sqlalchemy.engine.create, "create_engine")
227241
unwrap(Engine, "connect")
228242
if parse_version(sqlalchemy.__version__).release >= (1, 4):
229243
unwrap(sqlalchemy.ext.asyncio, "create_async_engine")

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

+11
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ def test_create_engine_wrapper(self):
182182
"opentelemetry.instrumentation.sqlalchemy",
183183
)
184184

185+
def test_instrument_engine_from_config(self):
186+
SQLAlchemyInstrumentor().instrument()
187+
from sqlalchemy import engine_from_config # pylint: disable-all
188+
189+
engine = engine_from_config({"sqlalchemy.url": "sqlite:///:memory:"})
190+
cnx = engine.connect()
191+
cnx.execute(text("SELECT 1 + 1;")).fetchall()
192+
spans = self.memory_exporter.get_finished_spans()
193+
194+
self.assertEqual(len(spans), 2)
195+
185196
def test_create_engine_wrapper_enable_commenter(self):
186197
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
187198
SQLAlchemyInstrumentor().instrument(

0 commit comments

Comments
 (0)