Skip to content

Commit

Permalink
Sentry: Add AsyncioIntegration, make all integrations disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed May 14, 2024
1 parent af44398 commit 80faaea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,12 @@ A few other environment variables can be used to tune the info sent with each re
- `SENTRY_CLIENT_IGNORE_EXCEPTIONS`: list (coma separated) of exceptions to ignore (defaults to SystemExit)
- `SENTRY_TAG_...`: to add other custom tags
- `SENTRY_LEVEL`: starting from what logging level to send events to Sentry (defaults to ERROR)
- `SENTRY_TRACES_SAMPLE_RATE`: The percentage of events to send to sentry in order to compute the performance. Value between 0 and 1, default is 0.
- `SENTRY_TRACES_SAMPLE_RATE`: The percentage of events to send to sentry in order to compute the performance. Value between 0 and 1 (default is 0)
- `SENTRY_INTEGRATION_LOGGING`: If set to 0, the Sentry integration will not log anything (default is 1)
- `SENTRY_INTEGRATION_PYRAMID`: If set to 0, the Sentry integration with Pyramid will not be enabled (default is 1)
- `SENTRY_INTEGRATION_SQLALCHEMY`: If set to 0, the Sentry integration with SQLAlchemy will not be enabled (default is 1)
- `SENTRY_INTEGRATION_REDIS`: If set to 0, the Sentry integration with Redis will not be enabled (default is 1)
- `SENTRY_INTEGRATION_ASYNCIO`: If set to 0, the Sentry integration with asyncio will not be enabled (default is 1)

# Developer info

Expand Down
50 changes: 42 additions & 8 deletions c2cwsgiutils/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import Any, Callable, Optional

import pyramid.config
import sentry_sdk
import sentry_sdk.integrations
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger
from sentry_sdk.integrations.pyramid import PyramidIntegration
from sentry_sdk.integrations.redis import RedisIntegration
Expand Down Expand Up @@ -74,20 +75,53 @@ def includeme(config: Optional[pyramid.config.Configurator] = None) -> None:
client_info["ignore_errors"] = client_info.pop("ignore_exceptions", "SystemExit").split(",")
tags = {key[11:].lower(): value for key, value in os.environ.items() if key.startswith("SENTRY_TAG_")}

sentry_logging = LoggingIntegration(
level=logging.DEBUG,
event_level=config_utils.env_or_config(
config, "SENTRY_LEVEL", "c2c.sentry_level", "ERROR"
).upper(),
)
traces_sample_rate = float(
config_utils.env_or_config(
config, "SENTRY_TRACES_SAMPLE_RATE", "c2c.sentry_traces_sample_rate", "0.0"
)
)
integrations: list[sentry_sdk.integrations.Integration] = []
if config_utils.config_bool(
config_utils.env_or_config(
config, "SENTRY_INTEGRATION_LOGGING", "c2c.sentry_integration_logging", "true"
)
):
integrations.append(
LoggingIntegration(
level=logging.DEBUG,
event_level=config_utils.env_or_config(
config, "SENTRY_LEVEL", "c2c.sentry_level", "ERROR"
).upper(),
)
)
if config_utils.config_bool(
config_utils.env_or_config(
config, "SENTRY_INTEGRATION_PYRAMID", "c2c.sentry_integration_pyramid", "true"
)
):
integrations.append(PyramidIntegration())
if config_utils.config_bool(
config_utils.env_or_config(
config, "SENTRY_INTEGRATION_SQLALCHEMY", "c2c.sentry_integration_sqlalchemy", "true"
)
):
integrations.append(SqlalchemyIntegration())
if config_utils.config_bool(
config_utils.env_or_config(
config, "SENTRY_INTEGRATION_REDIS", "c2c.sentry_integration_redis", "true"
)
):
integrations.append(RedisIntegration())
if config_utils.config_bool(
config_utils.env_or_config(
config, "SENTRY_INTEGRATION_ASYNCIO", "c2c.sentry_integration_asyncio", "true"
)
):
integrations.append(AsyncioIntegration())

sentry_sdk.init(
dsn=sentry_url,
integrations=[sentry_logging, PyramidIntegration(), SqlalchemyIntegration(), RedisIntegration()],
integrations=integrations,
traces_sample_rate=traces_sample_rate,
before_send=_create_before_send_filter(tags),
**client_info,
Expand Down

0 comments on commit 80faaea

Please sign in to comment.