diff --git a/CHANGELOG.md b/CHANGELOG.md index 4198628292..0a1039f83a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3022](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3022)) - Replace all instrumentor unit test `assertEqualSpanInstrumentationInfo` calls with `assertEqualSpanInstrumentationScope` calls ([#3037](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3037)) +- `opentelemetry-instrumentation-sqlalchemy`: Fix a remaining memory leak in EngineTracer + ([#3053](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3053)) ### Breaking changes diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index b64af796d1..a20e481819 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -167,6 +167,13 @@ def _pool_checkout( self._add_idle_to_connection_usage(-1) self._add_used_to_connection_usage(1) + @classmethod + def _dispose_of_event_listener(cls, obj): + try: + cls._remove_event_listener_params.remove(obj) + except ValueError: + pass + @classmethod def _register_event_listener(cls, target, identifier, func, *args, **kw): listen(target, identifier, func, *args, **kw) @@ -174,6 +181,12 @@ def _register_event_listener(cls, target, identifier, func, *args, **kw): (weakref.ref(target), identifier, func) ) + weakref.finalize( + target, + cls._dispose_of_event_listener, + (weakref.ref(target), identifier, func), + ) + @classmethod def remove_all_event_listeners(cls): for ( diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 957ae16237..18b9fa65f7 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -417,6 +417,10 @@ def test_no_memory_leakage_if_engine_diposed(self): from sqlalchemy import create_engine + from opentelemetry.instrumentation.sqlalchemy.engine import ( + EngineTracer, + ) + callback = mock.Mock() def make_shortlived_engine(): @@ -432,3 +436,4 @@ def make_shortlived_engine(): gc.collect() assert callback.call_count == 5 + assert len(EngineTracer._remove_event_listener_params) == 0