__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import re
  2. from sentry.utils.safe import get_path
  3. def strip_frame(frame):
  4. if frame:
  5. frame = {
  6. "data": {
  7. "symbolicator_status": get_path(frame, "data", "symbolicator_status"),
  8. "orig_in_app": get_path(frame, "data", "orig_in_app"),
  9. },
  10. "function": frame.get("function"),
  11. "instruction_addr": frame.get("instruction_addr"),
  12. "symbol": frame.get("symbol"),
  13. "package": frame.get("package"),
  14. "lineno": frame.get("lineno"),
  15. "in_app": frame.get("in_app"),
  16. "trust": frame.get("trust"),
  17. }
  18. return frame
  19. NORMALIZED_REGISTERS = {}
  20. def normalize_register(name):
  21. return NORMALIZED_REGISTERS.get(name, name)
  22. def strip_stacktrace(stacktrace):
  23. if stacktrace:
  24. stacktrace = dict(stacktrace)
  25. stacktrace["frames"] = [strip_frame(x) for x in stacktrace.get("frames") or ()]
  26. try:
  27. stacktrace["registers"] = {
  28. normalize_register(k): v for k, v in stacktrace["registers"].items()
  29. }
  30. except KeyError:
  31. pass
  32. return stacktrace
  33. STRIP_TRAILING_ADDR_RE = re.compile(" ?/ 0x[0-9a-fA-F]+$")
  34. def strip_trailing_addr(value):
  35. return STRIP_TRAILING_ADDR_RE.sub("", value)
  36. def normalize_exception(exc):
  37. if exc:
  38. exc = dict(exc)
  39. exc["type"] = strip_trailing_addr(exc["type"])
  40. exc["value"] = strip_trailing_addr(exc["value"])
  41. return exc
  42. def strip_stacktrace_container(container):
  43. if container:
  44. container = dict(container)
  45. container["stacktrace"] = strip_stacktrace(container.get("stacktrace"))
  46. container["raw_stacktrace"] = strip_stacktrace(container.get("raw_stacktrace"))
  47. return container
  48. def insta_snapshot_stacktrace_data(self, event, **kwargs):
  49. # limit amount of data going into a snapshot so that they don't break all
  50. # the time due to unrelated changes.
  51. self.insta_snapshot(
  52. {
  53. "stacktrace": strip_stacktrace(event.get("stacktrace")),
  54. "exception": {
  55. "values": [
  56. normalize_exception(strip_stacktrace_container(x))
  57. for x in get_path(event, "exception", "values") or ()
  58. ]
  59. },
  60. "threads": {
  61. "values": [
  62. strip_stacktrace_container(x)
  63. for x in get_path(event, "threads", "values") or ()
  64. ]
  65. },
  66. "debug_meta": event.get("debug_meta"),
  67. "contexts": {
  68. k: v for k, v in (event.get("contexts") or {}).items() if k != "reprocessing"
  69. }
  70. or None,
  71. "errors": [e for e in event.get("errors") or () if e.get("name") != "timestamp"],
  72. },
  73. **kwargs,
  74. )