From 49aae013280aa10da161cf1df5e5ea0801377a97 Mon Sep 17 00:00:00 2001 From: sroda Date: Wed, 30 Nov 2022 16:31:38 +0200 Subject: [PATCH 1/2] Add uninstrument test for sqlalchemy --- CHANGELOG.md | 2 ++ .../tests/test_sqlalchemy.py | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f336be0cba..442e1b13d4 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..20fd2b2119 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -227,3 +227,28 @@ 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) + + 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), 2) From d750273ac13ef09f06a9d40e350a251c89e74f89 Mon Sep 17 00:00:00 2001 From: sroda Date: Mon, 5 Dec 2022 10:50:24 +0200 Subject: [PATCH 2/2] Fix after CR --- .../tests/test_sqlalchemy.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 20fd2b2119..099c088f64 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -246,9 +246,10 @@ def test_uninstrument(self): 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), 2) + self.assertEqual(len(spans), 0)