Skip to content

Commit 5f0d4ff

Browse files
opentelemetry-instrumentation: teach opentelemetry-instrument about gevent (#3699)
* opentelemetry-instrumentation: teach opentelemetry-instrument about gevent Introduce OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH=patch_all environment variable that calls gevent monkey module patch_all method before starting up the distro and sdk. The environment variable should useful also for apps instrumented via the opentelemetry-operator. The flag removes the following warning (and hang) when running locust: $ opentelemetry-instrument locust /lib/python3.10/site-packages/locust/__init__.py:16: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See gevent/gevent#1016. Modules that had direct imports (NOT patched): ['urllib3.util (/lib/python3.10/site-packages/urllib3/util/__init__.py)', 'urllib3.util.ssl_ (/lib/python3.10/site-packages/urllib3/util/ssl_.py)']. monkey.patch_all() * Update CHANGELOG * Please pylint * Apply suggestions from code review Co-authored-by: Tammy Baylis <[email protected]> * Move environment variable to proper module --------- Co-authored-by: Tammy Baylis <[email protected]>
1 parent 86d26ce commit 5f0d4ff

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
([#3666](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3666))
3535
- `opentelemetry-sdk-extension-aws` Add AWS X-Ray Remote Sampler with initial Rules Poller implementation
3636
([#3366](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3366))
37+
- `opentelemetry-instrumentation`: add support for `OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH` to inform opentelemetry-instrument about gevent monkeypatching
38+
([#3699](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3699))
3739

3840
## Version 1.36.0/0.57b0 (2025-07-29)
3941

opentelemetry-instrumentation/README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ check `here <https://opentelemetry-python.readthedocs.io/en/stable/index.html#in
104104
* ``OTEL_PYTHON_DISABLED_INSTRUMENTATIONS``
105105

106106
If set by the user, opentelemetry-instrument will read this environment variable to disable specific instrumentations.
107-
e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "requests,django"
107+
e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS="requests,django"
108+
109+
* ``OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH``
110+
111+
If set by the user to `patch_all` , opentelemetry instrument will call the gevent monkeypatching method ``patch_all``.
112+
This is considered experimental but can be useful to instrument gevent applications.
113+
e.g OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH=patch_all
108114

109115

110116
Examples

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
from argparse import REMAINDER, ArgumentParser
1618
from logging import getLogger
1719
from os import environ, execl, getcwd
@@ -24,6 +26,9 @@
2426
_load_distro,
2527
_load_instrumentors,
2628
)
29+
from opentelemetry.instrumentation.environment_variables import (
30+
OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH,
31+
)
2732
from opentelemetry.instrumentation.utils import _python_path_without_directory
2833
from opentelemetry.instrumentation.version import __version__
2934
from opentelemetry.util._importlib_metadata import entry_points
@@ -130,6 +135,30 @@ def initialize(*, swallow_exceptions: bool = True) -> None:
130135
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
131136
)
132137

138+
# handle optional gevent monkey patching. This is done via environment variables so it may be used from the
139+
# opentelemetry operator
140+
gevent_patch: str | None = environ.get(
141+
OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH
142+
)
143+
if gevent_patch is not None:
144+
if gevent_patch != "patch_all":
145+
_logger.error(
146+
"%s value must be `patch_all`",
147+
OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH,
148+
)
149+
else:
150+
try:
151+
# pylint: disable=import-outside-toplevel
152+
from gevent import monkey
153+
154+
getattr(monkey, gevent_patch)()
155+
except ImportError:
156+
_logger.exception(
157+
"Failed to monkey patch with gevent because gevent is not available"
158+
)
159+
if not swallow_exceptions:
160+
raise
161+
133162
try:
134163
distro = _load_distro()
135164
distro.configure()

opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@
2626
"""
2727
.. envvar:: OTEL_PYTHON_CONFIGURATOR
2828
"""
29+
30+
OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH = (
31+
"OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH"
32+
)
33+
"""
34+
.. envvar:: OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH
35+
"""

opentelemetry-instrumentation/test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
asgiref==3.8.1
22
Deprecated==1.2.14
3+
gevent==25.5.1
34
iniconfig==2.0.0
45
packaging==24.0
56
pluggy==1.5.0

opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,30 @@ def test_reraises_exceptions(self, load_distro_mock, logger_mock):
7272
)
7373

7474
self.assertEqual("inner exception", str(em.exception))
75+
76+
@patch.dict(
77+
"os.environ",
78+
{
79+
"OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH": "patch_foo"
80+
},
81+
)
82+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
83+
def test_handles_invalid_gevent_monkeypatch(self, logger_mock):
84+
# pylint:disable=no-self-use
85+
auto_instrumentation.initialize()
86+
logger_mock.error.assert_called_once_with(
87+
"%s value must be `patch_all`",
88+
"OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH",
89+
)
90+
91+
@patch.dict(
92+
"os.environ",
93+
{
94+
"OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH": "patch_all"
95+
},
96+
)
97+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
98+
def test_handles_patch_all_gevent_monkeypatch(self, logger_mock):
99+
# pylint:disable=no-self-use
100+
auto_instrumentation.initialize()
101+
logger_mock.error.assert_not_called()

0 commit comments

Comments
 (0)