Skip to content

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Aug 19, 2025

Description

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 https://github.com/gevent/gevent/issues/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()

With the change:

$ OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH=patch_all opentelemetry-instrument locust
[2025-08-19 11:16:40,973] P1-Gen-6/INFO/locust.main: Starting Locust 2.38.1
[2025-08-19 11:16:40,973] P1-Gen-6/INFO/locust.main: Starting web interface at http://0.0.0.0:8089, press enter to open your default browser.

While we only support patch_all, it's trivial to add support for more gevent patch methods (https://www.gevent.org/api/gevent.monkey.html#use-as-a-module).

The name contains EXPERIMENTAL because this is also patching code used by our exporters and while it appears to work it's not something we should encourage.

Fixes #3629

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • tox -e py310-test-opentelemetry-instrumentation

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@xrmx xrmx requested a review from a team as a code owner August 19, 2025 09:25
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Aug 19, 2025
@xrmx xrmx force-pushed the gevent-auto-instrumentation branch from b66f315 to be971fb Compare August 29, 2025 07:49
@xrmx xrmx enabled auto-merge (squash) August 29, 2025 16:35
@xrmx xrmx moved this from Ready for review to Approved PRs in @xrmx's Python PR digest Aug 29, 2025
@xrmx xrmx disabled auto-merge August 29, 2025 18:37
@xrmx xrmx enabled auto-merge (squash) August 29, 2025 18:37
xrmx and others added 5 commits August 29, 2025 20:48
…event

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()
Co-authored-by: Tammy Baylis <96076570+tammy-baylis-swi@users.noreply.github.com>
@xrmx xrmx force-pushed the gevent-auto-instrumentation branch from 7dc08bf to 90c9af7 Compare August 29, 2025 18:48
@xrmx xrmx merged commit 5f0d4ff into open-telemetry:main Aug 29, 2025
632 checks passed
@github-project-automation github-project-automation bot moved this from Approved PRs to Done in @xrmx's Python PR digest Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

python instrumentation Encountering an error
4 participants