diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index 9b21696c59b..eb008eeadc4 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -160,9 +160,9 @@ def __init__(self): super().__init__() self._original_flask = None - def _instrument(self): + def _instrument(self, **kwargs): self._original_flask = flask.Flask flask.Flask = _InstrumentedFlask - def _uninstrument(self): + def _uninstrument(self, **kwargs): flask.Flask = self._original_flask diff --git a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/instrumentor.py b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/instrumentor.py index 9deb6b15238..f5d7cf7ddc2 100644 --- a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/instrumentor.py +++ b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/instrumentor.py @@ -26,22 +26,29 @@ class BaseInstrumentor(ABC): """An ABC for instrumentors""" - def __init__(self): - self._is_instrumented = False + _instance = None + _is_instrumented = False + + def __new__(cls): + + if cls._instance is None: + cls._instance = object.__new__(cls) + + return cls._instance @abstractmethod - def _instrument(self) -> None: + def _instrument(self, **kwargs): """Instrument""" @abstractmethod - def _uninstrument(self) -> None: + def _uninstrument(self, **kwargs): """Uninstrument""" - def instrument(self) -> None: + def instrument(self, **kwargs): """Instrument""" if not self._is_instrumented: - result = self._instrument() + result = self._instrument(**kwargs) self._is_instrumented = True return result @@ -49,11 +56,11 @@ def instrument(self) -> None: return None - def uninstrument(self) -> None: + def uninstrument(self, **kwargs): """Uninstrument""" if self._is_instrumented: - result = self._uninstrument() + result = self._uninstrument(**kwargs) self._is_instrumented = False return result diff --git a/opentelemetry-auto-instrumentation/tests/test_instrumentor.py b/opentelemetry-auto-instrumentation/tests/test_instrumentor.py index 1324213536c..40e762230af 100644 --- a/opentelemetry-auto-instrumentation/tests/test_instrumentor.py +++ b/opentelemetry-auto-instrumentation/tests/test_instrumentor.py @@ -20,21 +20,20 @@ class TestInstrumentor(TestCase): - def test_protect(self): - class Instrumentor(BaseInstrumentor): - def _instrument(self): - return "instrumented" + class Instrumentor(BaseInstrumentor): + def _instrument(self, **kwargs): + return "instrumented" - def _uninstrument(self): - return "uninstrumented" + def _uninstrument(self, **kwargs): + return "uninstrumented" - instrumentor = Instrumentor() + def test_protect(self): + instrumentor = self.Instrumentor() with self.assertLogs(level=WARNING): self.assertIs(instrumentor.uninstrument(), None) self.assertEqual(instrumentor.instrument(), "instrumented") - with self.assertLogs(level=WARNING): self.assertIs(instrumentor.instrument(), None) @@ -42,3 +41,6 @@ def _uninstrument(self): with self.assertLogs(level=WARNING): self.assertIs(instrumentor.uninstrument(), None) + + def test_singleton(self): + self.assertIs(self.Instrumentor(), self.Instrumentor())