queries.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. Code used for the Queries module in Sentry
  3. """
  4. from sentry_sdk.consts import OP, SPANDATA
  5. from sentry_sdk.integrations.redis.utils import _get_safe_command
  6. from sentry_sdk.utils import capture_internal_exceptions
  7. from typing import TYPE_CHECKING
  8. if TYPE_CHECKING:
  9. from redis import Redis
  10. from sentry_sdk.integrations.redis import RedisIntegration
  11. from sentry_sdk.tracing import Span
  12. from typing import Any
  13. def _compile_db_span_properties(
  14. integration: "RedisIntegration", redis_command: str, args: "tuple[Any, ...]"
  15. ) -> "dict[str, Any]":
  16. description = _get_db_span_description(integration, redis_command, args)
  17. properties = {
  18. "op": OP.DB_REDIS,
  19. "description": description,
  20. }
  21. return properties
  22. def _get_db_span_description(
  23. integration: "RedisIntegration", command_name: str, args: "tuple[Any, ...]"
  24. ) -> str:
  25. description = command_name
  26. with capture_internal_exceptions():
  27. description = _get_safe_command(command_name, args)
  28. if integration.max_data_size and len(description) > integration.max_data_size:
  29. description = description[: integration.max_data_size - len("...")] + "..."
  30. return description
  31. def _set_db_data_on_span(span: "Span", connection_params: "dict[str, Any]") -> None:
  32. span.set_data(SPANDATA.DB_SYSTEM, "redis")
  33. db = connection_params.get("db")
  34. if db is not None:
  35. span.set_data(SPANDATA.DB_NAME, str(db))
  36. host = connection_params.get("host")
  37. if host is not None:
  38. span.set_data(SPANDATA.SERVER_ADDRESS, host)
  39. port = connection_params.get("port")
  40. if port is not None:
  41. span.set_data(SPANDATA.SERVER_PORT, port)
  42. def _set_db_data(span: "Span", redis_instance: "Redis[Any]") -> None:
  43. try:
  44. _set_db_data_on_span(span, redis_instance.connection_pool.connection_kwargs)
  45. except AttributeError:
  46. pass # connections_kwargs may be missing in some cases