From e04b87ed7b7785a21fc6d5afea24f0a9313a2714 Mon Sep 17 00:00:00 2001 From: --global Date: Wed, 19 Jun 2024 14:53:57 -0400 Subject: [PATCH] add sentry --- app/config.py | 2 ++ app/data/celery.py | 15 ++++----------- app/main.py | 48 ++++++++++++++++++++++------------------------ requirements.in | 1 + requirements.txt | 7 +++++++ 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/app/config.py b/app/config.py index e6c536d..e5598df 100644 --- a/app/config.py +++ b/app/config.py @@ -202,6 +202,8 @@ def SQLALCHEMY_DATABASE_URI(self) -> str: "pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw", ) # noqa + SENTRY_DSN: str | None = os.getenv("SENTRY_DSN") + class ProductionConfig(Config): """The Production Config is used for deployment of the website to the diff --git a/app/data/celery.py b/app/data/celery.py index 0a4ca85..2fb3ec3 100644 --- a/app/data/celery.py +++ b/app/data/celery.py @@ -125,20 +125,14 @@ def predict_v3_task(*args, **kwargs) -> RecordsType: @celery_app.task -def update_db_task() -> None: - from app.data.processing.core import update_db - - update_db() - - -@celery_app.task -def update_website_task() -> None: +def update_db_task(tweet_status: bool = False) -> None: from app.data.globals import website_options from app.data.processing.core import update_db - from app.twitter import tweet_current_status update_db() - if website_options.boating_season: + if tweet_status and website_options.boating_season: + from app.twitter import tweet_current_status + tweet_current_status() @@ -161,5 +155,4 @@ def send_database_exports_task() -> None: predict_v2_task: WithAppContextTask predict_v3_task: WithAppContextTask update_db_task: WithAppContextTask -update_website_task: WithAppContextTask send_database_exports_task: WithAppContextTask diff --git a/app/main.py b/app/main.py index f587c6a..1408711 100644 --- a/app/main.py +++ b/app/main.py @@ -87,6 +87,20 @@ def register_extensions(app: Flask): cors = CORS(resources={"/api/*": {"origins": "*"}, "/flags": {"origins": "*"}}) cors.init_app(app) + if app.config.get("SENTRY_DSN"): + import sentry_sdk + + sentry_sdk.init( + dsn=app.config["SENTRY_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, + ) + def register_blueprints(app: Flask): """Register the "blueprints." Blueprints are basically like mini web apps @@ -262,42 +276,26 @@ def _wrap(*args, **kwargs): help="If set, then run this command in Celery." " This can help save a bit of money on Heroku compute.", ) + @click.option( + "--tweet-status/--dont-tweet-status", + is_flag=True, + default=False, + help="If set, then send a tweet indicating the status of the flags.", + ) @with_appcontext @mail_on_fail - def update_db_command(async_: bool = False): + def update_db_command(async_: bool = False, tweet_status: bool = False): """Update the database with the latest live data.""" from app.data.celery import update_db_task if async_: - res = update_db_task.delay() + res = update_db_task.delay(tweet_status=tweet_status) click.echo(f"Started update database task ({res.id!r}).") else: click.echo("Updating the database...") - update_db_task.run() + update_db_task.run(tweet_status=tweet_status) click.echo("Updated the database successfully.") - @app.cli.command("update-website") - @click.option( - "--async", - "async_", - is_flag=True, - default=False, - help="If set, then run this command in Celery." - " This can help save a bit of money on Heroku compute.", - ) - @mail_on_fail - def update_website_command(async_: bool = False): - """Updates the database, then Tweets a message.""" - from app.data.celery import update_website_task - - if async_: - res = update_website_task.delay() - click.echo(f"Started update website task ({res.id!r}).") - else: - click.echo("Updating the website...") - update_website_task.run() - click.echo("Updated the website successfully.") - @app.cli.command("gen-mock-data") @dev_only def generate_mock_data(): diff --git a/requirements.in b/requirements.in index a01ee43..973e694 100644 --- a/requirements.in +++ b/requirements.in @@ -27,6 +27,7 @@ redis requests rich-click SQLAlchemy +sentry-sdk[flask] tenacity tweepy diff --git a/requirements.txt b/requirements.txt index 9d14304..cbf3c73 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ blinker==1.8.2 # via # flask # flask-mail + # sentry-sdk cachelib==0.9.0 # via flask-caching celery==5.4.0 @@ -33,6 +34,7 @@ certifi==2024.2.2 # httpcore # httpx # requests + # sentry-sdk cfgv==3.4.0 # via pre-commit charset-normalizer==3.3.2 @@ -84,6 +86,7 @@ flask==3.0.3 # flask-mail # flask-postgres # flask-sqlalchemy + # sentry-sdk flask-admin==1.6.1 # via -r requirements.in flask-basicauth==0.2.0 @@ -169,6 +172,7 @@ markupsafe==2.1.5 # via # jinja2 # mako + # sentry-sdk # werkzeug # wtforms mdurl==0.1.2 @@ -288,6 +292,8 @@ ruff==0.4.5 # via -r requirements.in schemathesis==3.28.1 # via -r requirements.in +sentry-sdk==2.6.0 + # via -r requirements.in setuptools==70.0.0 # via # flask-db @@ -343,6 +349,7 @@ urllib3==2.2.1 # via # docker # requests + # sentry-sdk uv==0.2.3 # via -r requirements.in vine==5.1.0