|
| 1 | +from sentry_sdk import Hub |
| 2 | +from sentry_sdk.consts import OP, SPANDATA |
| 3 | +from sentry_sdk.hub import _should_send_default_pii |
| 4 | +from sentry_sdk.integrations import Integration, DidNotEnable |
| 5 | +from sentry_sdk.tracing import Span |
| 6 | +from sentry_sdk._types import TYPE_CHECKING |
| 7 | +from sentry_sdk.utils import capture_internal_exceptions |
| 8 | + |
| 9 | +from typing import TypeVar |
| 10 | + |
| 11 | +# Hack to get new Python features working in older versions |
| 12 | +# without introducing a hard dependency on `typing_extensions` |
| 13 | +# from: https://stackoverflow.com/a/71944042/300572 |
| 14 | +if TYPE_CHECKING: |
| 15 | + from typing import ParamSpec, Callable |
| 16 | +else: |
| 17 | + # Fake ParamSpec |
| 18 | + class ParamSpec: |
| 19 | + def __init__(self, _): |
| 20 | + self.args = None |
| 21 | + self.kwargs = None |
| 22 | + |
| 23 | + # Callable[anything] will return None |
| 24 | + class _Callable: |
| 25 | + def __getitem__(self, _): |
| 26 | + return None |
| 27 | + |
| 28 | + # Make instances |
| 29 | + Callable = _Callable() |
| 30 | + |
| 31 | + |
| 32 | +try: |
| 33 | + import clickhouse_driver # type: ignore[import] |
| 34 | + |
| 35 | +except ImportError: |
| 36 | + raise DidNotEnable("clickhouse-driver not installed.") |
| 37 | + |
| 38 | +if clickhouse_driver.VERSION < (0, 2, 0): |
| 39 | + raise DidNotEnable("clickhouse-driver >= 0.2.0 required") |
| 40 | + |
| 41 | + |
| 42 | +class ClickhouseDriverIntegration(Integration): |
| 43 | + identifier = "clickhouse_driver" |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def setup_once() -> None: |
| 47 | + # Every query is done using the Connection's `send_query` function |
| 48 | + clickhouse_driver.connection.Connection.send_query = _wrap_start( |
| 49 | + clickhouse_driver.connection.Connection.send_query |
| 50 | + ) |
| 51 | + |
| 52 | + # If the query contains parameters then the send_data function is used to send those parameters to clickhouse |
| 53 | + clickhouse_driver.client.Client.send_data = _wrap_send_data( |
| 54 | + clickhouse_driver.client.Client.send_data |
| 55 | + ) |
| 56 | + |
| 57 | + # Every query ends either with the Client's `receive_end_of_query` (no result expected) |
| 58 | + # or its `receive_result` (result expected) |
| 59 | + clickhouse_driver.client.Client.receive_end_of_query = _wrap_end( |
| 60 | + clickhouse_driver.client.Client.receive_end_of_query |
| 61 | + ) |
| 62 | + clickhouse_driver.client.Client.receive_result = _wrap_end( |
| 63 | + clickhouse_driver.client.Client.receive_result |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +P = ParamSpec("P") |
| 68 | +T = TypeVar("T") |
| 69 | + |
| 70 | + |
| 71 | +def _wrap_start(f: Callable[P, T]) -> Callable[P, T]: |
| 72 | + def _inner(*args: P.args, **kwargs: P.kwargs) -> T: |
| 73 | + hub = Hub.current |
| 74 | + if hub.get_integration(ClickhouseDriverIntegration) is None: |
| 75 | + return f(*args, **kwargs) |
| 76 | + connection = args[0] |
| 77 | + query = args[1] |
| 78 | + query_id = args[2] if len(args) > 2 else kwargs.get("query_id") |
| 79 | + params = args[3] if len(args) > 3 else kwargs.get("params") |
| 80 | + |
| 81 | + span = hub.start_span(op=OP.DB, description=query) |
| 82 | + |
| 83 | + connection._sentry_span = span # type: ignore[attr-defined] |
| 84 | + |
| 85 | + _set_db_data(span, connection) |
| 86 | + |
| 87 | + span.set_data("query", query) |
| 88 | + |
| 89 | + if query_id: |
| 90 | + span.set_data("db.query_id", query_id) |
| 91 | + |
| 92 | + if params and _should_send_default_pii(): |
| 93 | + span.set_data("db.params", params) |
| 94 | + |
| 95 | + # run the original code |
| 96 | + ret = f(*args, **kwargs) |
| 97 | + |
| 98 | + return ret |
| 99 | + |
| 100 | + return _inner |
| 101 | + |
| 102 | + |
| 103 | +def _wrap_end(f: Callable[P, T]) -> Callable[P, T]: |
| 104 | + def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T: |
| 105 | + res = f(*args, **kwargs) |
| 106 | + instance = args[0] |
| 107 | + span = instance.connection._sentry_span # type: ignore[attr-defined] |
| 108 | + |
| 109 | + if span is not None: |
| 110 | + if res is not None and _should_send_default_pii(): |
| 111 | + span.set_data("db.result", res) |
| 112 | + |
| 113 | + with capture_internal_exceptions(): |
| 114 | + span.hub.add_breadcrumb( |
| 115 | + message=span._data.pop("query"), category="query", data=span._data |
| 116 | + ) |
| 117 | + |
| 118 | + span.finish() |
| 119 | + |
| 120 | + return res |
| 121 | + |
| 122 | + return _inner_end |
| 123 | + |
| 124 | + |
| 125 | +def _wrap_send_data(f: Callable[P, T]) -> Callable[P, T]: |
| 126 | + def _inner_send_data(*args: P.args, **kwargs: P.kwargs) -> T: |
| 127 | + instance = args[0] # type: clickhouse_driver.client.Client |
| 128 | + data = args[2] |
| 129 | + span = instance.connection._sentry_span |
| 130 | + |
| 131 | + _set_db_data(span, instance.connection) |
| 132 | + |
| 133 | + if _should_send_default_pii(): |
| 134 | + db_params = span._data.get("db.params", []) |
| 135 | + db_params.extend(data) |
| 136 | + span.set_data("db.params", db_params) |
| 137 | + |
| 138 | + return f(*args, **kwargs) |
| 139 | + |
| 140 | + return _inner_send_data |
| 141 | + |
| 142 | + |
| 143 | +def _set_db_data( |
| 144 | + span: Span, connection: clickhouse_driver.connection.Connection |
| 145 | +) -> None: |
| 146 | + span.set_data(SPANDATA.DB_SYSTEM, "clickhouse") |
| 147 | + span.set_data(SPANDATA.SERVER_ADDRESS, connection.host) |
| 148 | + span.set_data(SPANDATA.SERVER_PORT, connection.port) |
| 149 | + span.set_data(SPANDATA.DB_NAME, connection.database) |
| 150 | + span.set_data(SPANDATA.DB_USER, connection.user) |
0 commit comments