Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Monkey Patch for Gevent #218

Merged
merged 9 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"):
harrryr marked this conversation as resolved.
Show resolved Hide resolved
# pylint: disable=import-outside-toplevel
# Delay import to only occur if monkey patch is needed (e.g. gevent is used to run application).
harrryr marked this conversation as resolved.
Show resolved Hide resolved
from gevent import monkey

monkey.patch_ssl()
harrryr marked this conversation as resolved.
Show resolved Hide resolved

if _is_installed("botocore ~= 1.0"):
# pylint: disable=import-outside-toplevel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import sys
from typing import Dict
from unittest import TestCase
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -57,12 +58,14 @@ def _run_patch_behaviour_tests(self):

# Validate unpatched upstream behaviour - important to detect upstream changes that may break instrumentation
self._test_unpatched_botocore_instrumentation()
self._test_unpatched_gevent_ssl_instrumentation()

# Apply patches
apply_instrumentation_patches()

# Validate patched upstream behaviour - important to detect downstream changes that may break instrumentation
self._test_patched_botocore_instrumentation()
self._test_patched_gevent_ssl_instrumentation()

# Test teardown
self._reset_mocks()
Expand Down Expand Up @@ -93,6 +96,10 @@ def _test_unpatched_botocore_instrumentation(self):
self.assertFalse("aws.sqs.queue_url" in attributes)
self.assertFalse("aws.sqs.queue_name" in attributes)

def _test_unpatched_gevent_ssl_instrumentation(self):
# Ssl
self.assertFalse("gevent.ssl" in sys.modules, "Upstream has added the gevent ssl patch")
harrryr marked this conversation as resolved.
Show resolved Hide resolved

def _test_patched_botocore_instrumentation(self):
# Kinesis
self.assertTrue("kinesis" in _KNOWN_EXTENSIONS)
Expand All @@ -115,6 +122,10 @@ def _test_patched_botocore_instrumentation(self):
self.assertTrue("aws.sqs.queue_name" in sqs_attributes)
self.assertEqual(sqs_attributes["aws.sqs.queue_name"], _QUEUE_NAME)

def _test_patched_gevent_ssl_instrumentation(self):
# Ssl
self.assertTrue("gevent.ssl" in sys.modules)
harrryr marked this conversation as resolved.
Show resolved Hide resolved

def _test_botocore_installed_flag(self):
with patch(
"amazon.opentelemetry.distro.patches._botocore_patches._apply_botocore_instrumentation_patches"
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
Loading