Skip to content

Commit 1e0839a

Browse files
committed
Centralize minimum version checking
1 parent 4432e26 commit 1e0839a

File tree

20 files changed

+85
-116
lines changed

20 files changed

+85
-116
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 41 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,
@@ -120,6 +119,30 @@ def iter_default_integrations(with_auto_enabling_integrations):
120119
del _generate_default_integrations_iterator
121120

122121

122+
_MIN_VERSIONS = {
123+
"aiohttp": (3, 4),
124+
"anthropic": (0, 16),
125+
"ariadne": (0, 20),
126+
"arq": (0, 23),
127+
"asyncpg": (0, 23),
128+
"boto3": (1, 12), # this is actually the botocore version
129+
"bottle": (0, 12),
130+
"celery": (4, 4, 7),
131+
"clickhouse_driver": (0, 2, 0),
132+
"django": (1, 8),
133+
"falcon": (1, 4),
134+
"flask": (0, 10),
135+
"gql": (3, 4, 1),
136+
"graphene": (3, 3),
137+
"ray": (2, 7, 0),
138+
"rq": (0, 6),
139+
"sanic": (0, 8),
140+
"sqlalchemy": (1, 2),
141+
"strawberry": (0, 209, 5),
142+
"tornado": (6, 0),
143+
}
144+
145+
123146
def setup_integrations(
124147
integrations,
125148
with_defaults=True,
@@ -195,6 +218,23 @@ def setup_integrations(
195218
return integrations
196219

197220

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

sentry_sdk/integrations/aiohttp.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA
88
from sentry_sdk.integrations import (
99
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
10+
_check_minimum_version,
1011
Integration,
1112
DidNotEnable,
1213
)
@@ -91,12 +92,7 @@ def setup_once():
9192
# type: () -> None
9293

9394
version = parse_version(AIOHTTP_VERSION)
94-
95-
if version is None:
96-
raise DidNotEnable("Unparsable AIOHTTP version: {}".format(AIOHTTP_VERSION))
97-
98-
if version < (3, 4):
99-
raise DidNotEnable("AIOHTTP 3.4 or newer required.")
95+
_check_minimum_version(AioHttpIntegration, version)
10096

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

sentry_sdk/integrations/ariadne.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 import get_client, capture_event
5-
from sentry_sdk.integrations import 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,12 +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-
if version < (0, 20):
44-
raise DidNotEnable("ariadne 0.20 or newer required.")
39+
_check_minimum_version(AriadneIntegration, version)
4540

4641
ignore_logger("ariadne")
4742

sentry_sdk/integrations/arq.py

Lines changed: 2 additions & 6 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 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,11 +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-
if version < (0, 23):
62-
raise DidNotEnable("arq 0.23 or newer required.")
58+
_check_minimum_version(ArqIntegration, version)
6359

6460
patch_enqueue_job()
6561
patch_run_job()

sentry_sdk/integrations/asyncpg.py

Lines changed: 6 additions & 1 deletion
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 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 (
@@ -37,6 +37,11 @@ def __init__(self, *, record_params: bool = False):
3737

3838
@staticmethod
3939
def setup_once() -> None:
40+
# asyncpg.__version__ is a string containing the semantic version in the form of "<major>.<minor>.<patch>"
41+
asyncpg_version = parse_version(asyncpg.__version__)
42+
43+
_check_minimum_version(AsyncPGIntegration, asyncpg_version)
44+
4045
asyncpg.Connection.execute = _wrap_execute(
4146
asyncpg.Connection.execute,
4247
)

sentry_sdk/integrations/boto3.py

Lines changed: 2 additions & 10 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 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,16 +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-
if version < (1, 12):
47-
raise DidNotEnable("Botocore 1.12 or newer is required.")
39+
_check_minimum_version(Boto3Integration, version, "botocore")
4840

4941
orig_init = BaseClient.__init__
5042

sentry_sdk/integrations/bottle.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Integration,
1414
DidNotEnable,
1515
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
16+
_check_minimum_version,
1617
)
1718
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1819
from sentry_sdk.integrations._wsgi_common import RequestExtractor
@@ -72,12 +73,7 @@ def __init__(
7273
def setup_once():
7374
# type: () -> None
7475
version = parse_version(BOTTLE_VERSION)
75-
76-
if version is None:
77-
raise DidNotEnable("Unparsable Bottle version: {}".format(BOTTLE_VERSION))
78-
79-
if version < (0, 12):
80-
raise DidNotEnable("Bottle 0.12 or newer required.")
76+
_check_minimum_version(BottleIntegration, version)
8177

8278
old_app = Bottle.__call__
8379

sentry_sdk/integrations/celery/__init__.py

Lines changed: 2 additions & 3 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 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,8 +79,7 @@ def __init__(
7979
@staticmethod
8080
def setup_once():
8181
# type: () -> None
82-
if CELERY_VERSION < (4, 4, 7):
83-
raise DidNotEnable("Celery 4.4.7 or newer required.")
82+
_check_minimum_version(CeleryIntegration, CELERY_VERSION)
8483

8584
_patch_build_tracer()
8685
_patch_task_apply_async()

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 3 additions & 1 deletion
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 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
@@ -44,6 +44,8 @@ class ClickhouseDriverIntegration(Integration):
4444

4545
@staticmethod
4646
def setup_once() -> None:
47+
_check_minimum_version(ClickhouseDriverIntegration, clickhouse_driver.VERSION)
48+
4749
# Every query is done using the Connection's `send_query` function
4850
clickhouse_driver.connection.Connection.send_query = _wrap_start(
4951
clickhouse_driver.connection.Connection.send_query

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 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-
158-
if DJANGO_VERSION < (1, 8):
159-
raise DidNotEnable("Django 1.8 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)