From 99e0b426350ab74cadb5fcb4c9504dee12e73773 Mon Sep 17 00:00:00 2001 From: Shalev Roda <65566801+shalevr@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:58:10 +0200 Subject: [PATCH] Add uninstrument test for sqlalchemy (#1471) --- CHANGELOG.md | 2 ++ .../tests/test_sqlalchemy.py | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 360b821440..7746f5fe67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Add uninstrument test for sqlalchemy + ([#1471](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1471)) - `opentelemetry-instrumentation-tortoiseorm` Initial release ([#685](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/685)) - Add metric instrumentation for tornado diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 913b7d3c5e..099c088f64 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -227,3 +227,29 @@ async def run(): ) asyncio.get_event_loop().run_until_complete(run()) + + def test_uninstrument(self): + engine = create_engine("sqlite:///:memory:") + SQLAlchemyInstrumentor().instrument( + engine=engine, + tracer_provider=self.tracer_provider, + ) + cnx = engine.connect() + cnx.execute("SELECT 1 + 1;").fetchall() + spans = self.memory_exporter.get_finished_spans() + + self.assertEqual(len(spans), 2) + # first span - the connection to the db + self.assertEqual(spans[0].name, "connect") + self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT) + # second span - the query itself + self.assertEqual(spans[1].name, "SELECT :memory:") + self.assertEqual(spans[1].kind, trace.SpanKind.CLIENT) + + self.memory_exporter.clear() + SQLAlchemyInstrumentor().uninstrument() + engine2 = create_engine("sqlite:///:memory:") + cnx2 = engine2.connect() + cnx2.execute("SELECT 2 + 2;").fetchall() + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0)