__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_native_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_native_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_native_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. )
  75. def insta_snapshot_javascript_stacktrace_data(insta_snapshot, event):
  76. # limit amount of data going into a snapshot so that they don't break all
  77. # the time due to unrelated changes.
  78. insta_snapshot(
  79. {
  80. "exception": {"values": [x for x in get_path(event, "exception", "values") or ()]},
  81. "errors": [e for e in event.get("errors") or () if e.get("name") != "timestamp"],
  82. }
  83. )
  84. def redact_location(candidates):
  85. """Redacts the sentry location URI to be independent of the specific ID.
  86. This modifies the data passed in, returns None.
  87. """
  88. location_re = re.compile("^sentry://project_debug_file/[0-9]+$")
  89. for candidate in candidates:
  90. try:
  91. location = candidate["location"]
  92. except KeyError:
  93. continue
  94. else:
  95. if location_re.search(location):
  96. candidate["location"] = "sentry://project_debug_file/x"