|
| 1 | +from sentry_sdk.hub import Hub, _should_send_default_pii |
| 2 | +from sentry_sdk.integrations import DidNotEnable, Integration |
| 3 | +from sentry_sdk.utils import parse_version |
| 4 | +from sentry_sdk._types import TYPE_CHECKING |
| 5 | + |
| 6 | +try: |
| 7 | + from graphql import version as GRAPHQL_CORE_VERSION |
| 8 | +except ImportError: |
| 9 | + raise DidNotEnable("graphql-core not installed") |
| 10 | + |
| 11 | + |
| 12 | +try: |
| 13 | + from graphql import schema as graphene_schema |
| 14 | +except ImportError: |
| 15 | + raise DidNotEnable("graphene not installed") |
| 16 | + |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from typing import Any, Union |
| 20 | + from graphene.language.source import Source |
| 21 | + from graphql.execution import ExecutionResult |
| 22 | + from graphql.type import GraphQLSchema |
| 23 | + from sentry_sdk._types import EventProcessor |
| 24 | + |
| 25 | + |
| 26 | +class GrapheneIntegration(Integration): |
| 27 | + identifier = "graphene" |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def setup_once(): |
| 31 | + # type: () -> None |
| 32 | + # XXX version guard for graphene |
| 33 | + version = parse_version(GRAPHQL_CORE_VERSION) |
| 34 | + |
| 35 | + if version is None: |
| 36 | + raise DidNotEnable( |
| 37 | + "Unparsable graphql-core version: {}".format(GRAPHQL_CORE_VERSION) |
| 38 | + ) |
| 39 | + |
| 40 | + if version < (3, 2): |
| 41 | + raise DidNotEnable("graphql-core 3.2 or newer required.") |
| 42 | + |
| 43 | + # XXX: a guard against patching multiple times? |
| 44 | + old_execute_sync = graphene_schema.graphql_sync |
| 45 | + old_execute_async = graphene_schema.graphql |
| 46 | + |
| 47 | + def _sentry_patched_graphql_sync(schema, source, *args, **kwargs): |
| 48 | + # type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult |
| 49 | + hub = Hub.current |
| 50 | + integration = hub.get_integration(GrapheneIntegration) |
| 51 | + if integration is None or hub.client is None: |
| 52 | + return old_execute_sync(schema, source, *args, **kwargs) |
| 53 | + |
| 54 | + scope = hub.scope # XXX |
| 55 | + if scope is not None: |
| 56 | + event_processor = _make_event_processor(source) |
| 57 | + scope.add_event_processor(event_processor) |
| 58 | + |
| 59 | + result = old_execute_sync(schema, source, *args, **kwargs) |
| 60 | + |
| 61 | + _raise_graphql_errors(result) |
| 62 | + |
| 63 | + return result |
| 64 | + |
| 65 | + async def _sentry_patched_graphql_async(schema, source, *args, **kwargs): |
| 66 | + # type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult |
| 67 | + hub = Hub.current |
| 68 | + integration = hub.get_integration(GrapheneIntegration) |
| 69 | + if integration is None or hub.client is None: |
| 70 | + return await old_execute_async(schema, source, *args, **kwargs) |
| 71 | + |
| 72 | + scope = hub.scope # XXX |
| 73 | + if scope is not None: |
| 74 | + event_processor = _make_event_processor(source) |
| 75 | + scope.add_event_processor(event_processor) |
| 76 | + |
| 77 | + result = await old_execute_async(schema, source, *args, **kwargs) |
| 78 | + |
| 79 | + _raise_graphql_errors(result) |
| 80 | + |
| 81 | + return result |
| 82 | + |
| 83 | + graphene_schema.graphql_sync = _sentry_patched_graphql_sync |
| 84 | + graphene_schema.graphql = _sentry_patched_graphql_async |
| 85 | + |
| 86 | + |
| 87 | +def _raise_graphql_errors(result, hub): |
| 88 | + # type: (ExecutionResult, Hub) -> None |
| 89 | + for error in result.errors or []: |
| 90 | + hub.capture_exception(error) |
| 91 | + |
| 92 | + |
| 93 | +def _make_event_processor(source): |
| 94 | + # type: (Source) -> EventProcessor |
| 95 | + def inner(event, hint): |
| 96 | + if _should_send_default_pii(): |
| 97 | + request_info = event.setdefault("request", {}) |
| 98 | + request_info["data"] = str(source) |
| 99 | + |
| 100 | + return event |
| 101 | + |
| 102 | + return inner |
0 commit comments