SILENT KILLERPanel

Current Path: > > opt > hc_python > > lib > python3.12 > > site-packages > > sentry_sdk > integrations


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: //opt/hc_python//lib/python3.12//site-packages//sentry_sdk/integrations

NameTypeSizeLast ModifiedActions
__pycache__ Directory - -
celery Directory - -
django Directory - -
grpc Directory - -
opentelemetry Directory - -
redis Directory - -
spark Directory - -
__init__.py File 10218 bytes May 23 2025 10:34:44.
_asgi_common.py File 3187 bytes May 23 2025 10:34:44.
_wsgi_common.py File 7558 bytes May 23 2025 10:34:44.
aiohttp.py File 12895 bytes May 23 2025 10:34:44.
anthropic.py File 9426 bytes May 23 2025 10:34:44.
argv.py File 911 bytes May 23 2025 10:34:44.
ariadne.py File 5834 bytes May 23 2025 10:34:44.
arq.py File 7857 bytes May 23 2025 10:34:44.
asgi.py File 12779 bytes May 23 2025 10:34:44.
asyncio.py File 4034 bytes May 23 2025 10:34:44.
asyncpg.py File 6521 bytes May 23 2025 10:34:44.
atexit.py File 1652 bytes May 23 2025 10:34:44.
aws_lambda.py File 17954 bytes May 23 2025 10:34:44.
beam.py File 5182 bytes May 23 2025 10:34:44.
boto3.py File 4411 bytes May 23 2025 10:34:44.
bottle.py File 6615 bytes May 23 2025 10:34:44.
chalice.py File 4699 bytes May 23 2025 10:34:44.
clickhouse_driver.py File 5247 bytes May 23 2025 10:34:44.
cloud_resource_context.py File 7780 bytes May 23 2025 10:34:44.
cohere.py File 9333 bytes May 23 2025 10:34:44.
dedupe.py File 1418 bytes May 23 2025 10:34:44.
dramatiq.py File 5583 bytes May 23 2025 10:34:44.
excepthook.py File 2408 bytes May 23 2025 10:34:44.
executing.py File 1994 bytes May 23 2025 10:34:44.
falcon.py File 9501 bytes May 23 2025 10:34:44.
fastapi.py File 4718 bytes May 23 2025 10:34:44.
flask.py File 8740 bytes May 23 2025 10:34:44.
gcp.py File 8274 bytes May 23 2025 10:34:44.
gnu_backtrace.py File 2894 bytes May 23 2025 10:34:44.
gql.py File 4179 bytes May 23 2025 10:34:44.
graphene.py File 5042 bytes May 23 2025 10:34:44.
httpx.py File 5866 bytes May 23 2025 10:34:44.
huey.py File 5443 bytes May 23 2025 10:34:44.
huggingface_hub.py File 6551 bytes May 23 2025 10:34:44.
langchain.py File 17718 bytes May 23 2025 10:34:44.
launchdarkly.py File 1935 bytes May 23 2025 10:34:44.
litestar.py File 11569 bytes May 23 2025 10:34:44.
logging.py File 13506 bytes May 23 2025 10:34:44.
loguru.py File 3620 bytes May 23 2025 10:34:44.
modules.py File 820 bytes May 23 2025 10:34:44.
openai.py File 15585 bytes May 23 2025 10:34:44.
openfeature.py File 1235 bytes May 23 2025 10:34:44.
pure_eval.py File 4581 bytes May 23 2025 10:34:44.
pymongo.py File 6380 bytes May 23 2025 10:34:44.
pyramid.py File 7364 bytes May 23 2025 10:34:44.
quart.py File 7437 bytes May 23 2025 10:34:44.
ray.py File 4162 bytes May 23 2025 10:34:44.
rq.py File 5307 bytes May 23 2025 10:34:44.
rust_tracing.py File 9078 bytes May 23 2025 10:34:44.
sanic.py File 12960 bytes May 23 2025 10:34:44.
serverless.py File 1804 bytes May 23 2025 10:34:44.
socket.py File 3169 bytes May 23 2025 10:34:44.
sqlalchemy.py File 4372 bytes May 23 2025 10:34:44.
starlette.py File 26413 bytes May 23 2025 10:34:44.
starlite.py File 10620 bytes May 23 2025 10:34:44.
statsig.py File 1227 bytes May 23 2025 10:34:44.
stdlib.py File 8831 bytes May 23 2025 10:34:44.
strawberry.py File 14126 bytes May 23 2025 10:34:44.
sys_exit.py File 2493 bytes May 23 2025 10:34:44.
threading.py File 5392 bytes May 23 2025 10:34:44.
tornado.py File 7222 bytes May 23 2025 10:34:44.
trytond.py File 1651 bytes May 23 2025 10:34:44.
typer.py File 1815 bytes May 23 2025 10:34:44.
unleash.py File 1058 bytes May 23 2025 10:34:44.
wsgi.py File 10747 bytes May 23 2025 10:34:44.

Reading File: //opt/hc_python//lib/python3.12//site-packages//sentry_sdk/integrations/asgi.py

"""
An ASGI middleware.

Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
"""

import asyncio
import inspect
from copy import deepcopy
from functools import partial

import sentry_sdk
from sentry_sdk.api import continue_trace
from sentry_sdk.consts import OP

from sentry_sdk.integrations._asgi_common import (
    _get_headers,
    _get_request_data,
    _get_url,
)
from sentry_sdk.integrations._wsgi_common import (
    DEFAULT_HTTP_METHODS_TO_CAPTURE,
    nullcontext,
)
from sentry_sdk.sessions import track_session
from sentry_sdk.tracing import (
    SOURCE_FOR_STYLE,
    TransactionSource,
)
from sentry_sdk.utils import (
    ContextVar,
    event_from_exception,
    HAS_REAL_CONTEXTVARS,
    CONTEXTVARS_ERROR_MESSAGE,
    logger,
    transaction_from_function,
    _get_installed_modules,
)
from sentry_sdk.tracing import Transaction

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import Any
    from typing import Callable
    from typing import Dict
    from typing import Optional
    from typing import Tuple

    from sentry_sdk._types import Event, Hint


_asgi_middleware_applied = ContextVar("sentry_asgi_middleware_applied")

_DEFAULT_TRANSACTION_NAME = "generic ASGI request"

TRANSACTION_STYLE_VALUES = ("endpoint", "url")


def _capture_exception(exc, mechanism_type="asgi"):
    # type: (Any, str) -> None

    event, hint = event_from_exception(
        exc,
        client_options=sentry_sdk.get_client().options,
        mechanism={"type": mechanism_type, "handled": False},
    )
    sentry_sdk.capture_event(event, hint=hint)


def _looks_like_asgi3(app):
    # type: (Any) -> bool
    """
    Try to figure out if an application object supports ASGI3.

    This is how uvicorn figures out the application version as well.
    """
    if inspect.isclass(app):
        return hasattr(app, "__await__")
    elif inspect.isfunction(app):
        return asyncio.iscoroutinefunction(app)
    else:
        call = getattr(app, "__call__", None)  # noqa
        return asyncio.iscoroutinefunction(call)


class SentryAsgiMiddleware:
    __slots__ = (
        "app",
        "__call__",
        "transaction_style",
        "mechanism_type",
        "span_origin",
        "http_methods_to_capture",
    )

    def __init__(
        self,
        app,  # type: Any
        unsafe_context_data=False,  # type: bool
        transaction_style="endpoint",  # type: str
        mechanism_type="asgi",  # type: str
        span_origin="manual",  # type: str
        http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE,  # type: Tuple[str, ...]
    ):
        # type: (...) -> None
        """
        Instrument an ASGI application with Sentry. Provides HTTP/websocket
        data to sent events and basic handling for exceptions bubbling up
        through the middleware.

        :param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default.
        """
        if not unsafe_context_data and not HAS_REAL_CONTEXTVARS:
            # We better have contextvars or we're going to leak state between
            # requests.
            raise RuntimeError(
                "The ASGI middleware for Sentry requires Python 3.7+ "
                "or the aiocontextvars package." + CONTEXTVARS_ERROR_MESSAGE
            )
        if transaction_style not in TRANSACTION_STYLE_VALUES:
            raise ValueError(
                "Invalid value for transaction_style: %s (must be in %s)"
                % (transaction_style, TRANSACTION_STYLE_VALUES)
            )

        asgi_middleware_while_using_starlette_or_fastapi = (
            mechanism_type == "asgi" and "starlette" in _get_installed_modules()
        )
        if asgi_middleware_while_using_starlette_or_fastapi:
            logger.warning(
                "The Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. "
                "Please remove 'SentryAsgiMiddleware' from your project. "
                "See https://docs.sentry.io/platforms/python/guides/asgi/ for more information."
            )

        self.transaction_style = transaction_style
        self.mechanism_type = mechanism_type
        self.span_origin = span_origin
        self.app = app
        self.http_methods_to_capture = http_methods_to_capture

        if _looks_like_asgi3(app):
            self.__call__ = self._run_asgi3  # type: Callable[..., Any]
        else:
            self.__call__ = self._run_asgi2

    def _run_asgi2(self, scope):
        # type: (Any) -> Any
        async def inner(receive, send):
            # type: (Any, Any) -> Any
            return await self._run_app(scope, receive, send, asgi_version=2)

        return inner

    async def _run_asgi3(self, scope, receive, send):
        # type: (Any, Any, Any) -> Any
        return await self._run_app(scope, receive, send, asgi_version=3)

    async def _run_app(self, scope, receive, send, asgi_version):
        # type: (Any, Any, Any, Any, int) -> Any
        is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
        is_lifespan = scope["type"] == "lifespan"
        if is_recursive_asgi_middleware or is_lifespan:
            try:
                if asgi_version == 2:
                    return await self.app(scope)(receive, send)
                else:
                    return await self.app(scope, receive, send)

            except Exception as exc:
                _capture_exception(exc, mechanism_type=self.mechanism_type)
                raise exc from None

        _asgi_middleware_applied.set(True)
        try:
            with sentry_sdk.isolation_scope() as sentry_scope:
                with track_session(sentry_scope, session_mode="request"):
                    sentry_scope.clear_breadcrumbs()
                    sentry_scope._name = "asgi"
                    processor = partial(self.event_processor, asgi_scope=scope)
                    sentry_scope.add_event_processor(processor)

                    ty = scope["type"]
                    (
                        transaction_name,
                        transaction_source,
                    ) = self._get_transaction_name_and_source(
                        self.transaction_style,
                        scope,
                    )

                    method = scope.get("method", "").upper()
                    transaction = None
                    if ty in ("http", "websocket"):
                        if ty == "websocket" or method in self.http_methods_to_capture:
                            transaction = continue_trace(
                                _get_headers(scope),
                                op="{}.server".format(ty),
                                name=transaction_name,
                                source=transaction_source,
                                origin=self.span_origin,
                            )
                            logger.debug(
                                "[ASGI] Created transaction (continuing trace): %s",
                                transaction,
                            )
                    else:
                        transaction = Transaction(
                            op=OP.HTTP_SERVER,
                            name=transaction_name,
                            source=transaction_source,
                            origin=self.span_origin,
                        )
                        logger.debug(
                            "[ASGI] Created transaction (new): %s", transaction
                        )

                    if transaction:
                        transaction.set_tag("asgi.type", ty)
                        logger.debug(
                            "[ASGI] Set transaction name and source on transaction: '%s' / '%s'",
                            transaction.name,
                            transaction.source,
                        )

                    with (
                        sentry_sdk.start_transaction(
                            transaction,
                            custom_sampling_context={"asgi_scope": scope},
                        )
                        if transaction is not None
                        else nullcontext()
                    ):
                        logger.debug("[ASGI] Started transaction: %s", transaction)
                        try:

                            async def _sentry_wrapped_send(event):
                                # type: (Dict[str, Any]) -> Any
                                if transaction is not None:
                                    is_http_response = (
                                        event.get("type") == "http.response.start"
                                        and "status" in event
                                    )
                                    if is_http_response:
                                        transaction.set_http_status(event["status"])

                                return await send(event)

                            if asgi_version == 2:
                                return await self.app(scope)(
                                    receive, _sentry_wrapped_send
                                )
                            else:
                                return await self.app(
                                    scope, receive, _sentry_wrapped_send
                                )
                        except Exception as exc:
                            _capture_exception(exc, mechanism_type=self.mechanism_type)
                            raise exc from None
        finally:
            _asgi_middleware_applied.set(False)

    def event_processor(self, event, hint, asgi_scope):
        # type: (Event, Hint, Any) -> Optional[Event]
        request_data = event.get("request", {})
        request_data.update(_get_request_data(asgi_scope))
        event["request"] = deepcopy(request_data)

        # Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
        transaction = event.get("transaction")
        transaction_source = (event.get("transaction_info") or {}).get("source")
        already_set = (
            transaction is not None
            and transaction != _DEFAULT_TRANSACTION_NAME
            and transaction_source
            in [
                TransactionSource.COMPONENT,
                TransactionSource.ROUTE,
                TransactionSource.CUSTOM,
            ]
        )
        if not already_set:
            name, source = self._get_transaction_name_and_source(
                self.transaction_style, asgi_scope
            )
            event["transaction"] = name
            event["transaction_info"] = {"source": source}

            logger.debug(
                "[ASGI] Set transaction name and source in event_processor: '%s' / '%s'",
                event["transaction"],
                event["transaction_info"]["source"],
            )

        return event

    # Helper functions.
    #
    # Note: Those functions are not public API. If you want to mutate request
    # data to your liking it's recommended to use the `before_send` callback
    # for that.

    def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
        # type: (SentryAsgiMiddleware, str, Any) -> Tuple[str, str]
        name = None
        source = SOURCE_FOR_STYLE[transaction_style]
        ty = asgi_scope.get("type")

        if transaction_style == "endpoint":
            endpoint = asgi_scope.get("endpoint")
            # Webframeworks like Starlette mutate the ASGI env once routing is
            # done, which is sometime after the request has started. If we have
            # an endpoint, overwrite our generic transaction name.
            if endpoint:
                name = transaction_from_function(endpoint) or ""
            else:
                name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
                source = TransactionSource.URL

        elif transaction_style == "url":
            # FastAPI includes the route object in the scope to let Sentry extract the
            # path from it for the transaction name
            route = asgi_scope.get("route")
            if route:
                path = getattr(route, "path", None)
                if path is not None:
                    name = path
            else:
                name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
                source = TransactionSource.URL

        if name is None:
            name = _DEFAULT_TRANSACTION_NAME
            source = TransactionSource.ROUTE
            return name, source

        return name, source

SILENT KILLER Tool