__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. def strip_stacktrace(stacktrace):
  20. if stacktrace:
  21. stacktrace = dict(stacktrace)
  22. stacktrace["frames"] = [strip_frame(x) for x in stacktrace.get("frames") or ()]
  23. try:
  24. stacktrace["registers"] = {k: v for k, v in stacktrace["registers"].items()}
  25. except KeyError:
  26. pass
  27. return stacktrace
  28. STRIP_TRAILING_ADDR_RE = re.compile(" ?/ 0x[0-9a-fA-F]+$")
  29. def strip_trailing_addr(value):
  30. return STRIP_TRAILING_ADDR_RE.sub("", value)
  31. def normalize_native_exception(exc):
  32. if exc:
  33. exc = dict(exc)
  34. exc["type"] = strip_trailing_addr(exc["type"])
  35. exc["value"] = strip_trailing_addr(exc["value"])
  36. return exc
  37. def strip_stacktrace_container(container):
  38. if container:
  39. container = dict(container)
  40. container["stacktrace"] = strip_stacktrace(container.get("stacktrace"))
  41. container["raw_stacktrace"] = strip_stacktrace(container.get("raw_stacktrace"))
  42. return container
  43. def insta_snapshot_native_stacktrace_data(self, event, **kwargs):
  44. # limit amount of data going into a snapshot so that they don't break all
  45. # the time due to unrelated changes.
  46. self.insta_snapshot(
  47. {
  48. "stacktrace": strip_stacktrace(event.get("stacktrace")),
  49. "exception": {
  50. "values": [
  51. normalize_native_exception(strip_stacktrace_container(x))
  52. for x in get_path(event, "exception", "values") or ()
  53. ]
  54. },
  55. "threads": {
  56. "values": [
  57. strip_stacktrace_container(x)
  58. for x in get_path(event, "threads", "values") or ()
  59. ]
  60. },
  61. "debug_meta": event.get("debug_meta"),
  62. "contexts": {
  63. k: v for k, v in (event.get("contexts") or {}).items() if k != "reprocessing"
  64. }
  65. or None,
  66. "errors": [e for e in event.get("errors") or () if e.get("name") != "timestamp"],
  67. },
  68. **kwargs,
  69. )
  70. def insta_snapshot_javascript_stacktrace_data(insta_snapshot, event):
  71. # limit amount of data going into a snapshot so that they don't break all
  72. # the time due to unrelated changes.
  73. insta_snapshot(
  74. {
  75. "exception": {"values": [x for x in get_path(event, "exception", "values") or ()]},
  76. "errors": [e for e in event.get("errors") or () if e.get("name") != "timestamp"],
  77. }
  78. )
  79. def redact_location(candidates):
  80. """Redacts the sentry location URI to be independent of the specific ID.
  81. This modifies the data passed in, returns None.
  82. """
  83. location_re = re.compile("^sentry://project_debug_file/[0-9]+$")
  84. for candidate in candidates:
  85. try:
  86. location = candidate["location"]
  87. except KeyError:
  88. continue
  89. else:
  90. if location_re.search(location):
  91. candidate["location"] = "sentry://project_debug_file/x"