|
| 1 | +from sentry_sdk.utils import event_from_exception, parse_version |
| 2 | +from sentry_sdk.hub import Hub, _should_send_default_pii |
| 3 | +from sentry_sdk.integrations import DidNotEnable, Integration |
| 4 | + |
| 5 | +try: |
| 6 | + import gql # type: ignore[import] |
| 7 | + from graphql import print_ast, get_operation_ast, DocumentNode, VariableDefinitionNode # type: ignore[import] |
| 8 | + from gql.transport import Transport, AsyncTransport # type: ignore[import] |
| 9 | + from gql.transport.exceptions import TransportQueryError # type: ignore[import] |
| 10 | +except ImportError: |
| 11 | + raise DidNotEnable("gql is not installed") |
| 12 | + |
| 13 | +from sentry_sdk._types import TYPE_CHECKING |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from typing import Any, Dict, Tuple, Union |
| 17 | + from sentry_sdk._types import EventProcessor |
| 18 | + |
| 19 | + EventDataType = Dict[str, Union[str, Tuple[VariableDefinitionNode, ...]]] |
| 20 | + |
| 21 | +MIN_GQL_VERSION = (3, 4, 1) |
| 22 | + |
| 23 | + |
| 24 | +class GQLIntegration(Integration): |
| 25 | + identifier = "gql" |
| 26 | + |
| 27 | + @staticmethod |
| 28 | + def setup_once(): |
| 29 | + # type: () -> None |
| 30 | + gql_version = parse_version(gql.__version__) |
| 31 | + if gql_version is None or gql_version < MIN_GQL_VERSION: |
| 32 | + raise DidNotEnable( |
| 33 | + "GQLIntegration is only supported for GQL versions %s and above." |
| 34 | + % ".".join(str(num) for num in MIN_GQL_VERSION) |
| 35 | + ) |
| 36 | + _patch_execute() |
| 37 | + |
| 38 | + |
| 39 | +def _data_from_document(document): |
| 40 | + # type: (DocumentNode) -> EventDataType |
| 41 | + try: |
| 42 | + operation_ast = get_operation_ast(document) |
| 43 | + data = {"query": print_ast(document)} # type: EventDataType |
| 44 | + |
| 45 | + if operation_ast is not None: |
| 46 | + data["variables"] = operation_ast.variable_definitions |
| 47 | + if operation_ast.name is not None: |
| 48 | + data["operationName"] = operation_ast.name.value |
| 49 | + |
| 50 | + return data |
| 51 | + except (AttributeError, TypeError): |
| 52 | + return dict() |
| 53 | + |
| 54 | + |
| 55 | +def _transport_method(transport): |
| 56 | + # type: (Union[Transport, AsyncTransport]) -> str |
| 57 | + """ |
| 58 | + The RequestsHTTPTransport allows defining the HTTP method; all |
| 59 | + other transports use POST. |
| 60 | + """ |
| 61 | + try: |
| 62 | + return transport.method |
| 63 | + except AttributeError: |
| 64 | + return "POST" |
| 65 | + |
| 66 | + |
| 67 | +def _request_info_from_transport(transport): |
| 68 | + # type: (Union[Transport, AsyncTransport, None]) -> Dict[str, str] |
| 69 | + if transport is None: |
| 70 | + return {} |
| 71 | + |
| 72 | + request_info = { |
| 73 | + "method": _transport_method(transport), |
| 74 | + } |
| 75 | + |
| 76 | + try: |
| 77 | + request_info["url"] = transport.url |
| 78 | + except AttributeError: |
| 79 | + pass |
| 80 | + |
| 81 | + return request_info |
| 82 | + |
| 83 | + |
| 84 | +def _patch_execute(): |
| 85 | + # type: () -> None |
| 86 | + real_execute = gql.Client.execute |
| 87 | + |
| 88 | + def sentry_patched_execute(self, document, *args, **kwargs): |
| 89 | + # type: (gql.Client, DocumentNode, Any, Any) -> Any |
| 90 | + hub = Hub.current |
| 91 | + if hub.get_integration(GQLIntegration) is None: |
| 92 | + return real_execute(self, document, *args, **kwargs) |
| 93 | + |
| 94 | + with Hub.current.configure_scope() as scope: |
| 95 | + scope.add_event_processor(_make_gql_event_processor(self, document)) |
| 96 | + |
| 97 | + try: |
| 98 | + return real_execute(self, document, *args, **kwargs) |
| 99 | + except TransportQueryError as e: |
| 100 | + event, hint = event_from_exception( |
| 101 | + e, |
| 102 | + client_options=hub.client.options if hub.client is not None else None, |
| 103 | + mechanism={"type": "gql", "handled": False}, |
| 104 | + ) |
| 105 | + |
| 106 | + hub.capture_event(event, hint) |
| 107 | + raise e |
| 108 | + |
| 109 | + gql.Client.execute = sentry_patched_execute |
| 110 | + |
| 111 | + |
| 112 | +def _make_gql_event_processor(client, document): |
| 113 | + # type: (gql.Client, DocumentNode) -> EventProcessor |
| 114 | + def processor(event, hint): |
| 115 | + # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any] |
| 116 | + try: |
| 117 | + errors = hint["exc_info"][1].errors |
| 118 | + except (AttributeError, KeyError): |
| 119 | + errors = None |
| 120 | + |
| 121 | + request = event.setdefault("request", {}) |
| 122 | + request.update( |
| 123 | + { |
| 124 | + "api_target": "graphql", |
| 125 | + **_request_info_from_transport(client.transport), |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + if _should_send_default_pii(): |
| 130 | + request["data"] = _data_from_document(document) |
| 131 | + contexts = event.setdefault("contexts", {}) |
| 132 | + response = contexts.setdefault("response", {}) |
| 133 | + response.update( |
| 134 | + { |
| 135 | + "data": {"errors": errors}, |
| 136 | + "type": response, |
| 137 | + } |
| 138 | + ) |
| 139 | + |
| 140 | + return event |
| 141 | + |
| 142 | + return processor |
0 commit comments