Skip to content

Commit 65538cf

Browse files
ocelotlAlex Boten
authored and
Alex Boten
committed
auto-instr: Add support for programmatic instrumentation (open-telemetry#579)
Fixes open-telemetry#554 This makes it possible to call the instrument method with arguments that make programmatic instrumentation possible. This also makes the children of BaseInstrumentors to be singletons. In this way regardless of how many times the programmatic instrumentation or uninstrumentation methods are called they will only be executed once.
1 parent ca11d27 commit 65538cf

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

Diff for: ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def __init__(self):
160160
super().__init__()
161161
self._original_flask = None
162162

163-
def _instrument(self):
163+
def _instrument(self, **kwargs):
164164
self._original_flask = flask.Flask
165165
flask.Flask = _InstrumentedFlask
166166

167-
def _uninstrument(self):
167+
def _uninstrument(self, **kwargs):
168168
flask.Flask = self._original_flask

Diff for: opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/instrumentor.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,41 @@
2626
class BaseInstrumentor(ABC):
2727
"""An ABC for instrumentors"""
2828

29-
def __init__(self):
30-
self._is_instrumented = False
29+
_instance = None
30+
_is_instrumented = False
31+
32+
def __new__(cls):
33+
34+
if cls._instance is None:
35+
cls._instance = object.__new__(cls)
36+
37+
return cls._instance
3138

3239
@abstractmethod
33-
def _instrument(self) -> None:
40+
def _instrument(self, **kwargs):
3441
"""Instrument"""
3542

3643
@abstractmethod
37-
def _uninstrument(self) -> None:
44+
def _uninstrument(self, **kwargs):
3845
"""Uninstrument"""
3946

40-
def instrument(self) -> None:
47+
def instrument(self, **kwargs):
4148
"""Instrument"""
4249

4350
if not self._is_instrumented:
44-
result = self._instrument()
51+
result = self._instrument(**kwargs)
4552
self._is_instrumented = True
4653
return result
4754

4855
_LOG.warning("Attempting to instrument while already instrumented")
4956

5057
return None
5158

52-
def uninstrument(self) -> None:
59+
def uninstrument(self, **kwargs):
5360
"""Uninstrument"""
5461

5562
if self._is_instrumented:
56-
result = self._uninstrument()
63+
result = self._uninstrument(**kwargs)
5764
self._is_instrumented = False
5865
return result
5966

Diff for: opentelemetry-auto-instrumentation/tests/test_instrumentor.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@
2020

2121

2222
class TestInstrumentor(TestCase):
23-
def test_protect(self):
24-
class Instrumentor(BaseInstrumentor):
25-
def _instrument(self):
26-
return "instrumented"
23+
class Instrumentor(BaseInstrumentor):
24+
def _instrument(self, **kwargs):
25+
return "instrumented"
2726

28-
def _uninstrument(self):
29-
return "uninstrumented"
27+
def _uninstrument(self, **kwargs):
28+
return "uninstrumented"
3029

31-
instrumentor = Instrumentor()
30+
def test_protect(self):
31+
instrumentor = self.Instrumentor()
3232

3333
with self.assertLogs(level=WARNING):
3434
self.assertIs(instrumentor.uninstrument(), None)
3535

3636
self.assertEqual(instrumentor.instrument(), "instrumented")
37-
3837
with self.assertLogs(level=WARNING):
3938
self.assertIs(instrumentor.instrument(), None)
4039

4140
self.assertEqual(instrumentor.uninstrument(), "uninstrumented")
4241

4342
with self.assertLogs(level=WARNING):
4443
self.assertIs(instrumentor.uninstrument(), None)
44+
45+
def test_singleton(self):
46+
self.assertIs(self.Instrumentor(), self.Instrumentor())

0 commit comments

Comments
 (0)