選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

274 行
8.9 KiB

  1. import os
  2. import warnings
  3. from threading import Thread, Lock, Event
  4. from contextlib import contextmanager
  5. import sentry_sdk
  6. from sentry_sdk.envelope import Envelope
  7. from sentry_sdk.session import Session
  8. from sentry_sdk.utils import format_timestamp
  9. from typing import TYPE_CHECKING
  10. if TYPE_CHECKING:
  11. from typing import Any
  12. from typing import Callable
  13. from typing import Dict
  14. from typing import Generator
  15. from typing import List
  16. from typing import Optional
  17. from typing import Union
  18. def is_auto_session_tracking_enabled(hub=None):
  19. # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None]
  20. """DEPRECATED: Utility function to find out if session tracking is enabled."""
  21. # Internal callers should use private _is_auto_session_tracking_enabled, instead.
  22. warnings.warn(
  23. "This function is deprecated and will be removed in the next major release. "
  24. "There is no public API replacement.",
  25. DeprecationWarning,
  26. stacklevel=2,
  27. )
  28. if hub is None:
  29. hub = sentry_sdk.Hub.current
  30. should_track = hub.scope._force_auto_session_tracking
  31. if should_track is None:
  32. client_options = hub.client.options if hub.client else {}
  33. should_track = client_options.get("auto_session_tracking", False)
  34. return should_track
  35. @contextmanager
  36. def auto_session_tracking(hub=None, session_mode="application"):
  37. # type: (Optional[sentry_sdk.Hub], str) -> Generator[None, None, None]
  38. """DEPRECATED: Use track_session instead
  39. Starts and stops a session automatically around a block.
  40. """
  41. warnings.warn(
  42. "This function is deprecated and will be removed in the next major release. "
  43. "Use track_session instead.",
  44. DeprecationWarning,
  45. stacklevel=2,
  46. )
  47. if hub is None:
  48. hub = sentry_sdk.Hub.current
  49. with warnings.catch_warnings():
  50. warnings.simplefilter("ignore", DeprecationWarning)
  51. should_track = is_auto_session_tracking_enabled(hub)
  52. if should_track:
  53. hub.start_session(session_mode=session_mode)
  54. try:
  55. yield
  56. finally:
  57. if should_track:
  58. hub.end_session()
  59. def is_auto_session_tracking_enabled_scope(scope):
  60. # type: (sentry_sdk.Scope) -> bool
  61. """
  62. DEPRECATED: Utility function to find out if session tracking is enabled.
  63. """
  64. warnings.warn(
  65. "This function is deprecated and will be removed in the next major release. "
  66. "There is no public API replacement.",
  67. DeprecationWarning,
  68. stacklevel=2,
  69. )
  70. # Internal callers should use private _is_auto_session_tracking_enabled, instead.
  71. return _is_auto_session_tracking_enabled(scope)
  72. def _is_auto_session_tracking_enabled(scope):
  73. # type: (sentry_sdk.Scope) -> bool
  74. """
  75. Utility function to find out if session tracking is enabled.
  76. """
  77. should_track = scope._force_auto_session_tracking
  78. if should_track is None:
  79. client_options = sentry_sdk.get_client().options
  80. should_track = client_options.get("auto_session_tracking", False)
  81. return should_track
  82. @contextmanager
  83. def auto_session_tracking_scope(scope, session_mode="application"):
  84. # type: (sentry_sdk.Scope, str) -> Generator[None, None, None]
  85. """DEPRECATED: This function is a deprecated alias for track_session.
  86. Starts and stops a session automatically around a block.
  87. """
  88. warnings.warn(
  89. "This function is a deprecated alias for track_session and will be removed in the next major release.",
  90. DeprecationWarning,
  91. stacklevel=2,
  92. )
  93. with track_session(scope, session_mode=session_mode):
  94. yield
  95. @contextmanager
  96. def track_session(scope, session_mode="application"):
  97. # type: (sentry_sdk.Scope, str) -> Generator[None, None, None]
  98. """
  99. Start a new session in the provided scope, assuming session tracking is enabled.
  100. This is a no-op context manager if session tracking is not enabled.
  101. """
  102. should_track = _is_auto_session_tracking_enabled(scope)
  103. if should_track:
  104. scope.start_session(session_mode=session_mode)
  105. try:
  106. yield
  107. finally:
  108. if should_track:
  109. scope.end_session()
  110. TERMINAL_SESSION_STATES = ("exited", "abnormal", "crashed")
  111. MAX_ENVELOPE_ITEMS = 100
  112. def make_aggregate_envelope(aggregate_states, attrs):
  113. # type: (Any, Any) -> Any
  114. return {"attrs": dict(attrs), "aggregates": list(aggregate_states.values())}
  115. class SessionFlusher:
  116. def __init__(
  117. self,
  118. capture_func, # type: Callable[[Envelope], None]
  119. flush_interval=60, # type: int
  120. ):
  121. # type: (...) -> None
  122. self.capture_func = capture_func
  123. self.flush_interval = flush_interval
  124. self.pending_sessions = [] # type: List[Any]
  125. self.pending_aggregates = {} # type: Dict[Any, Any]
  126. self._thread = None # type: Optional[Thread]
  127. self._thread_lock = Lock()
  128. self._aggregate_lock = Lock()
  129. self._thread_for_pid = None # type: Optional[int]
  130. self.__shutdown_requested = Event()
  131. def flush(self):
  132. # type: (...) -> None
  133. pending_sessions = self.pending_sessions
  134. self.pending_sessions = []
  135. with self._aggregate_lock:
  136. pending_aggregates = self.pending_aggregates
  137. self.pending_aggregates = {}
  138. envelope = Envelope()
  139. for session in pending_sessions:
  140. if len(envelope.items) == MAX_ENVELOPE_ITEMS:
  141. self.capture_func(envelope)
  142. envelope = Envelope()
  143. envelope.add_session(session)
  144. for attrs, states in pending_aggregates.items():
  145. if len(envelope.items) == MAX_ENVELOPE_ITEMS:
  146. self.capture_func(envelope)
  147. envelope = Envelope()
  148. envelope.add_sessions(make_aggregate_envelope(states, attrs))
  149. if len(envelope.items) > 0:
  150. self.capture_func(envelope)
  151. def _ensure_running(self):
  152. # type: (...) -> None
  153. """
  154. Check that we have an active thread to run in, or create one if not.
  155. Note that this might fail (e.g. in Python 3.12 it's not possible to
  156. spawn new threads at interpreter shutdown). In that case self._running
  157. will be False after running this function.
  158. """
  159. if self._thread_for_pid == os.getpid() and self._thread is not None:
  160. return None
  161. with self._thread_lock:
  162. if self._thread_for_pid == os.getpid() and self._thread is not None:
  163. return None
  164. def _thread():
  165. # type: (...) -> None
  166. running = True
  167. while running:
  168. running = not self.__shutdown_requested.wait(self.flush_interval)
  169. self.flush()
  170. thread = Thread(target=_thread)
  171. thread.daemon = True
  172. try:
  173. thread.start()
  174. except RuntimeError:
  175. # Unfortunately at this point the interpreter is in a state that no
  176. # longer allows us to spawn a thread and we have to bail.
  177. self.__shutdown_requested.set()
  178. return None
  179. self._thread = thread
  180. self._thread_for_pid = os.getpid()
  181. return None
  182. def add_aggregate_session(
  183. self, session # type: Session
  184. ):
  185. # type: (...) -> None
  186. # NOTE on `session.did`:
  187. # the protocol can deal with buckets that have a distinct-id, however
  188. # in practice we expect the python SDK to have an extremely high cardinality
  189. # here, effectively making aggregation useless, therefore we do not
  190. # aggregate per-did.
  191. # For this part we can get away with using the global interpreter lock
  192. with self._aggregate_lock:
  193. attrs = session.get_json_attrs(with_user_info=False)
  194. primary_key = tuple(sorted(attrs.items()))
  195. secondary_key = session.truncated_started # (, session.did)
  196. states = self.pending_aggregates.setdefault(primary_key, {})
  197. state = states.setdefault(secondary_key, {})
  198. if "started" not in state:
  199. state["started"] = format_timestamp(session.truncated_started)
  200. # if session.did is not None:
  201. # state["did"] = session.did
  202. if session.status == "crashed":
  203. state["crashed"] = state.get("crashed", 0) + 1
  204. elif session.status == "abnormal":
  205. state["abnormal"] = state.get("abnormal", 0) + 1
  206. elif session.errors > 0:
  207. state["errored"] = state.get("errored", 0) + 1
  208. else:
  209. state["exited"] = state.get("exited", 0) + 1
  210. def add_session(
  211. self, session # type: Session
  212. ):
  213. # type: (...) -> None
  214. if session.session_mode == "request":
  215. self.add_aggregate_session(session)
  216. else:
  217. self.pending_sessions.append(session.to_json())
  218. self._ensure_running()
  219. def kill(self):
  220. # type: (...) -> None
  221. self.__shutdown_requested.set()