Add support to instrument multiple sqlalchemy engines (#1132)

* Override __new__ to instrument multiple engines

* add possibility to instrument mutiple engines

* update changelog

* update return type in doc

* fix CHANGELOG

* format

* fix test

* Check if engines isinstance Sequence

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
This commit is contained in:
hemma
2022-06-16 13:44:55 +02:00
committed by GitHub
parent 4ba62140ad
commit f03bef2579
3 changed files with 35 additions and 1 deletions

View File

@ -50,6 +50,24 @@ class TestSqlalchemyInstrumentation(TestBase):
self.assertEqual(spans[0].name, "SELECT :memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
def test_instrument_two_engines(self):
engine_1 = create_engine("sqlite:///:memory:")
engine_2 = create_engine("sqlite:///:memory:")
SQLAlchemyInstrumentor().instrument(
engines=[engine_1, engine_2],
tracer_provider=self.tracer_provider,
)
cnx_1 = engine_1.connect()
cnx_1.execute("SELECT 1 + 1;").fetchall()
cnx_2 = engine_2.connect()
cnx_2.execute("SELECT 1 + 1;").fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
@pytest.mark.skipif(
not sqlalchemy.__version__.startswith("1.4"),
reason="only run async tests for 1.4",