Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor websockets handling #2836

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mypy_path = ./stubs
python_version = 3.10
warn_unused_configs = True
allow_untyped_globals = True
exclude = env
exclude = (env\/|build\/)

[mypy-nose.*]
ignore_missing_imports = True
Expand Down Expand Up @@ -144,9 +144,6 @@ ignore_missing_imports = True
[mypy-draftjs_exporter.*]
ignore_missing_imports = True

[mypy-raven.*]
ignore_missing_imports = True

[mypy-kombu.*]
ignore_missing_imports = True

Expand Down Expand Up @@ -191,3 +188,6 @@ ignore_missing_imports = True

[mypy-pyexiv2.*]
ignore_missing_imports = True

[mypy-sentry_sdk.*]
ignore_missing_imports = True
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"ldap3>=2.2.4,<2.10",
"pytz>=2021.3",
"tzlocal>=2.1,<3.0",
"raven[flask]>=5.10,<7.0",
"sentry-sdk[flask]>=2.0.0,<3.0.0", # Replacing raven[flask]>=5.10,<7.0
"requests>=2.7.0,<3.0",
"boto3>=1.26,<2.0",
"websockets>=10.3,<13.2",
"websockets>=14.1,<16",
"PyYAML>=6.0.1",
"lxml>=5.2.2,<5.4",
"lxml_html_clean>=0.1.1,<0.5",
Expand Down
4 changes: 4 additions & 0 deletions superdesk/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ def local_to_utc_hour(hour):
SENTRY_DSN = env("SENTRY_DSN")
SENTRY_INCLUDE_PATHS = ["superdesk", "apps"]

#: Set to number between 0.0 to 1.0 to enable sentry Enable Sentry traces
SENTRY_TRACES_SAMPLE_RATE = float(os.environ.get("SENTRY_TRACES_SAMPLE_RATE", "0")) or None
SENTRY_PROFILES_SAMPLE_RATE = float(os.environ.get("SENTRY_PROFILES_SAMPLE_RATE", "0")) or None

CORE_APPS = [
"apps.auth",
"superdesk.roles",
Expand Down
51 changes: 31 additions & 20 deletions superdesk/factory/sentry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from raven.contrib.flask import Sentry
from raven.contrib.celery import register_signal, register_logger_signal
import flask
import sentry_sdk

from sentry_sdk.integrations.celery import CeleryIntegration


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

def __init__(self, app):
def __init__(self, app: flask.Flask) -> None:
self.enabled = False
if app.config.get(SENTRY_DSN):
if "verify_ssl" not in app.config[SENTRY_DSN]:
app.config[SENTRY_DSN] += "?verify_ssl=0"
app.config.setdefault("SENTRY_NAME", app.config.get("SERVER_DOMAIN"))
self.sentry = Sentry(app, register_signal=False, wrap_wsgi=False, logging=True, level=logging.WARNING)
register_logger_signal(self.sentry.client)
register_signal(self.sentry.client)
else:
self.sentry = None

def captureException(self, exc_info=None, **kwargs):
if self.sentry:
self.sentry.captureException(exc_info, **kwargs)

def captureMessage(self, message, **kwargs):
if self.sentry:
self.sentry.captureMessage(message, **kwargs)
self.enabled = True
dsn = app.config[SENTRY_DSN]
if "verify_ssl" not in dsn:
dsn += "?verify_ssl=0"
sentry_sdk.init(
dsn=dsn,
send_default_pii=True,
server_name=app.config.get("SERVER_DOMAIN"),
debug=app.debug,
traces_sample_rate=app.config.get("SENTRY_TRACES_SAMPLE_RATE"),
profiles_sample_rate=app.config.get("SENTRY_PROFILES_SAMPLE_RATE"),
integrations=[
CeleryIntegration(
monitor_beat_tasks=True,
),
],
)

def captureException(self, exc_info=None, **kwargs) -> None:
if self.enabled:
sentry_sdk.capture_exception(exc_info)

def captureMessage(self, message, **kwargs) -> None:
if self.enabled:
sentry_sdk.capture_message(message)
2 changes: 1 addition & 1 deletion superdesk/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ def push_notification(name, filters: Optional[WebsocketMessageFilterConditions]

message = _create_socket_message(**msg_kwargs)
logger.debug("Sending the message: {} to the broker.".format(message))
app.notification_client.send(message)
app.notification_client.send(message, name)
except Exception as err:
logger.exception(err)
2 changes: 1 addition & 1 deletion superdesk/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def __init__(self):
self.client = None
self.open = True

def send(self, message):
def send(self, message, name):
self.messages.append(message)

def reset(self):
Expand Down
Loading