Skip to content

Commit

Permalink
feat: allow specifying any Sentry option
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Oct 4, 2022
1 parent fd9541b commit 3325c22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/user_guide/sentry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ To setup Errbot with Sentry:
* Set **BOT_LOG_SENTRY** to *True* and fill in **SENTRY_DSN** with the DSN value obtained previously
* Optionally adjust **SENTRY_LOGLEVEL** to the desired level
* Optionally adjust **SENTRY_TRANSPORT** to the desired transport
* Optionally adjust **SENTRY_OPTIONS** to customise the rest of the initialization.
* Restart Errbot

You can find a list of `Sentry transport classes <https://docs.sentry.io/clients/python/transports/>`_.
You can find a list of `Sentry options <https://docs.sentry.io/platforms/python/configuration/options/>`_.

You should now see Exceptions and log messages show up in your Sentry stream.
32 changes: 16 additions & 16 deletions errbot/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ def setup_bot(

sentry_integrations.append(FlaskIntegration())

try:
if hasattr(config, "SENTRY_TRANSPORT") and isinstance(
config.SENTRY_TRANSPORT, tuple
):
sentry_options = getattr(config, "SENTRY_OPTIONS", {})
if hasattr(config, "SENTRY_TRANSPORT") and isinstance(
config.SENTRY_TRANSPORT, tuple
):
try:
mod = importlib.import_module(config.SENTRY_TRANSPORT[1])
transport = getattr(mod, config.SENTRY_TRANSPORT[0])

sentry_sdk.init(
dsn=config.SENTRY_DSN,
integrations=sentry_integrations,
transport=transport,
sentry_options["transport"] = transport
except ImportError:
log.exception(
f"Unable to import selected SENTRY_TRANSPORT - {config.SENTRY_TRANSPORT}"
)
else:
sentry_sdk.init(dsn=config.SENTRY_DSN, integrations=sentry_integrations)
except ImportError:
log.exception(
f"Unable to import selected SENTRY_TRANSPORT - {config.SENTRY_TRANSPORT}"
)
exit(-1)
exit(-1)
# merge options dict with dedicated SENTRY_DSN setting
sentry_kwargs = {
**sentry_options,
**{"dsn": config.SENTRY_DSN, "integrations": sentry_integrations},
}
sentry_sdk.init(**sentry_kwargs)

logger.setLevel(config.BOT_LOG_LEVEL)

Expand Down
6 changes: 6 additions & 0 deletions errbot/config-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@
# For more info, see https://docs.sentry.io/error-reporting/configuration/?platform=python#transport-options
# SENTRY_TRANSPORT = ("RequestsHTTPTransport", "raven.transport.requests")

# Any other options that can be passed to sentry_sdk.init() at initialization time
# Note that dsn and transport should be specified via their dedicated setting,
# and that the 'integrations' setting cannot be set
# e.g: SENTRY_OPTIONS = {"environment": "production"}
SENTRY_OPTIONS = {}

# Execute commands in asynchronous mode. In this mode, Errbot will spawn 10
# separate threads to handle commands, instead of blocking on each
# single command.
Expand Down

0 comments on commit 3325c22

Please sign in to comment.