getReasonGroupName.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import {Outcome} from 'sentry/types';
  2. // List of Relay's current invalid reasons - https://github.com/getsentry/relay/blob/89a8dd7caaad1f126e1cacced0d73bb50fcd4f5a/relay-server/src/services/outcome.rs#L333
  3. enum DiscardReason {
  4. DUPLICATE = 'duplicate',
  5. PROJECT_ID = 'project_id',
  6. AUTH_VERSION = 'auth_version',
  7. AUTH_CLIENT = 'auth_client',
  8. NO_DATA = 'no_data',
  9. DISALLOWED_METHOD = 'disallowed_method',
  10. CONTENT_TYPE = 'content_type',
  11. INVALID_MULTIPART = 'invalid_multipart',
  12. INVALID_MSGPACK = 'invalid_msgpack',
  13. INVALID_JSON = 'invalid_json',
  14. INVALID_ENVELOPE = 'invalid_envelope',
  15. TIMESTAMP = 'timestamp',
  16. DUPLICATE_ITEM = 'duplicate_item',
  17. INVALID_TRANSACTION = 'invalid_transaction',
  18. INVALID_SPAN = 'invalid_span',
  19. INVALID_REPLAY = 'invalid_replay',
  20. INVALID_REPLAY_RECORDING = 'invalid_replay_recording',
  21. INVALID_REPLAY_VIDEO = 'invalid_replay_video',
  22. PAYLOAD = 'payload',
  23. INVALID_COMPRESSION = 'invalid_compression',
  24. TOO_LARGE = 'too_large',
  25. MISSING_MINIDUMP_UPLOAD = 'missing_minidump_upload',
  26. INVALID_MINIDUMP = 'invalid_minidump',
  27. SECURITY_REPORT = 'security_report',
  28. SECURITY_REPORT_TYPE = 'security_report_type',
  29. PROCESS_UNREAL = 'process_unreal',
  30. CORS = 'cors',
  31. NO_EVENT_PAYLOAD = 'no_event_payload',
  32. EMPTY_ENVELOPE = 'empty_envelope',
  33. INVALID_REPLAY_NO_PAYLOAD = 'invalid_replay_no_payload',
  34. TRANSACTION_SAMPLED = 'transaction_sampled',
  35. INTERNAL = 'internal',
  36. MULTI_PROJECT_ID = 'multi_project_id',
  37. PROJECT_STATE = 'project_state',
  38. PROJECT_STATE_PII = 'project_state_pii',
  39. INVALID_REPLAY_PII_SCRUBBER_FAILED = 'invalid_replay_pii_scrubber_failed',
  40. FEATURE_DISABLED = 'feature_disabled',
  41. }
  42. // List of Relay's current filtered reasons - https://github.com/getsentry/relay/blob/ce5520b4a3bea022808982a52a66bfddacc70ac0/relay-filter/src/common.rs#L11
  43. enum FilteredReason {
  44. BROWSER_EXTENSION = 'browser-extensions',
  45. DENIED_NAME = 'denied-name',
  46. DISABLED_NAMESPACE = 'disabled-namespace',
  47. ERROR_MESSAGE = 'error-message',
  48. FILTERED_TRANSACTION = 'filtered-transaction',
  49. INVALID_CSP = 'invalid-csp',
  50. IP_ADDRESS = 'ip-address',
  51. LEGACY_BROWSER = 'legacy-browsers',
  52. LOCALHOST = 'localhost',
  53. RELEASE_VERSION = 'release-version',
  54. WEB_CRAWLER = 'web-crawlers',
  55. }
  56. // List of Client Discard Reason according to the Client Report's doc - https://develop.sentry.dev/sdk/client-reports/#envelope-item-payload
  57. enum ClientDiscardReason {
  58. QUEUE_OVERFLOW = 'queue_overflow',
  59. CACHE_OVERFLOW = 'cache_overflow',
  60. RATELIMIT_BACKOFF = 'ratelimit_backoff',
  61. NETWORK_ERROR = 'network_error',
  62. SAMPLE_RATE = 'sample_rate',
  63. BEFORE_SEND = 'before_send',
  64. EVENT_PROCESSSOR = 'event_processor',
  65. SEND_ERROR = 'send_error',
  66. INTERNAL_SDK_ERROR = 'internal_sdk_error',
  67. INSUFFICIENT_DATA = 'insufficient_data',
  68. BACKPRESSURE = 'backpressure',
  69. }
  70. enum RateLimitedReason {
  71. KEY_QUOTA = 'key_quota',
  72. SPIKE_PROTECTION = 'spike_protection',
  73. SMART_RATE_LIMIT = 'smart_rate_limit',
  74. }
  75. // Invalid reasons should not be exposed directly, but instead in the following groups:
  76. const invalidReasonsGroup: Record<string, DiscardReason[]> = {
  77. duplicate: [DiscardReason.DUPLICATE],
  78. project_missing: [DiscardReason.PROJECT_ID],
  79. invalid_request: [
  80. DiscardReason.AUTH_VERSION,
  81. DiscardReason.AUTH_CLIENT,
  82. DiscardReason.NO_DATA,
  83. DiscardReason.DISALLOWED_METHOD,
  84. DiscardReason.CONTENT_TYPE,
  85. DiscardReason.INVALID_MULTIPART,
  86. DiscardReason.INVALID_MSGPACK,
  87. DiscardReason.INVALID_JSON,
  88. DiscardReason.INVALID_ENVELOPE,
  89. DiscardReason.TIMESTAMP,
  90. DiscardReason.DUPLICATE_ITEM,
  91. ],
  92. invalid_data: [
  93. DiscardReason.INVALID_TRANSACTION,
  94. DiscardReason.INVALID_SPAN,
  95. DiscardReason.INVALID_REPLAY,
  96. DiscardReason.INVALID_REPLAY_RECORDING,
  97. DiscardReason.INVALID_REPLAY_VIDEO,
  98. ],
  99. payload: [DiscardReason.PAYLOAD, DiscardReason.INVALID_COMPRESSION],
  100. too_large: [DiscardReason.TOO_LARGE],
  101. minidump: [DiscardReason.MISSING_MINIDUMP_UPLOAD, DiscardReason.INVALID_MINIDUMP],
  102. security_report: [DiscardReason.SECURITY_REPORT, DiscardReason.SECURITY_REPORT_TYPE],
  103. unreal: [DiscardReason.PROCESS_UNREAL],
  104. cors: [DiscardReason.CORS],
  105. empty: [
  106. DiscardReason.NO_EVENT_PAYLOAD,
  107. DiscardReason.EMPTY_ENVELOPE,
  108. DiscardReason.INVALID_REPLAY_NO_PAYLOAD,
  109. ],
  110. sampling: [DiscardReason.TRANSACTION_SAMPLED],
  111. };
  112. function getInvalidReasonGroupName(reason: DiscardReason): string {
  113. for (const [group, reasons] of Object.entries(invalidReasonsGroup)) {
  114. if (reasons.includes(reason)) {
  115. return group;
  116. }
  117. }
  118. return 'internal';
  119. }
  120. function getRateLimitedReasonGroupName(reason: RateLimitedReason | string): string {
  121. if (reason.endsWith('_usage_exceeded')) {
  122. return 'quota';
  123. }
  124. switch (reason) {
  125. case RateLimitedReason.KEY_QUOTA:
  126. return 'key limit';
  127. case RateLimitedReason.SPIKE_PROTECTION:
  128. case RateLimitedReason.SMART_RATE_LIMIT:
  129. return 'spike protection';
  130. default:
  131. return 'internal';
  132. }
  133. }
  134. function getFilteredReasonGroupName(reason: FilteredReason): string {
  135. switch (reason) {
  136. case FilteredReason.BROWSER_EXTENSION:
  137. case FilteredReason.ERROR_MESSAGE:
  138. case FilteredReason.FILTERED_TRANSACTION:
  139. case FilteredReason.INVALID_CSP:
  140. case FilteredReason.IP_ADDRESS:
  141. case FilteredReason.LEGACY_BROWSER:
  142. case FilteredReason.LOCALHOST:
  143. case FilteredReason.RELEASE_VERSION:
  144. case FilteredReason.WEB_CRAWLER:
  145. return reason;
  146. default:
  147. return 'other';
  148. }
  149. }
  150. function getClientDiscardReasonGroupName(reason: ClientDiscardReason): string {
  151. switch (reason) {
  152. case ClientDiscardReason.QUEUE_OVERFLOW:
  153. case ClientDiscardReason.CACHE_OVERFLOW:
  154. case ClientDiscardReason.RATELIMIT_BACKOFF:
  155. case ClientDiscardReason.NETWORK_ERROR:
  156. case ClientDiscardReason.SAMPLE_RATE:
  157. case ClientDiscardReason.BEFORE_SEND:
  158. case ClientDiscardReason.EVENT_PROCESSSOR:
  159. case ClientDiscardReason.SEND_ERROR:
  160. case ClientDiscardReason.INTERNAL_SDK_ERROR:
  161. case ClientDiscardReason.INSUFFICIENT_DATA:
  162. case ClientDiscardReason.BACKPRESSURE:
  163. return reason;
  164. default:
  165. return 'other';
  166. }
  167. }
  168. export function getReasonGroupName(outcome: string | number, reason: string): string {
  169. switch (outcome) {
  170. case Outcome.INVALID:
  171. return getInvalidReasonGroupName(reason as DiscardReason);
  172. case Outcome.CARDINALITY_LIMITED:
  173. case Outcome.RATE_LIMITED:
  174. case Outcome.ABUSE:
  175. return getRateLimitedReasonGroupName(reason as RateLimitedReason);
  176. case Outcome.FILTERED:
  177. return getFilteredReasonGroupName(reason as FilteredReason);
  178. case Outcome.CLIENT_DISCARD:
  179. return getClientDiscardReasonGroupName(reason as ClientDiscardReason);
  180. default:
  181. return String(reason);
  182. }
  183. }