Skip to content

Commit

Permalink
Include framework in SDK name (#1662)
Browse files Browse the repository at this point in the history
* Made SDK name dynamic depending on modules loaded
  • Loading branch information
antonpirker authored Oct 10, 2022
1 parent c0ef3d0 commit a48fafd
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
19 changes: 18 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
current_stacktrace,
disable_capture_event,
format_timestamp,
get_sdk_name,
get_type_name,
get_default_release,
handle_in_app,
logger,
)
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,
VERSION,
ClientConstructor,
)
from sentry_sdk.integrations import setup_integrations
from sentry_sdk.utils import ContextVar
from sentry_sdk.sessions import SessionFlusher
Expand All @@ -41,6 +46,13 @@
_client_init_debug = ContextVar("client_init_debug")


SDK_INFO = {
"name": "sentry.python", # SDK name will be overridden after integrations have been loaded with sentry_sdk.integrations.setup_integrations()
"version": VERSION,
"packages": [{"name": "pypi:sentry-sdk", "version": VERSION}],
}


def _get_options(*args, **kwargs):
# type: (*Optional[str], **Any) -> Dict[str, Any]
if args and (isinstance(args[0], (text_type, bytes, str)) or args[0] is None):
Expand Down Expand Up @@ -128,6 +140,11 @@ def _capture_envelope(envelope):
"auto_enabling_integrations"
],
)

sdk_name = get_sdk_name(list(self.integrations.keys()))
SDK_INFO["name"] = sdk_name
logger.debug("Setting SDK name to '%s'", sdk_name)

finally:
_client_init_debug.set(old_debug)

Expand Down
5 changes: 0 additions & 5 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ def _get_default_options():


VERSION = "1.9.10"
SDK_INFO = {
"name": "sentry.python",
"version": VERSION,
"packages": [{"name": "pypi:sentry-sdk", "version": VERSION}],
}


class OP:
Expand Down
34 changes: 34 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,40 @@ def get_default_release():
return None


def get_sdk_name(installed_integrations):
# 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"


class CaptureInternalException(object):
__slots__ = ()

Expand Down
67 changes: 67 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
add_global_event_processor,
global_event_processors,
)
from sentry_sdk.utils import get_sdk_name


def test_processors(sentry_init, capture_events):
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

0 comments on commit a48fafd

Please sign in to comment.