From 2751b82cbbc0675894fc7eff7752e011439191f0 Mon Sep 17 00:00:00 2001 From: "jeanloup.monnier" Date: Mon, 27 Feb 2023 12:09:34 +0100 Subject: [PATCH] fix: sentry client is None --- .pre-commit-config.yaml | 2 +- sentry_dynamic_sampling_lib/__init__.py | 3 +++ tests/test_hooks.py | 14 +++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index efcee3b..ea0bb29 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: - pyflakes==2.4.0 - repo: https://github.com/pycqa/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort args: ["-m=VERTICAL_HANGING_INDENT", "--combine-as", "--profile=black"] diff --git a/sentry_dynamic_sampling_lib/__init__.py b/sentry_dynamic_sampling_lib/__init__.py index 0f904f4..c5be3de 100644 --- a/sentry_dynamic_sampling_lib/__init__.py +++ b/sentry_dynamic_sampling_lib/__init__.py @@ -42,6 +42,9 @@ def init_wrapper(): sentry_sdk: sentry_sdk_type = importlib.import_module("sentry_sdk") client = sentry_sdk.Hub.current.client + if client is None: + return + if CONTROLLER_HOST: app_key = build_app_key(client.options) controller_endpoint = urljoin(CONTROLLER_HOST, CONTROLLER_PATH) diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 1257eaa..eea4361 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch import pytest @@ -26,6 +26,18 @@ def test_init_wrapper_no_sentry(importlib_mock: Mock): importlib_mock.import_module.assert_not_called() +@patch("sentry_dynamic_sampling_lib.importlib") +def test_init_wrapper_no_client(importlib_mock: Mock): + importlib_mock.util.find_spec.return_value = True + sentry_sdk = MagicMock() + sentry_sdk.Hub.current.client = None + importlib_mock.import_module.return_value = sentry_sdk + + init_wrapper() + importlib_mock.util.find_spec.assert_called_once_with("sentry_sdk") + importlib_mock.import_module.assert_called_once_with("sentry_sdk") + + @patch("sentry_dynamic_sampling_lib.TraceSampler") @patch("sentry_dynamic_sampling_lib.importlib") def test_init_wrapper_no_controller(importlib_mock: Mock, trace_sampler: Mock):