Esempio n. 1
0
    def __init__(self, app: ASGIApp, api_key: str, **kwargs: Any):
        dispatch = kwargs.pop("dispatch", None)

        super().__init__(app, dispatch)

        self._client = Client(api_key=api_key, **kwargs)
        self._client.configuration.configure(
            traceback_exclude_modules=[sys.modules[__name__]])
        self._configure_packages_tab()
Esempio n. 2
0
class BugsnagMiddleware(BaseHTTPMiddleware):
    def __init__(self, app: ASGIApp, api_key: str, **kwargs: Any):
        dispatch = kwargs.pop("dispatch", None)

        super().__init__(app, dispatch)

        self._client = Client(api_key=api_key, **kwargs)
        self._client.configuration.configure(
            traceback_exclude_modules=[sys.modules[__name__]])
        self._configure_packages_tab()

    async def dispatch(self, request: Request,
                       call_next: RequestResponseEndpoint) -> Response:
        try:
            return await call_next(request)
        except Exception as exc:
            self._client.notify(
                exc,
                context=request.url,
                severity="error",
                scope=request.scope,
                locals=self._get_locals(exc),
            )

            raise exc

    @staticmethod
    def _get_packages() -> dict:
        return {
            dist.metadata["Name"]: dist.metadata["Version"]
            for dist in metadata.distributions()
        }

    @staticmethod
    def _get_locals(exception: Exception) -> dict:
        """Return local variables from exception last traceback frame."""

        tb = exception.__traceback__

        if not tb:  # pragma: no cover
            return {}

        while tb.tb_next is not None:
            tb = tb.tb_next

        return tb.tb_frame.f_locals

    def _configure_packages_tab(self) -> None:
        packages = self._get_packages()

        def add_packages_tab(notification: Notification) -> None:
            notification.add_tab("packages", packages)

        self._client.configuration.middleware.before_notify(add_packages_tab)
Esempio n. 3
0
import types

from bugsnag.configuration import RequestConfiguration
from bugsnag.client import Client

import bugsnag

default_client = Client()
configuration = default_client.configuration


def configure(**options):
    """
    Configure the Bugsnag notifier application-wide settings.
    """
    return configuration.configure(**options)


def configure_request(**options):
    """
    Configure the Bugsnag notifier per-request settings.
    """
    RequestConfiguration.get_instance().configure(**options)


def add_metadata_tab(tab_name, data):
    """
    Add metaData to the tab

    bugsnag.add_metadata_tab("user", {"id": "1", "name": "Conrad"})
    """