Skip to content

Commit

Permalink
Fix #1406 introduce sentry to better diagnose runtime errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Sep 14, 2024
1 parent 11aed6b commit b165c0e
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions emperor.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ strict-mode = true
protocol = uwsgi
master = true
enable-threads = true
lazy-apps = true
emperor = ./vassals/*/*.ini

# See https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html#passing-configuration-parameters-to-all-vassals
Expand All @@ -13,3 +14,5 @@ emperor-on-demand-extension = .socket
fastrouter = 127.0.0.1:8001
fastrouter-subscription-server = /tmp/sock2
vacuum = true

log-format = %(host) - %(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)"
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ flask db upgrade

flask initdb

exec uwsgi --http :5000 --workers 1 --threads 2 --wsgi-file subscribie.wsgi --touch-chain-reload subscribie.wsgi --chdir /usr/src/app/subscribie/
exec uwsgi --http :5000 --workers 1 --threads 2 --py-call-uwsgi-fork-hooks --wsgi-file subscribie.wsgi --touch-chain-reload subscribie.wsgi --chdir /usr/src/app/subscribie/

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"scikit-learn",
"black",
"strictyaml",
"sentry-sdk[flask]>=2.14.0",
]
readme = "README.md"
requires-python = ">= 3.12"
Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ black==24.4.2
blinker==1.8.2
# via flask
# via flask-mail
# via sentry-sdk
# via subscribie
certifi==2024.2.2
# via requests
# via sentry-sdk
cffi==1.16.0
# via cryptography
charset-normalizer==3.3.2
Expand All @@ -50,6 +52,7 @@ flask==3.0.3
# via flask-reuploaded
# via flask-sqlalchemy
# via flask-wtf
# via sentry-sdk
# via subscribie
flask-babel==4.0.0
# via subscribie
Expand Down Expand Up @@ -96,6 +99,7 @@ mako==1.3.5
markupsafe==2.1.5
# via jinja2
# via mako
# via sentry-sdk
# via werkzeug
# via wtforms
mypy-extensions==1.0.0
Expand Down Expand Up @@ -148,6 +152,8 @@ scikit-learn==1.5.0
scipy==1.13.1
# via scikit-learn
# via subscribie
sentry-sdk==2.14.0
# via subscribie
six==1.16.0
# via python-dateutil
smmap==5.0.1
Expand All @@ -172,6 +178,7 @@ tzdata==2024.1
# via pandas
urllib3==2.2.1
# via requests
# via sentry-sdk
werkzeug==3.0.3
# via flask
wheel==0.43.0
Expand Down
7 changes: 7 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ black==24.4.2
blinker==1.8.2
# via flask
# via flask-mail
# via sentry-sdk
# via subscribie
certifi==2024.2.2
# via requests
# via sentry-sdk
cffi==1.16.0
# via cryptography
charset-normalizer==3.3.2
Expand All @@ -50,6 +52,7 @@ flask==3.0.3
# via flask-reuploaded
# via flask-sqlalchemy
# via flask-wtf
# via sentry-sdk
# via subscribie
flask-babel==4.0.0
# via subscribie
Expand Down Expand Up @@ -96,6 +99,7 @@ mako==1.3.5
markupsafe==2.1.5
# via jinja2
# via mako
# via sentry-sdk
# via werkzeug
# via wtforms
mypy-extensions==1.0.0
Expand Down Expand Up @@ -148,6 +152,8 @@ scikit-learn==1.5.0
scipy==1.13.1
# via scikit-learn
# via subscribie
sentry-sdk==2.14.0
# via subscribie
six==1.16.0
# via python-dateutil
smmap==5.0.1
Expand All @@ -172,6 +178,7 @@ tzdata==2024.1
# via pandas
urllib3==2.2.1
# via requests
# via sentry-sdk
werkzeug==3.0.3
# via flask
wheel==0.43.0
Expand Down
4 changes: 4 additions & 0 deletions settings.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ FLASK_ENV: development
# SESSION_COOKIE_HTTPONLY: True
# SESSION_COOKIE_SAMESITE: None

# Sentry_sdk_dsn
SENTRY_SDK_DSN: "https://[email protected]/123456789"
SENTRY_SDK_SESSION_REPLAY_ID: "changeme"

# Software as a service (SAAS)
SAAS_URL: https://subscribie.co.uk/
# SAAS_API_KEY is to allow subscribie platform to send authenticated
Expand Down
22 changes: 22 additions & 0 deletions subscribie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:copyright: (c) 2018 by Karma Computing Ltd
"""
import sentry_sdk
from subscribie.settings import settings
from .logger import logger # noqa: F401
import logging
Expand All @@ -25,6 +26,17 @@
request,
abort,
)

sentry_sdk.init(
dsn=settings.get("SENTRY_SDK_DSN"),
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
from flask_babel import Babel, _
from subscribie.email import EmailMessageQueue
from .Template import load_theme
Expand Down Expand Up @@ -378,4 +390,14 @@ def alert_subscriber_update_choices(subscriber: Person):
def test_language():
return _("Hello")

@app.route("/error")
def raise_error():
1 / 0 # raises an error
return ""

@app.route("/ui-error")
def raise_ui_error():
"""Raises UI error"""
return render_template("errors/ui-error.html"), 200

return app
1 change: 1 addition & 0 deletions subscribie/blueprints/admin/templates/admin/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<link rel="stylesheet" href="{{ url_for('admin.static', filename='custom_style.css') }}">
<link href="https://fonts.googleapis.com/css?family=Lato:900,400" rel="stylesheet">
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://js-de.sentry-cdn.com/{{ SENTRY_SDK_SESSION_REPLAY_ID }}.min.js" crossorigin="anonymous"></script>

{% if integration.google_tag_manager_container_id
and integration.google_tag_manager_active
Expand Down
7 changes: 7 additions & 0 deletions subscribie/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from strictyaml import load, Map, Email, Str, Url, Int, Bool, Regex, CommaSeparated
import os

# Load application settings according to schema

Expand All @@ -7,6 +8,8 @@
schema = Map(
{
"FLASK_ENV": Str(),
"SENTRY_SDK_DSN": Str(),
"SENTRY_SDK_SESSION_REPLAY_ID": Str(),
"SAAS_URL": Url(),
"SAAS_API_KEY": Str(),
"SAAS_ACTIVATE_ACCOUNT_PATH": Str(),
Expand Down Expand Up @@ -57,6 +60,10 @@ def load_settings():
with open("settings.yaml") as fp:
settings_string = fp.read()
settings = load(settings_string, schema)
for key in schema._required_keys:
if key in os.environ:
print(f"Overriding setting {key} with environ value: {os.getenv(key)}")
settings[key] = os.getenv(key)
return settings


Expand Down
10 changes: 10 additions & 0 deletions subscribie/themes/theme-jesmond/jesmond/errors/ui-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}

{% block body %}
<script>
window.onload = () => {
throw new Error('This is a test error');
};
</script>

{% endblock %}
1 change: 1 addition & 0 deletions subscribie/themes/theme-jesmond/jesmond/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="/static/style.css">
<link href="https://fonts.googleapis.com/css?family=Lato:900,400" rel="stylesheet">
<script src="https://js.stripe.com/v3/"></script>
<script src="https://js-de.sentry-cdn.com/{{ SENTRY_SDK_SESSION_REPLAY_ID }}.min.js" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script>
{% if custom_code != "None" %}
<!-- custom code -->
Expand Down
3 changes: 3 additions & 0 deletions subscribie/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ def check_if_inside_iframe():

@bp.app_context_processor
def inject_template_globals():
from subscribie.settings import settings as internal_settings
company = Company.query.first()
integration = Integration.query.first()
plans = Plan.query.filter_by(archived=0)
pages = Page.query.all()
settings = Setting.query.first()
custom_code = settings.custom_code
SENTRY_SDK_SESSION_REPLAY_ID = internal_settings.get("SENTRY_SDK_SESSION_REPLAY_ID")
geo_currency_symbol = get_geo_currency_symbol()
default_currency_symbol = get_shop_default_currency_symbol()
currency_format = currencyFormat
Expand All @@ -166,6 +168,7 @@ def inject_template_globals():
plans=plans,
pages=pages,
custom_code=Markup(custom_code),
SENTRY_SDK_SESSION_REPLAY_ID=SENTRY_SDK_SESSION_REPLAY_ID,
geo_currency_symbol=geo_currency_symbol,
get_geo_currency_code=get_geo_currency_code,
default_currency_symbol=default_currency_symbol,
Expand Down

0 comments on commit b165c0e

Please sign in to comment.