clickhouse_driver.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import sentry_sdk
  2. from sentry_sdk.consts import OP, SPANDATA
  3. from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
  4. from sentry_sdk.tracing import Span
  5. from sentry_sdk.scope import should_send_default_pii
  6. from sentry_sdk.utils import capture_internal_exceptions, ensure_integration_enabled
  7. from typing import TYPE_CHECKING, TypeVar
  8. # Hack to get new Python features working in older versions
  9. # without introducing a hard dependency on `typing_extensions`
  10. # from: https://stackoverflow.com/a/71944042/300572
  11. if TYPE_CHECKING:
  12. from collections.abc import Iterator
  13. from typing import Any, ParamSpec, Callable
  14. else:
  15. # Fake ParamSpec
  16. class ParamSpec:
  17. def __init__(self, _):
  18. self.args = None
  19. self.kwargs = None
  20. # Callable[anything] will return None
  21. class _Callable:
  22. def __getitem__(self, _):
  23. return None
  24. # Make instances
  25. Callable = _Callable()
  26. try:
  27. from clickhouse_driver import VERSION # type: ignore[import-not-found]
  28. from clickhouse_driver.client import Client # type: ignore[import-not-found]
  29. from clickhouse_driver.connection import Connection # type: ignore[import-not-found]
  30. except ImportError:
  31. raise DidNotEnable("clickhouse-driver not installed.")
  32. class ClickhouseDriverIntegration(Integration):
  33. identifier = "clickhouse_driver"
  34. origin = f"auto.db.{identifier}"
  35. @staticmethod
  36. def setup_once() -> None:
  37. _check_minimum_version(ClickhouseDriverIntegration, VERSION)
  38. # Every query is done using the Connection's `send_query` function
  39. Connection.send_query = _wrap_start(Connection.send_query)
  40. # If the query contains parameters then the send_data function is used to send those parameters to clickhouse
  41. _wrap_send_data()
  42. # Every query ends either with the Client's `receive_end_of_query` (no result expected)
  43. # or its `receive_result` (result expected)
  44. Client.receive_end_of_query = _wrap_end(Client.receive_end_of_query)
  45. if hasattr(Client, "receive_end_of_insert_query"):
  46. # In 0.2.7, insert queries are handled separately via `receive_end_of_insert_query`
  47. Client.receive_end_of_insert_query = _wrap_end(
  48. Client.receive_end_of_insert_query
  49. )
  50. Client.receive_result = _wrap_end(Client.receive_result)
  51. P = ParamSpec("P")
  52. T = TypeVar("T")
  53. def _wrap_start(f: "Callable[P, T]") -> "Callable[P, T]":
  54. @ensure_integration_enabled(ClickhouseDriverIntegration, f)
  55. def _inner(*args: "P.args", **kwargs: "P.kwargs") -> "T":
  56. connection = args[0]
  57. query = args[1]
  58. query_id = args[2] if len(args) > 2 else kwargs.get("query_id")
  59. params = args[3] if len(args) > 3 else kwargs.get("params")
  60. span = sentry_sdk.start_span(
  61. op=OP.DB,
  62. name=query,
  63. origin=ClickhouseDriverIntegration.origin,
  64. )
  65. connection._sentry_span = span # type: ignore[attr-defined]
  66. _set_db_data(span, connection)
  67. span.set_data("query", query)
  68. if query_id:
  69. span.set_data("db.query_id", query_id)
  70. if params and should_send_default_pii():
  71. span.set_data("db.params", params)
  72. # run the original code
  73. ret = f(*args, **kwargs)
  74. return ret
  75. return _inner
  76. def _wrap_end(f: "Callable[P, T]") -> "Callable[P, T]":
  77. def _inner_end(*args: "P.args", **kwargs: "P.kwargs") -> "T":
  78. res = f(*args, **kwargs)
  79. instance = args[0]
  80. span = getattr(instance.connection, "_sentry_span", None) # type: ignore[attr-defined]
  81. if span is not None:
  82. if res is not None and should_send_default_pii():
  83. span.set_data("db.result", res)
  84. with capture_internal_exceptions():
  85. span.scope.add_breadcrumb(
  86. message=span._data.pop("query"), category="query", data=span._data
  87. )
  88. span.finish()
  89. return res
  90. return _inner_end
  91. def _wrap_send_data() -> None:
  92. original_send_data = Client.send_data
  93. def _inner_send_data( # type: ignore[no-untyped-def] # clickhouse-driver does not type send_data
  94. self, sample_block, data, types_check=False, columnar=False, *args, **kwargs
  95. ):
  96. span = getattr(self.connection, "_sentry_span", None)
  97. if span is not None:
  98. _set_db_data(span, self.connection)
  99. if should_send_default_pii():
  100. db_params = span._data.get("db.params", [])
  101. if isinstance(data, (list, tuple)):
  102. db_params.extend(data)
  103. else: # data is a generic iterator
  104. orig_data = data
  105. # Wrap the generator to add items to db.params as they are yielded.
  106. # This allows us to send the params to Sentry without needing to allocate
  107. # memory for the entire generator at once.
  108. def wrapped_generator() -> "Iterator[Any]":
  109. for item in orig_data:
  110. db_params.append(item)
  111. yield item
  112. # Replace the original iterator with the wrapped one.
  113. data = wrapped_generator()
  114. span.set_data("db.params", db_params)
  115. return original_send_data(
  116. self, sample_block, data, types_check, columnar, *args, **kwargs
  117. )
  118. Client.send_data = _inner_send_data
  119. def _set_db_data(span: "Span", connection: "Connection") -> None:
  120. span.set_data(SPANDATA.DB_SYSTEM, "clickhouse")
  121. span.set_data(SPANDATA.SERVER_ADDRESS, connection.host)
  122. span.set_data(SPANDATA.SERVER_PORT, connection.port)
  123. span.set_data(SPANDATA.DB_NAME, connection.database)
  124. span.set_data(SPANDATA.DB_USER, connection.user)