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

Ensure RedisIntegration is disabled, unless redis is installed #2504

Merged
15 changes: 12 additions & 3 deletions sentry_sdk/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


_installer_lock = Lock()
_installed_integrations = set() # type: Set[str]
_install_attempted_integrations = set() # type: Set[str]


def _generate_default_integrations_iterator(
Expand Down Expand Up @@ -119,9 +119,10 @@
integrations[instance.identifier] = instance
used_as_default_integration.add(instance.identifier)

installed_integrations = set() # type: set[str]

Check warning on line 122 in sentry_sdk/integrations/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/__init__.py#L122

Added line #L122 was not covered by tests
for identifier, integration in iteritems(integrations):
with _installer_lock:
if identifier not in _installed_integrations:
if identifier not in _install_attempted_integrations:
logger.debug(
"Setting up previously not enabled integration %s", identifier
)
Expand All @@ -144,8 +145,16 @@
logger.debug(
"Did not enable default integration %s: %s", identifier, e
)
else:
installed_integrations.add(identifier)

Check warning on line 149 in sentry_sdk/integrations/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/__init__.py#L149

Added line #L149 was not covered by tests

_installed_integrations.add(identifier)
_install_attempted_integrations.add(identifier)

Check warning on line 151 in sentry_sdk/integrations/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/__init__.py#L151

Added line #L151 was not covered by tests

integrations = {
identifier: integration
for identifier, integration in iteritems(integrations)
if identifier in installed_integrations
}

for identifier in integrations:
logger.debug("Enabling integration %s", identifier)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import sentry_sdk
from sentry_sdk._compat import iteritems, reraise, string_types
from sentry_sdk.envelope import Envelope
from sentry_sdk.integrations import _installed_integrations # noqa: F401
from sentry_sdk.integrations import _install_attempted_integrations # noqa: F401
from sentry_sdk.profiler import teardown_profiler
from sentry_sdk.transport import Transport
from sentry_sdk.utils import capture_internal_exceptions
Expand Down Expand Up @@ -187,8 +187,8 @@ def reset_integrations():
with a clean slate to ensure monkeypatching works well,
but this also means some other stuff will be monkeypatched twice.
"""
global _installed_integrations
_installed_integrations.clear()
global _install_attempted_integrations
_install_attempted_integrations.clear()


@pytest.fixture
Expand Down
24 changes: 22 additions & 2 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sentry_sdk._compat import reraise
from sentry_sdk.integrations import _AUTO_ENABLING_INTEGRATIONS
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.scope import ( # noqa: F401
add_global_event_processor,
global_event_processors,
Expand All @@ -28,6 +29,18 @@
from sentry_sdk.tracing_utils import has_tracing_enabled


def _redis_installed(): # type: () -> bool
"""
Determines whether Redis is installed.
"""
try:
import redis # noqa: F401
except ImportError:
return False

return True


def test_processors(sentry_init, capture_events):
sentry_init()
events = capture_events()
Expand Down Expand Up @@ -59,8 +72,8 @@ def test_auto_enabling_integrations_catches_import_error(sentry_init, caplog):
sentry_init(auto_enabling_integrations=True, debug=True)

for import_string in _AUTO_ENABLING_INTEGRATIONS:
# Ignore redis in the test case, because it is installed as a
# dependency for running tests, and therefore always enabled.
# Ignore redis in the test case, because it does not raise a DidNotEnable
# exception on import; rather, it raises the exception upon enabling.
if _AUTO_ENABLING_INTEGRATIONS[redis_index] == import_string:
continue

Expand Down Expand Up @@ -686,3 +699,10 @@ def test_functions_to_trace_with_class(sentry_init, capture_events):
assert len(event["spans"]) == 2
assert event["spans"][0]["description"] == "tests.test_basics.WorldGreeter.greet"
assert event["spans"][1]["description"] == "tests.test_basics.WorldGreeter.greet"


@pytest.mark.skipif(_redis_installed(), reason="skipping because redis is installed")
def test_redis_disabled_when_not_installed(sentry_init):
sentry_init()

assert Hub.current.get_integration(RedisIntegration) is None
Loading