Skip to content

Commit

Permalink
Add Sentry support and logging controls. Bump version to 0.3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdknigga committed Jul 18, 2024
1 parent 8029966 commit 92c2119
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
RFTB_ENVIRONMENT="development"
RFTB_PRAW_CLIENT_ID=""
RFTB_PRAW_CLIENT_SECRET=""
RFTB_PRAW_USERNAME=""
RFTB_PRAW_PASSWORD=""
RFTB_SUBREDDIT=""
RFTB_DB_CONNECTION_STRING="sqlite+aiosqlite:///:memory:"
RFTB_LOG_LEVEL="debug"
RFTB_SENTRY_DSN=""
RFTB_SENTRY_TRACE_RATE="0.00001"
RFTB_SENTRY_PROFILE_RATE="0.00001"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ $ export RFTB_PRAW_CLIENT_SECRET="your_client_secret_here"
$ export RFTB_PRAW_USERNAME="your_reddit_username_here"
$ export RFTB_PRAW_PASSWORD="your_reddit_password_here"
$ export RFTB_SUBREDDIT="the_name_of_a_subreddit_here"
$ export RFTB_DB_CONNECTION_STRING="sqlite+aiosqlite:///:memory:" # Optional. It keeps track of what actions have been done so they don't accidently get repeated.
$ export RFTB_LOG_LEVEL="info" # Optional. Can be debug, info, warning, error, or critical.
$ export RFTB_SENTRY_DSN="your_sentry_dsn" # Optional. Enables Sentry logging
$ export RFTB_SENTRY_TRACE_RATE="0.1" # Optional.
$ export RFTB_SENTRY_PROFILE_RATE="0.1" # Optional.
$ export RFTB_ENVIRONMENT="development" # Optional. This is for Sentry, mostly, currently
```

or, put the secrets in a file called .env (see .env.example)
Expand Down
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rflying_tower_bot"
version = "0.2.1"
version = "0.3.0"
description = ""
authors = ["Kris Knigga <[email protected]>"]

Expand All @@ -12,6 +12,7 @@ aiohttp = "^3.9.5"
pydantic = "^2.8.2"
python-dotenv = "^1.0.1"
sqlalchemy = {extras = ["asyncio"], version = "^2.0.31"}
sentry-sdk = "^2.10.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.7.1"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ python-dotenv==1.0.1 ; python_version >= "3.12" and python_version < "4.0"
requests==2.32.3 ; python_version >= "3.12" and python_version < "4.0"
ruamel-yaml-clib==0.2.8 ; platform_python_implementation == "CPython" and python_version < "3.13" and python_version >= "3.12"
ruamel-yaml==0.18.6 ; python_version >= "3.12" and python_version < "4.0"
sentry-sdk==2.10.0 ; python_version >= "3.12" and python_version < "4.0"
sqlalchemy[asyncio]==2.0.31 ; python_version >= "3.12" and python_version < "4.0"
typing-extensions==4.12.2 ; python_version >= "3.12" and python_version < "4.0"
update-checker==0.18.0 ; python_version >= "3.12" and python_version < "4.0"
Expand Down
2 changes: 1 addition & 1 deletion rflying_tower_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""rflying_tower_bot package."""

__version__ = "0.2.1"
__version__ = "0.3.0"
4 changes: 0 additions & 4 deletions rflying_tower_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
from rflying_tower_bot.modlog import ModLog
from rflying_tower_bot.post_stream import PostStream

logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)

log: logging.Logger = logging.getLogger(f"{__name__}")

praw_config = PRAWConfig()
Expand Down
27 changes: 27 additions & 0 deletions rflying_tower_bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os

import sentry_sdk
from asyncpraw import Reddit
from asyncpraw.models import Subreddit
from asyncpraw.models.reddit.removal_reasons import RemovalReason
Expand All @@ -22,6 +23,32 @@

load_dotenv()

log_level_map: dict[str, int] = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL,
}

log_level = os.getenv("RFTB_LOG_LEVEL", "info")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=log_level_map.get(log_level, logging.INFO),
)

sentry_dsn = os.getenv("RFTB_SENTRY_DSN")
if sentry_dsn is not None:
sentry_sdk.init(
dsn=sentry_dsn,
release=bot_version,
environment=os.getenv("RFTB_ENVIRONMENT", "development"),
debug=log_level == "debug",
enable_tracing=True,
traces_sample_rate=float(os.getenv("RFTB_SENTRY_TRACE_RATE", 0.00001)),
profiles_sample_rate=float(os.getenv("RFTB_SENTRY_PROFILE_RATE", 0.00001)),
)


class BotConfig:
"""General configuration for the bot, as well as a shared asyncpraw.Reddit instance."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rflying_tower_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

def test_version():
"""Test the version of the rflying_tower_bot package."""
assert __version__ == "0.2.0"
assert __version__ == "0.3.0"

0 comments on commit 92c2119

Please sign in to comment.