Skip to content

Commit

Permalink
Add monkey.patch to instrumentation_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
harrryr committed Jun 24, 2024
1 parent 6bffb7a commit e6ca6ac
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0
import os
import sys
from pathlib import Path
from logging import Logger, getLogger

from amazon.opentelemetry.distro.patches._instrumentation_patch import apply_instrumentation_patches
Expand Down Expand Up @@ -56,15 +55,6 @@ def _configure(self, **kwargs):
if cwd_path not in sys.path:
sys.path.insert(0, cwd_path)

# If application is using gevent, the application may crash due to SSL recursive issues. Check if application has the module 'gevent', and if so, patch SSL before gevent is started.
for path in Path('/').rglob('*'):
try:
if path.is_dir() and path.name == 'gevent':
from gevent import monkey; monkey.patch_ssl()
break
except Exception:
continue

os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")

super(AwsOpenTelemetryDistro, self)._configure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def apply_instrumentation_patches() -> None:
Where possible, automated testing should be run to catch upstream changes resulting in broken patches
"""
if _is_installed("gevent"):
# pylint: disable=import-outside-toplevel
# Delay import to only occur if monkey patch is needed (e.g. gevent is used to run application).
from gevent import monkey

monkey.patch_ssl()

if _is_installed("botocore ~= 1.0"):
# pylint: disable=import-outside-toplevel
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ codespell==2.1.0
requests==2.32.0
ruamel.yaml==0.17.21
flaky==3.7.0
botocore==1.34.67
botocore==1.34.67
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ commands_pre =
; Install common packages for all the tests. These are not needed in all the
; cases but it saves a lot of boilerplate in this file.
test: pip install botocore
test: pip install gevent
test: pip install "opentelemetry-api[test] @ {env:CORE_REPO}#egg=opentelemetry-api&subdirectory=opentelemetry-api"
test: pip install "opentelemetry-sdk[test] @ {env:CORE_REPO}#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk"
test: pip install "opentelemetry-instrumentation[test] @ {env:CONTRIB_REPO}#egg=opentelemetry-instrumentation&subdirectory=opentelemetry-instrumentation"
Expand Down

0 comments on commit e6ca6ac

Please sign in to comment.