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
Start using Sentry in our general stack #143
Changes from 4 commits
e33728d
03fb769
ba6301e
2394c1d
9312c7f
c10825a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,56 @@ | |
|
||
import click | ||
from flask import Flask | ||
from flask.cli import FlaskGroup | ||
from flask.cli import FlaskGroup, with_appcontext | ||
import sentry_sdk | ||
from sentry_sdk.integrations.flask import FlaskIntegration | ||
from sentry_sdk.integrations.rq import RqIntegration | ||
from pkg_resources import get_distribution | ||
|
||
from flexmeasures.app import create as create_app | ||
|
||
|
||
@click.group(cls=FlaskGroup, create_app=create_app) | ||
@with_appcontext | ||
def flexmeasures_cli(): | ||
"""Management scripts for the FlexMeasures platform.""" | ||
""" | ||
Management scripts for the FlexMeasures platform. | ||
We use @app_context here so things from the app setup are initialised | ||
only once. This is crucial for Sentry, for example. | ||
""" | ||
pass | ||
|
||
|
||
def init_sentry(app: Flask): | ||
""" | ||
Configure Sentry. | ||
We need the app to read the Sentry DSN from configuration, and also | ||
to send some additional meta information. | ||
""" | ||
sentry_dsn = app.config.get("SENTRY_DSN") | ||
if not sentry_dsn: | ||
app.logger.info( | ||
"[FLEXMEASURES] No SENTRY_DSN setting found, so initialising Sentry cannot happen ..." | ||
) | ||
return | ||
app.logger.info("[FLEXMEASURES] Initialising Sentry ...") | ||
sentry_sdk.init( | ||
dsn=sentry_dsn, | ||
integrations=[FlaskIntegration(), RqIntegration()], | ||
debug=app.debug, | ||
release=f"flexmeasures@{get_distribution('flexmeasures').version}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be useful to also add which plugins are loaded (incl. their version). I'm not sure whether this is the right place to put such info, but this is where the thought came up for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is already handled in line 146. |
||
send_default_pii=True, # user data (current user id, email address, username) is attached to the event. | ||
environment=app.env, | ||
# Set traces_sample_rate to 1.0 to capture 100% | ||
# of transactions for performance monitoring. | ||
# We recommend adjusting this value in production. | ||
# TODO: Decide if we need this and if to configure it. | ||
traces_sample_rate=0.33, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a potential FM config setting to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe in the form of a default |
||
) | ||
sentry_sdk.set_tag("mode", app.config.get("FLEXMEASURES_MODE")) | ||
sentry_sdk.set_tag("platform-name", app.config.get("FLEXMEASURES_PLATFORM_NAME")) | ||
|
||
|
||
def set_secret_key(app, filename="secret_key"): | ||
"""Set the SECRET_KEY or exit. | ||
|
||
|
@@ -104,3 +143,4 @@ def register_plugins(app: Flask): | |
plugin_version = getattr(plugin_blueprint, "__version__", "0.1") | ||
app.config["LOADED_PLUGINS"][plugin_name] = plugin_version | ||
app.logger.info(f"Loaded plugins: {app.config['LOADED_PLUGINS']}") | ||
sentry_sdk.set_context("plugins", app.config.get("LOADED_PLUGINS", {})) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ Flask-Security-Too>=4.0 | |
Flask-Classful | ||
Flask-Marshmallow | ||
Flask-Cors | ||
sentry-sdk[flask] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you comment on this notation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the common syntax for libraries that also install to load certain extensions. In this case, Flask is the extension. As it is rather standard, I'd like to not add a comment. |
||
marshmallow-sqlalchemy>=0.23.1 | ||
webargs | ||
# flask should be after all the flask plugins, because setup might find they ARE flask | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: DSN instead of SDN.