Skip to content
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

Sentry: Add AsyncioIntegration, make all integrations disabled #2248

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 43 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 @@ -55,6 +56,7 @@ def includeme(config: Optional[pyramid.config.Configurator] = None) -> None:
"propagate_traces",
"auto_enabling_integrations",
"auto_session_tracking",
"enable_tracing",
):
if key in client_info:
client_info[key] = client_info[key].lower() in ("1", "t", "true")
Expand All @@ -73,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