frame.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import type {SymbolicatorStatus} from 'sentry/components/events/interfaces/types';
  2. import {t} from 'sentry/locale';
  3. const ROOT_KEY = 'sentry root';
  4. export class Frame {
  5. readonly key: string | number;
  6. readonly name: string;
  7. readonly file?: string;
  8. readonly line?: number;
  9. readonly column?: number;
  10. readonly is_application: boolean;
  11. readonly path?: string;
  12. readonly package?: string;
  13. readonly module?: string;
  14. readonly resource?: string;
  15. readonly threadId?: number;
  16. readonly inline?: boolean;
  17. readonly instructionAddr?: string;
  18. readonly symbol?: string;
  19. readonly symbolAddr?: string;
  20. readonly symbolicatorStatus?: SymbolicatorStatus;
  21. readonly isRoot: boolean;
  22. totalWeight: number = 0;
  23. selfWeight: number = 0;
  24. static Root = new Frame({
  25. key: ROOT_KEY,
  26. name: ROOT_KEY,
  27. is_application: false,
  28. });
  29. constructor(
  30. frameInfo: Profiling.FrameInfo,
  31. type?: 'mobile' | 'javascript' | 'node' | string
  32. ) {
  33. this.key = frameInfo.key;
  34. this.file = frameInfo.file;
  35. this.name = frameInfo.name;
  36. this.resource = frameInfo.resource;
  37. this.line = frameInfo.line;
  38. this.column = frameInfo.column;
  39. this.is_application = !!frameInfo.is_application;
  40. this.package = frameInfo.package;
  41. this.module = frameInfo.module ?? frameInfo.image;
  42. this.threadId = frameInfo.threadId;
  43. this.path = frameInfo.path;
  44. this.instructionAddr = frameInfo.instructionAddr;
  45. this.symbol = frameInfo.symbol;
  46. this.symbolAddr = frameInfo.symbolAddr;
  47. this.symbolicatorStatus = frameInfo.symbolicatorStatus;
  48. this.isRoot = this.key === ROOT_KEY;
  49. // We are remapping some of the keys as they differ between platforms.
  50. // This is a temporary solution until we adopt a unified format.
  51. if (frameInfo.columnNumber && this.column === undefined) {
  52. this.column = frameInfo.columnNumber;
  53. }
  54. if (frameInfo.lineNumber && this.column === undefined) {
  55. this.line = frameInfo.lineNumber;
  56. }
  57. if (frameInfo.scriptName && this.column === undefined) {
  58. this.resource = frameInfo.scriptName;
  59. }
  60. // If the frame is a web frame and there is no name associated to it, then it was likely invoked as an iife or anonymous callback as
  61. // most modern browser engines properly show anonymous functions when they are assigned to references (e.g. `let foo = function() {};`)
  62. if (type === 'javascript' || type === 'node') {
  63. if (this.name === '(garbage collector)' || this.name === '(root)') {
  64. this.is_application = false;
  65. }
  66. if (!this.name || this.name === 'unknown') {
  67. this.name = t('<anonymous>');
  68. }
  69. // If the frame had no line or column, it was part of the native code, (e.g. calling String.fromCharCode)
  70. if (this.line === undefined && this.column === undefined) {
  71. this.name += ` ${t('[native code]')}`;
  72. this.is_application = false;
  73. }
  74. // Doing this on the frontend while we figure out how to do this on the backend/client properly
  75. // @TODO Our old node.js incorrectly sends file instead of path (fixed in SDK, but not all SDK's are upgraded :rip:)
  76. const pathOrFile = this.path || this.file;
  77. if (pathOrFile) {
  78. if (pathOrFile.startsWith('node:internal')) {
  79. this.is_application = false;
  80. }
  81. if (this.module === undefined && pathOrFile) {
  82. const match =
  83. /node_modules(\/|\\)(?<maybeScopeOrPackage>.*?)(\/|\\)((?<maybePackage>.*)((\/|\\)))?/.exec(
  84. pathOrFile
  85. );
  86. if (match?.groups) {
  87. const {maybeScopeOrPackage, maybePackage} = match.groups;
  88. if (maybeScopeOrPackage.startsWith('@')) {
  89. this.module = `${maybeScopeOrPackage}/${maybePackage}`;
  90. } else {
  91. this.module = match.groups.maybeScopeOrPackage;
  92. }
  93. this.is_application = false;
  94. }
  95. }
  96. // Extract the first component of node:namespace/pkg if there is one
  97. // else return just the node:namespace
  98. if (pathOrFile?.substring(0, 5) === 'node:') {
  99. let image = '';
  100. const l = pathOrFile.length;
  101. for (let i = 0; i < l; i++) {
  102. if (pathOrFile.charAt(i) === '/') {
  103. image += '/';
  104. i++;
  105. while (i < l && pathOrFile.charAt(i) !== '/') {
  106. image += pathOrFile.charAt(i);
  107. i++;
  108. }
  109. break;
  110. }
  111. image += pathOrFile.charAt(i);
  112. }
  113. if (image) {
  114. this.module = image;
  115. }
  116. }
  117. }
  118. }
  119. if (!this.name) {
  120. this.name = t('<unknown>');
  121. }
  122. }
  123. }