Skip to content

Commit 6ba1d71

Browse files
authored
refactor websockets handling (#2836)
- drop unused server loop - drop pings (it's handled via websockets library) - setup sentry_sdk SDCP-879
1 parent 0164b52 commit 6ba1d71

9 files changed

+273
-212
lines changed

setup.cfg

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mypy_path = ./stubs
2828
python_version = 3.10
2929
warn_unused_configs = True
3030
allow_untyped_globals = True
31-
exclude = env
31+
exclude = (env\/|build\/)
3232

3333
[mypy-nose.*]
3434
ignore_missing_imports = True
@@ -144,9 +144,6 @@ ignore_missing_imports = True
144144
[mypy-draftjs_exporter.*]
145145
ignore_missing_imports = True
146146

147-
[mypy-raven.*]
148-
ignore_missing_imports = True
149-
150147
[mypy-kombu.*]
151148
ignore_missing_imports = True
152149

@@ -191,3 +188,6 @@ ignore_missing_imports = True
191188

192189
[mypy-pyexiv2.*]
193190
ignore_missing_imports = True
191+
192+
[mypy-sentry_sdk.*]
193+
ignore_missing_imports = True

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
"ldap3>=2.2.4,<2.10",
3737
"pytz>=2021.3",
3838
"tzlocal>=2.1,<3.0",
39-
"raven[flask]>=5.10,<7.0",
39+
"sentry-sdk[flask]>=2.0.0,<3.0.0", # Replacing raven[flask]>=5.10,<7.0
4040
"requests>=2.7.0,<3.0",
4141
"boto3>=1.26,<2.0",
42-
"websockets>=10.3,<13.2",
42+
"websockets>=14.1,<16",
4343
"PyYAML>=6.0.1",
4444
"lxml>=5.2.2,<5.4",
4545
"lxml_html_clean>=0.1.1,<0.5",

superdesk/default_settings.py

+4
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ def local_to_utc_hour(hour):
382382
SENTRY_DSN = env("SENTRY_DSN")
383383
SENTRY_INCLUDE_PATHS = ["superdesk", "apps"]
384384

385+
#: Set to number between 0.0 to 1.0 to enable sentry Enable Sentry traces
386+
SENTRY_TRACES_SAMPLE_RATE = float(os.environ.get("SENTRY_TRACES_SAMPLE_RATE", "0")) or None
387+
SENTRY_PROFILES_SAMPLE_RATE = float(os.environ.get("SENTRY_PROFILES_SAMPLE_RATE", "0")) or None
388+
385389
CORE_APPS = [
386390
"apps.auth",
387391
"superdesk.roles",

superdesk/factory/sentry.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import logging
2-
from raven.contrib.flask import Sentry
3-
from raven.contrib.celery import register_signal, register_logger_signal
1+
import flask
2+
import sentry_sdk
3+
4+
from sentry_sdk.integrations.celery import CeleryIntegration
45

56

67
SENTRY_DSN = "SENTRY_DSN"
@@ -9,21 +10,31 @@
910
class SuperdeskSentry:
1011
"""Sentry proxy that will do nothing in case sentry is not configured."""
1112

12-
def __init__(self, app):
13+
def __init__(self, app: flask.Flask) -> None:
14+
self.enabled = False
1315
if app.config.get(SENTRY_DSN):
14-
if "verify_ssl" not in app.config[SENTRY_DSN]:
15-
app.config[SENTRY_DSN] += "?verify_ssl=0"
16-
app.config.setdefault("SENTRY_NAME", app.config.get("SERVER_DOMAIN"))
17-
self.sentry = Sentry(app, register_signal=False, wrap_wsgi=False, logging=True, level=logging.WARNING)
18-
register_logger_signal(self.sentry.client)
19-
register_signal(self.sentry.client)
20-
else:
21-
self.sentry = None
22-
23-
def captureException(self, exc_info=None, **kwargs):
24-
if self.sentry:
25-
self.sentry.captureException(exc_info, **kwargs)
26-
27-
def captureMessage(self, message, **kwargs):
28-
if self.sentry:
29-
self.sentry.captureMessage(message, **kwargs)
16+
self.enabled = True
17+
dsn = app.config[SENTRY_DSN]
18+
if "verify_ssl" not in dsn:
19+
dsn += "?verify_ssl=0"
20+
sentry_sdk.init(
21+
dsn=dsn,
22+
send_default_pii=True,
23+
server_name=app.config.get("SERVER_DOMAIN"),
24+
debug=app.debug,
25+
traces_sample_rate=app.config.get("SENTRY_TRACES_SAMPLE_RATE"),
26+
profiles_sample_rate=app.config.get("SENTRY_PROFILES_SAMPLE_RATE"),
27+
integrations=[
28+
CeleryIntegration(
29+
monitor_beat_tasks=True,
30+
),
31+
],
32+
)
33+
34+
def captureException(self, exc_info=None, **kwargs) -> None:
35+
if self.enabled:
36+
sentry_sdk.capture_exception(exc_info)
37+
38+
def captureMessage(self, message, **kwargs) -> None:
39+
if self.enabled:
40+
sentry_sdk.capture_message(message)

superdesk/notification.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ def push_notification(name, filters: Optional[WebsocketMessageFilterConditions]
8484

8585
message = _create_socket_message(**msg_kwargs)
8686
logger.debug("Sending the message: {} to the broker.".format(message))
87-
app.notification_client.send(message)
87+
app.notification_client.send(message, name)
8888
except Exception as err:
8989
logger.exception(err)

superdesk/tests/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def __init__(self):
486486
self.client = None
487487
self.open = True
488488

489-
def send(self, message):
489+
def send(self, message, name):
490490
self.messages.append(message)
491491

492492
def reset(self):

0 commit comments

Comments
 (0)