Skip to content

Commit 2fb3dc5

Browse files
committed
Centralize version checking
1 parent 0c44202 commit 2fb3dc5

File tree

20 files changed

+53
-139
lines changed

20 files changed

+53
-139
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def iter_default_integrations(with_auto_enabling_integrations):
111111
"sentry_sdk.integrations.tornado.TornadoIntegration",
112112
]
113113

114-
115114
iter_default_integrations = _generate_default_integrations_iterator(
116115
integrations=_DEFAULT_INTEGRATIONS,
117116
auto_enabling_integrations=_AUTO_ENABLING_INTEGRATIONS,
@@ -218,6 +217,21 @@ def setup_integrations(
218217
return integrations
219218

220219

220+
def _check_minimum_version(integration, version, package=None):
221+
# type: (Integration, Optional[tuple[int]], Optional[str]) -> None
222+
package = package or integration.identifier
223+
224+
if version is None:
225+
raise DidNotEnable(f"Unparsable {package} version: {version}")
226+
227+
min_version = _MIN_VERSIONS.get(integration.identifier)
228+
if min_version is None:
229+
return
230+
231+
if version < min_version:
232+
raise DidNotEnable(f"Integration only supports {package} {'.'.join(map(str, min_version))} or newer.")
233+
234+
221235
class DidNotEnable(Exception): # noqa: N818
222236
"""
223237
The integration could not be enabled due to a trivial user error like

sentry_sdk/integrations/aiohttp.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA
88
from sentry_sdk.integrations import (
99
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
10-
_MIN_VERSIONS,
10+
_check_minimum_version,
1111
Integration,
1212
DidNotEnable,
1313
)
@@ -92,13 +92,7 @@ def setup_once():
9292
# type: () -> None
9393

9494
version = parse_version(AIOHTTP_VERSION)
95-
96-
if version is None:
97-
raise DidNotEnable("Unparsable AIOHTTP version: {}".format(AIOHTTP_VERSION))
98-
99-
min_version = _MIN_VERSIONS[AioHttpIntegration.identifier]
100-
if version < min_version:
101-
raise DidNotEnable(f"AIOHTTP {'.'.join(map(str, min_version))} or newer required.")
95+
_check_minimum_version(AioHttpIntegration, version)
10296

10397
if not HAS_REAL_CONTEXTVARS:
10498
# We better have contextvars or we're going to leak state between

sentry_sdk/integrations/ariadne.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sentry_sdk
44
from sentry_sdk import get_client, capture_event
5-
from sentry_sdk.integrations import _MIN_VERSIONS, DidNotEnable, Integration
5+
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
66
from sentry_sdk.integrations.logging import ignore_logger
77
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
88
from sentry_sdk.scope import should_send_default_pii
@@ -36,13 +36,7 @@ class AriadneIntegration(Integration):
3636
def setup_once():
3737
# type: () -> None
3838
version = package_version("ariadne")
39-
40-
if version is None:
41-
raise DidNotEnable("Unparsable ariadne version.")
42-
43-
min_version = _MIN_VERSIONS[AriadneIntegration.identifier]
44-
if version < min_version:
45-
raise DidNotEnable(f"ariadne {'.'.join(map(str, min_version))} or newer required.")
39+
_check_minimum_version(AriadneIntegration, version)
4640

4741
ignore_logger("ariadne")
4842

sentry_sdk/integrations/arq.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sentry_sdk
44
from sentry_sdk.consts import OP, SPANSTATUS
5-
from sentry_sdk.integrations import _MIN_VERSIONS, DidNotEnable, Integration
5+
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
66
from sentry_sdk.integrations.logging import ignore_logger
77
from sentry_sdk.scope import should_send_default_pii
88
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_TASK
@@ -55,12 +55,7 @@ def setup_once():
5555
except (TypeError, ValueError):
5656
version = None
5757

58-
if version is None:
59-
raise DidNotEnable("Unparsable arq version: {}".format(ARQ_VERSION))
60-
61-
min_version = _MIN_VERSIONS[ArqIntegration.identifier]
62-
if version < min_version:
63-
raise DidNotEnable(f"arq {'.'.join(map(str, min_version))} or newer required.")
58+
_check_minimum_version(ArqIntegration, version)
6459

6560
patch_enqueue_job()
6661
patch_run_job()

sentry_sdk/integrations/asyncpg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sentry_sdk
66
from sentry_sdk.consts import OP, SPANDATA
7-
from sentry_sdk.integrations import _MIN_VERSIONS, Integration, DidNotEnable
7+
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
88
from sentry_sdk.tracing import Span
99
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
1010
from sentry_sdk.utils import (
@@ -34,9 +34,7 @@ def setup_once() -> None:
3434
# asyncpg.__version__ is a string containing the semantic version in the form of "<major>.<minor>.<patch>"
3535
asyncpg_version = parse_version(asyncpg.__version__)
3636

37-
min_version = _MIN_VERSIONS[AsyncPGIntegration.identifier]
38-
if asyncpg_version is not None and asyncpg_version < min_version:
39-
raise DidNotEnable(f"asyncpg >= {'.'.join(map(str, min_version))} required")
37+
_check_minimum_version(AsyncPGIntegration, asyncpg_version)
4038

4139
asyncpg.Connection.execute = _wrap_execute(
4240
asyncpg.Connection.execute,

sentry_sdk/integrations/boto3.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sentry_sdk
44
from sentry_sdk.consts import OP, SPANDATA
5-
from sentry_sdk.integrations import _MIN_VERSIONS, Integration, DidNotEnable
5+
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
66
from sentry_sdk.tracing import Span
77
from sentry_sdk.utils import (
88
capture_internal_exceptions,
@@ -35,17 +35,8 @@ class Boto3Integration(Integration):
3535
@staticmethod
3636
def setup_once():
3737
# type: () -> None
38-
3938
version = parse_version(BOTOCORE_VERSION)
40-
41-
if version is None:
42-
raise DidNotEnable(
43-
"Unparsable botocore version: {}".format(BOTOCORE_VERSION)
44-
)
45-
46-
min_version = _MIN_VERSIONS[Boto3Integration.identifier]
47-
if version < min_version:
48-
raise DidNotEnable(f"Botocore {'.'.join(map(str, min_version))} or newer is required.")
39+
_check_minimum_version(Boto3Integration, version, 'botocore')
4940

5041
orig_init = BaseClient.__init__
5142

sentry_sdk/integrations/bottle.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Integration,
1414
DidNotEnable,
1515
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
16-
_MIN_VERSIONS,
16+
_check_minimum_version,
1717
)
1818
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1919
from sentry_sdk.integrations._wsgi_common import RequestExtractor
@@ -73,13 +73,7 @@ def __init__(
7373
def setup_once():
7474
# type: () -> None
7575
version = parse_version(BOTTLE_VERSION)
76-
77-
if version is None:
78-
raise DidNotEnable("Unparsable Bottle version: {}".format(BOTTLE_VERSION))
79-
80-
min_version = _MIN_VERSIONS[BottleIntegration.identifier]
81-
if version < min_version:
82-
raise DidNotEnable(f"Bottle {'.'.join(map(str, min_version))} or newer required.")
76+
_check_minimum_version(BottleIntegration, version)
8377

8478
old_app = Bottle.__call__
8579

sentry_sdk/integrations/celery/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sentry_sdk import isolation_scope
77
from sentry_sdk.api import continue_trace
88
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA
9-
from sentry_sdk.integrations import _MIN_VERSIONS, Integration, DidNotEnable
9+
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
1010
from sentry_sdk.integrations.celery.beat import (
1111
_patch_beat_apply_entry,
1212
_patch_redbeat_maybe_due,
@@ -79,9 +79,7 @@ def __init__(
7979
@staticmethod
8080
def setup_once():
8181
# type: () -> None
82-
min_version = _MIN_VERSIONS[CeleryIntegration.identifier]
83-
if CELERY_VERSION < min_version:
84-
raise DidNotEnable(f"Celery {'.'.join(map(str, min_version))} or newer required.")
82+
_check_minimum_version(CeleryIntegration, CELERY_VERSION)
8583

8684
_patch_build_tracer()
8785
_patch_task_apply_async()

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sentry_sdk
22
from sentry_sdk.consts import OP, SPANDATA
3-
from sentry_sdk.integrations import _MIN_VERSIONS, Integration, DidNotEnable
3+
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
44
from sentry_sdk.tracing import Span
55
from sentry_sdk.scope import should_send_default_pii
66
from sentry_sdk.utils import capture_internal_exceptions, ensure_integration_enabled
@@ -42,10 +42,7 @@ class ClickhouseDriverIntegration(Integration):
4242

4343
@staticmethod
4444
def setup_once() -> None:
45-
min_version = _MIN_VERSIONS[ClickhouseDriverIntegration.identifier]
46-
47-
if clickhouse_driver.VERSION < min_version:
48-
raise DidNotEnable(f"clickhouse-driver >= {'.'.join(map(str, min_version))} required")
45+
_check_minimum_version(ClickhouseDriverIntegration, clickhouse_driver.VERSION)
4946

5047
# Every query is done using the Connection's `send_query` function
5148
clickhouse_driver.connection.Connection.send_query = _wrap_start(

sentry_sdk/integrations/django/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
transaction_from_function,
2323
walk_exception_chain,
2424
)
25-
from sentry_sdk.integrations import _MIN_VERSIONS, Integration, DidNotEnable
25+
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
2626
from sentry_sdk.integrations.logging import ignore_logger
2727
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
2828
from sentry_sdk.integrations._wsgi_common import (
@@ -154,9 +154,7 @@ def __init__(
154154
@staticmethod
155155
def setup_once():
156156
# type: () -> None
157-
min_version = _MIN_VERSIONS[DjangoIntegration.identifier]
158-
if DJANGO_VERSION < min_version:
159-
raise DidNotEnable(f"Django {'.'.join(map(str, min_version))} or newer is required.")
157+
_check_minimum_version(DjangoIntegration, DJANGO_VERSION)
160158

161159
install_sql_hook()
162160
# Patch in our custom middleware.

0 commit comments

Comments
 (0)