frame.tsx 4.6 KB

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