Skip to content

Commit 88d3709

Browse files
authored
fix: pass sentry_logs_level only to sentry-sdk versions that accept it (#244)
1 parent 5e96b5c commit 88d3709

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎lite_bootstrap/instruments/sentry_instrument.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import inspect
23
import logging
34
import typing
45

@@ -17,6 +18,12 @@
1718
import sentry_sdk
1819
from sentry_sdk.integrations.logging import LoggingIntegration
1920

21+
# sentry-sdk 2.25 added the parameter along with Sentry Logs; the declared floor is 2.1,
22+
# where passing it raises TypeError and there is no logs handler to disable anyway.
23+
SENTRY_LOGS_LEVEL_SUPPORTED: typing.Final = (
24+
"sentry_logs_level" in inspect.signature(LoggingIntegration.__init__).parameters
25+
)
26+
2027

2128
# Back-compat alias: this vocabulary moved to logging_factory and was renamed
2229
# STRUCTLOG_META_KEYS. Preserved here for external importers of the old name.
@@ -110,10 +117,10 @@ def _build_integrations(self) -> list["Integration"]:
110117
if not config.sentry_default_integrations:
111118
self._warn_breadcrumb_level_ignored("sentry_default_integrations is False")
112119
return config.sentry_integrations
113-
return [
114-
*config.sentry_integrations,
115-
LoggingIntegration(level=config.sentry_logging_breadcrumb_level, sentry_logs_level=None),
116-
]
120+
logging_integration_kwargs: dict[str, typing.Any] = {"level": config.sentry_logging_breadcrumb_level}
121+
if SENTRY_LOGS_LEVEL_SUPPORTED:
122+
logging_integration_kwargs["sentry_logs_level"] = None
123+
return [*config.sentry_integrations, LoggingIntegration(**logging_integration_kwargs)]
117124

118125
def bootstrap(self) -> None:
119126
config = self.bootstrap_config

‎tests/instruments/test_sentry_instrument.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import structlog
1010
from sentry_sdk.integrations.logging import LoggingIntegration
1111

12+
from lite_bootstrap.instruments import sentry_instrument
1213
from lite_bootstrap.instruments.logging_instrument import LoggingConfig, LoggingInstrument
1314
from tests.conftest import LoggingMock, SentryTestTransport
1415

@@ -273,3 +274,20 @@ def test_sentry_does_not_warn_when_the_breadcrumb_level_is_left_at_its_default(
273274
assert [one for one in recwarn if "sentry_logging_breadcrumb_level" in str(one.message)] == []
274275
finally:
275276
instrument.teardown()
277+
278+
279+
@pytest.mark.parametrize("supported", [True, False], ids=["sdk_has_it", "sdk_lacks_it"])
280+
def test_sentry_passes_sentry_logs_level_only_when_the_sdk_accepts_it(
281+
minimal_sentry_config: SentryConfig, monkeypatch: pytest.MonkeyPatch, supported: bool
282+
) -> None:
283+
"""INVARIANT: `sentry_logs_level` reaches only the sentry-sdk versions that accept it.
284+
285+
It arrived in sentry-sdk 2.25 while the declared floor is 2.1, where passing it raises
286+
`TypeError: LoggingIntegration.__init__() got an unexpected keyword argument` at bootstrap.
287+
Below 2.25 there is no Sentry Logs feature, so there is no handler to disable either.
288+
"""
289+
monkeypatch.setattr(sentry_instrument, "SENTRY_LOGS_LEVEL_SUPPORTED", supported)
290+
integrations = SentryInstrument(bootstrap_config=minimal_sentry_config)._build_integrations() # noqa: SLF001
291+
292+
logging_integration = next(one for one in integrations if isinstance(one, LoggingIntegration))
293+
assert (logging_integration._sentry_logs_handler is None) is supported # noqa: SLF001

0 commit comments

Comments
 (0)