culprit.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. This file implements the legacy culprit system. The culprit at this point is
  3. just used as a fallback if no transaction is set. When a transaction is set
  4. the culprit is overridden by the transaction value.
  5. Over time we want to fully phase out the culprit. Until then this is the
  6. code that generates it.
  7. """
  8. from sentry.constants import MAX_CULPRIT_LENGTH
  9. from sentry.utils.safe import get_path
  10. from sentry.utils.strings import truncatechars
  11. def generate_culprit(data):
  12. platform = data.get("platform")
  13. exceptions = get_path(data, "exception", "values", filter=True)
  14. if exceptions:
  15. # Synthetic events no longer get a culprit
  16. last_exception = get_path(exceptions, -1)
  17. if get_path(last_exception, "mechanism", "synthetic"):
  18. return ""
  19. stacktraces = [
  20. e["stacktrace"] for e in exceptions if get_path(e, "stacktrace", "frames")
  21. ]
  22. else:
  23. stacktrace = data.get("stacktrace")
  24. if stacktrace and stacktrace.get("frames"):
  25. stacktraces = [stacktrace]
  26. else:
  27. stacktraces = None
  28. culprit = None
  29. if not culprit and stacktraces:
  30. culprit = get_stacktrace_culprit(get_path(stacktraces, -1), platform=platform)
  31. if not culprit and data.get("request"):
  32. culprit = get_path(data, "request", "url")
  33. return truncatechars(culprit or "", MAX_CULPRIT_LENGTH)
  34. def get_stacktrace_culprit(stacktrace, platform):
  35. default = None
  36. for frame in reversed(stacktrace["frames"]):
  37. if not frame:
  38. continue
  39. if frame.get("in_app"):
  40. culprit = get_frame_culprit(frame, platform=platform)
  41. if culprit:
  42. return culprit
  43. elif default is None:
  44. default = get_frame_culprit(frame, platform=platform)
  45. return default
  46. def get_frame_culprit(frame, platform):
  47. # If this frame has a platform, we use it instead of the one that
  48. # was passed in (as that one comes from the exception which might
  49. # not necessarily be the same platform).
  50. platform = frame.get("platform") or platform
  51. if platform in ("objc", "cocoa", "native"):
  52. return frame.get("function") or "?"
  53. fileloc = frame.get("module") or frame.get("filename")
  54. if not fileloc:
  55. return ""
  56. elif platform in ("javascript", "node"):
  57. # function and fileloc might be unicode here, so let it coerce
  58. # to a unicode string if needed.
  59. return "%s(%s)" % (frame.get("function") or "?", fileloc)
  60. return "%s in %s" % (fileloc, frame.get("function") or "?")