Skip to content

Commit 41b308f

Browse files
committed
Final fixes for docs
1 parent 89082e5 commit 41b308f

File tree

7 files changed

+104
-123
lines changed

7 files changed

+104
-123
lines changed

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and integration packages.
1616
:caption: OpenTelemetry API:
1717

1818
opentelemetry.context
19+
opentelemetry.patcher
1920
opentelemetry.metrics
2021
opentelemetry.trace
2122
opentelemetry.util.loader

docs/opentelemetry.patcher.rst

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
opentelemetry.patcher package
2+
=============================
3+
4+
5+
Module contents
6+
---------------
7+
8+
.. automodule:: opentelemetry.patcher

ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import opentelemetry.ext.wsgi as otel_wsgi
99
from opentelemetry import propagators, trace
1010
from opentelemetry.ext.flask.version import __version__
11-
from opentelemetry.patcher.base_patcher import BasePatcher
11+
from opentelemetry.patcher import BasePatcher
1212
from opentelemetry.util import time_ns
1313

1414
logger = getLogger(__name__)
@@ -105,7 +105,10 @@ def _teardown_flask_request(exc):
105105

106106
@BasePatcher.protect
107107
class FlaskPatcher(BasePatcher):
108-
"""A patcher for flask.Flask"""
108+
"""A patcher for flask.Flask
109+
110+
See `BasePatcher`
111+
"""
109112

110113
def __init__(self):
111114
self._patched = False

opentelemetry-api/src/opentelemetry/patcher/__init__.py

+89
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,98 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# type: ignore
1415

1516
"""
1617
OpenTelemetry patcher
1718
1819
This includes the base patcher class and a no-op implementation.
1920
"""
21+
22+
from abc import ABC, abstractmethod
23+
from functools import wraps
24+
from logging import getLogger
25+
26+
_LOG = getLogger(__name__)
27+
28+
29+
class BasePatcher(ABC):
30+
"""An ABC for patchers"""
31+
32+
@staticmethod
33+
def protect(class_) -> None:
34+
"""
35+
Provides a class decorator that protects patch and unpatch methods
36+
37+
A protected patch method can't be called again until its corresponding
38+
unpatch method has been called and vice versa. A protected patch method
39+
must be called first before its corresponding unpatch method can be
40+
called.
41+
42+
To use this decorator simply decorate the patcher class:
43+
44+
.. code-block:: python
45+
46+
from opentelemetry.patcher.base_patcher import BasePatcher
47+
48+
@BasePatcher.protect
49+
class PatcherClass(BasePatcher):
50+
...
51+
"""
52+
53+
# pylint: disable=protected-access
54+
55+
class_._unprotected_patch = class_.patch
56+
class_._unprotected_patch._protected = False
57+
58+
@wraps(class_.patch)
59+
def protected_patch(self):
60+
if not self._unprotected_patch.__func__._protected:
61+
self._unprotected_patch.__func__._protected = True
62+
self._unprotected_unpatch.__func__._protected = False
63+
64+
return self._unprotected_patch()
65+
66+
_LOG.warning("Attempting to patch while already patched")
67+
68+
return None
69+
70+
class_.patch = protected_patch
71+
72+
class_._unprotected_unpatch = class_.unpatch
73+
class_._unprotected_unpatch._protected = True
74+
75+
@wraps(class_.unpatch)
76+
def protected_unpatch(self):
77+
if not self._unprotected_unpatch.__func__._protected:
78+
self._unprotected_unpatch.__func__._protected = True
79+
self._unprotected_patch.__func__._protected = False
80+
81+
return self._unprotected_unpatch()
82+
83+
_LOG.warning("Attempting to unpatch while already unpatched")
84+
85+
return None
86+
87+
class_.unpatch = protected_unpatch
88+
89+
return class_
90+
91+
@abstractmethod
92+
def patch(self) -> None:
93+
"""Patch"""
94+
95+
@abstractmethod
96+
def unpatch(self) -> None:
97+
"""Unpatch"""
98+
99+
100+
class NoOpPatcher(BasePatcher):
101+
def patch(self) -> None:
102+
"""Patch"""
103+
104+
def unpatch(self) -> None:
105+
"""Unpatch"""
106+
107+
108+
__all__ = ["BasePatcher", "NoOpPatcher"]

opentelemetry-api/src/opentelemetry/patcher/base_patcher.py

-94
This file was deleted.

opentelemetry-api/src/opentelemetry/patcher/no_op_patcher.py

-26
This file was deleted.

opentelemetry-api/tests/patcher/test_patcher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from unittest import TestCase
1717

18-
from opentelemetry.patcher.base_patcher import BasePatcher
18+
from opentelemetry.patcher import BasePatcher
1919

2020

2121
class TestSampler(TestCase):

0 commit comments

Comments
 (0)