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

Include framework in SDK name #1662

Merged
merged 10 commits into from
Oct 10, 2022
12 changes: 11 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
)
from sentry_sdk.serializer import serialize
from sentry_sdk.transport import make_transport
from sentry_sdk.consts import DEFAULT_OPTIONS, SDK_INFO, ClientConstructor
from sentry_sdk.consts import (
DEFAULT_OPTIONS,
SDK_INFO,
ClientConstructor,
_get_sdk_name,
)
from sentry_sdk.integrations import setup_integrations
from sentry_sdk.utils import ContextVar
from sentry_sdk.sessions import SessionFlusher
Expand Down Expand Up @@ -128,6 +133,11 @@ def _capture_envelope(envelope):
"auto_enabling_integrations"
],
)

sdk_name = _get_sdk_name(list(self.integrations.keys()))
SDK_INFO["name"] = sdk_name
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Setting SDK name to '%s'", sdk_name)

finally:
_client_init_debug.set(old_debug)

Expand Down
36 changes: 35 additions & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,43 @@ def _get_default_options():
del _get_default_options


def _get_sdk_name(installed_integrations):
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
# type: (List[str]) -> str
"""Return the SDK name including the name of the used web framework."""

# Note: I can not use for example sentry_sdk.integrations.django.DjangoIntegration.identifier
# here because if django is not installed the integration is not accessible.
framework_integrations = [
"django",
"flask",
"fastapi",
"bottle",
"falcon",
"quart",
"sanic",
"starlette",
"chalice",
"serverless",
"pyramid",
"tornado",
"aiohttp",
"aws_lambda",
"gcp",
"beam",
"asgi",
"wsgi",
]

for integration in framework_integrations:
if integration in installed_integrations:
return "sentry.python.{}".format(integration)

return "sentry.python"


VERSION = "1.9.10"
SDK_INFO = {
"name": "sentry.python",
"name": "sentry.python", # SDK name be overridden after integrations have been loaded with sentry_sdk.integrations.setup_integrations()
"version": VERSION,
"packages": [{"name": "pypi:sentry-sdk", "version": VERSION}],
}
67 changes: 67 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)

from sentry_sdk._compat import reraise
from sentry_sdk.consts import _get_sdk_name
from sentry_sdk.integrations import _AUTO_ENABLING_INTEGRATIONS
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.scope import ( # noqa: F401
Expand Down Expand Up @@ -437,3 +438,69 @@ def foo(event, hint):
assert reports == [("event_processor", "error"), ("event_processor", "transaction")]

global_event_processors.pop()


@pytest.mark.parametrize(
"installed_integrations, expected_name",
[
# integrations with own name
(["django"], "sentry.python.django"),
(["flask"], "sentry.python.flask"),
(["fastapi"], "sentry.python.fastapi"),
(["bottle"], "sentry.python.bottle"),
(["falcon"], "sentry.python.falcon"),
(["quart"], "sentry.python.quart"),
(["sanic"], "sentry.python.sanic"),
(["starlette"], "sentry.python.starlette"),
(["chalice"], "sentry.python.chalice"),
(["serverless"], "sentry.python.serverless"),
(["pyramid"], "sentry.python.pyramid"),
(["tornado"], "sentry.python.tornado"),
(["aiohttp"], "sentry.python.aiohttp"),
(["aws_lambda"], "sentry.python.aws_lambda"),
(["gcp"], "sentry.python.gcp"),
(["beam"], "sentry.python.beam"),
(["asgi"], "sentry.python.asgi"),
(["wsgi"], "sentry.python.wsgi"),
# integrations without name
(["argv"], "sentry.python"),
(["atexit"], "sentry.python"),
(["boto3"], "sentry.python"),
(["celery"], "sentry.python"),
(["dedupe"], "sentry.python"),
(["excepthook"], "sentry.python"),
(["executing"], "sentry.python"),
(["modules"], "sentry.python"),
(["pure_eval"], "sentry.python"),
(["redis"], "sentry.python"),
(["rq"], "sentry.python"),
(["sqlalchemy"], "sentry.python"),
(["stdlib"], "sentry.python"),
(["threading"], "sentry.python"),
(["trytond"], "sentry.python"),
(["logging"], "sentry.python"),
(["gnu_backtrace"], "sentry.python"),
(["httpx"], "sentry.python"),
# precedence of frameworks
(["flask", "django", "celery"], "sentry.python.django"),
(["fastapi", "flask", "redis"], "sentry.python.flask"),
(["bottle", "fastapi", "httpx"], "sentry.python.fastapi"),
(["falcon", "bottle", "logging"], "sentry.python.bottle"),
(["quart", "falcon", "gnu_backtrace"], "sentry.python.falcon"),
(["sanic", "quart", "sqlalchemy"], "sentry.python.quart"),
(["starlette", "sanic", "rq"], "sentry.python.sanic"),
(["chalice", "starlette", "modules"], "sentry.python.starlette"),
(["serverless", "chalice", "pure_eval"], "sentry.python.chalice"),
(["pyramid", "serverless", "modules"], "sentry.python.serverless"),
(["tornado", "pyramid", "executing"], "sentry.python.pyramid"),
(["aiohttp", "tornado", "dedupe"], "sentry.python.tornado"),
(["aws_lambda", "aiohttp", "boto3"], "sentry.python.aiohttp"),
(["gcp", "aws_lambda", "atexit"], "sentry.python.aws_lambda"),
(["beam", "gcp", "argv"], "sentry.python.gcp"),
(["asgi", "beam", "stdtlib"], "sentry.python.beam"),
(["wsgi", "asgi", "boto3"], "sentry.python.asgi"),
(["wsgi", "celery", "redis"], "sentry.python.wsgi"),
],
)
def test_get_sdk_name(installed_integrations, expected_name):
assert _get_sdk_name(installed_integrations) == expected_name