options.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import mapValues from 'lodash/mapValues';
  2. import {t} from 'sentry/locale';
  3. import {Organization, TagCollection} from 'sentry/types';
  4. import {
  5. FieldKey,
  6. makeTagCollection,
  7. MobileVital,
  8. ReplayClickFieldKey,
  9. ReplayFieldKey,
  10. SpanOpBreakdown,
  11. WebVital,
  12. } from 'sentry/utils/fields';
  13. import {
  14. Dataset,
  15. EventTypes,
  16. SessionsAggregate,
  17. } from 'sentry/views/alerts/rules/metric/types';
  18. export type AlertType =
  19. | 'issues'
  20. | 'num_errors'
  21. | 'users_experiencing_errors'
  22. | 'throughput'
  23. | 'trans_duration'
  24. | 'apdex'
  25. | 'failure_rate'
  26. | 'lcp'
  27. | 'fid'
  28. | 'cls'
  29. | 'custom'
  30. | 'crash_free_sessions'
  31. | 'crash_free_users';
  32. export enum MEPAlertsQueryType {
  33. ERROR = 0,
  34. PERFORMANCE = 1,
  35. CRASH_RATE = 2,
  36. }
  37. export enum MEPAlertsDataset {
  38. DISCOVER = 'discover',
  39. METRICS = 'metrics',
  40. METRICS_ENHANCED = 'metricsEnhanced',
  41. }
  42. export type MetricAlertType = Exclude<AlertType, 'issues'>;
  43. export const DatasetMEPAlertQueryTypes: Record<Dataset, MEPAlertsQueryType> = {
  44. [Dataset.ERRORS]: MEPAlertsQueryType.ERROR,
  45. [Dataset.TRANSACTIONS]: MEPAlertsQueryType.PERFORMANCE,
  46. [Dataset.GENERIC_METRICS]: MEPAlertsQueryType.PERFORMANCE,
  47. [Dataset.METRICS]: MEPAlertsQueryType.CRASH_RATE,
  48. [Dataset.SESSIONS]: MEPAlertsQueryType.CRASH_RATE,
  49. };
  50. export const AlertWizardAlertNames: Record<AlertType, string> = {
  51. issues: t('Issues'),
  52. num_errors: t('Number of Errors'),
  53. users_experiencing_errors: t('Users Experiencing Errors'),
  54. throughput: t('Throughput'),
  55. trans_duration: t('Transaction Duration'),
  56. apdex: t('Apdex'),
  57. failure_rate: t('Failure Rate'),
  58. lcp: t('Largest Contentful Paint'),
  59. fid: t('First Input Delay'),
  60. cls: t('Cumulative Layout Shift'),
  61. custom: t('Custom Metric'),
  62. crash_free_sessions: t('Crash Free Session Rate'),
  63. crash_free_users: t('Crash Free User Rate'),
  64. };
  65. type AlertWizardCategory = {
  66. categoryHeading: string;
  67. options: AlertType[];
  68. };
  69. export const getAlertWizardCategories = (org: Organization): AlertWizardCategory[] => [
  70. {
  71. categoryHeading: t('Errors'),
  72. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  73. },
  74. ...(org.features.includes('crash-rate-alerts')
  75. ? [
  76. {
  77. categoryHeading: t('Sessions'),
  78. options: ['crash_free_sessions', 'crash_free_users'] as AlertType[],
  79. },
  80. ]
  81. : []),
  82. {
  83. categoryHeading: t('Performance'),
  84. options: [
  85. 'throughput',
  86. 'trans_duration',
  87. 'apdex',
  88. 'failure_rate',
  89. 'lcp',
  90. 'fid',
  91. 'cls',
  92. ],
  93. },
  94. {
  95. categoryHeading: t('Other'),
  96. options: ['custom'],
  97. },
  98. ];
  99. export type WizardRuleTemplate = {
  100. aggregate: string;
  101. dataset: Dataset;
  102. eventTypes: EventTypes;
  103. };
  104. export const AlertWizardRuleTemplates: Record<
  105. MetricAlertType,
  106. Readonly<WizardRuleTemplate>
  107. > = {
  108. num_errors: {
  109. aggregate: 'count()',
  110. dataset: Dataset.ERRORS,
  111. eventTypes: EventTypes.ERROR,
  112. },
  113. users_experiencing_errors: {
  114. aggregate: 'count_unique(user)',
  115. dataset: Dataset.ERRORS,
  116. eventTypes: EventTypes.ERROR,
  117. },
  118. throughput: {
  119. aggregate: 'count()',
  120. dataset: Dataset.TRANSACTIONS,
  121. eventTypes: EventTypes.TRANSACTION,
  122. },
  123. trans_duration: {
  124. aggregate: 'p95(transaction.duration)',
  125. dataset: Dataset.TRANSACTIONS,
  126. eventTypes: EventTypes.TRANSACTION,
  127. },
  128. apdex: {
  129. aggregate: 'apdex(300)',
  130. dataset: Dataset.TRANSACTIONS,
  131. eventTypes: EventTypes.TRANSACTION,
  132. },
  133. failure_rate: {
  134. aggregate: 'failure_rate()',
  135. dataset: Dataset.TRANSACTIONS,
  136. eventTypes: EventTypes.TRANSACTION,
  137. },
  138. lcp: {
  139. aggregate: 'p95(measurements.lcp)',
  140. dataset: Dataset.TRANSACTIONS,
  141. eventTypes: EventTypes.TRANSACTION,
  142. },
  143. fid: {
  144. aggregate: 'p95(measurements.fid)',
  145. dataset: Dataset.TRANSACTIONS,
  146. eventTypes: EventTypes.TRANSACTION,
  147. },
  148. cls: {
  149. aggregate: 'p95(measurements.cls)',
  150. dataset: Dataset.TRANSACTIONS,
  151. eventTypes: EventTypes.TRANSACTION,
  152. },
  153. custom: {
  154. aggregate: 'p95(measurements.fp)',
  155. dataset: Dataset.TRANSACTIONS,
  156. eventTypes: EventTypes.TRANSACTION,
  157. },
  158. crash_free_sessions: {
  159. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  160. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  161. dataset: Dataset.SESSIONS,
  162. eventTypes: EventTypes.SESSION,
  163. },
  164. crash_free_users: {
  165. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  166. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  167. dataset: Dataset.SESSIONS,
  168. eventTypes: EventTypes.USER,
  169. },
  170. };
  171. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  172. export const hidePrimarySelectorSet = new Set<AlertType>([
  173. 'num_errors',
  174. 'users_experiencing_errors',
  175. 'throughput',
  176. 'apdex',
  177. 'failure_rate',
  178. 'crash_free_sessions',
  179. 'crash_free_users',
  180. ]);
  181. export const hideParameterSelectorSet = new Set<AlertType>([
  182. 'trans_duration',
  183. 'lcp',
  184. 'fid',
  185. 'cls',
  186. ]);
  187. const TRANSACTION_SUPPORTED_TAGS = [
  188. FieldKey.RELEASE,
  189. FieldKey.TRANSACTION,
  190. FieldKey.TRANSACTION_OP,
  191. FieldKey.TRANSACTION_STATUS,
  192. FieldKey.HTTP_METHOD,
  193. FieldKey.HTTP_STATUS_CODE,
  194. FieldKey.BROWSER_NAME,
  195. FieldKey.GEO_COUNTRY_CODE,
  196. FieldKey.OS_NAME,
  197. ];
  198. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  199. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  200. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  201. FieldKey.AGE,
  202. FieldKey.ASSIGNED,
  203. FieldKey.ASSIGNED_OR_SUGGESTED,
  204. FieldKey.BOOKMARKS,
  205. FieldKey.DEVICE_MODEL_ID,
  206. FieldKey.EVENT_TIMESTAMP,
  207. FieldKey.EVENT_TYPE,
  208. FieldKey.FIRST_RELEASE,
  209. FieldKey.FIRST_SEEN,
  210. FieldKey.IS,
  211. FieldKey.ISSUE_CATEGORY,
  212. FieldKey.ISSUE_TYPE,
  213. FieldKey.LAST_SEEN,
  214. FieldKey.PLATFORM_NAME,
  215. ...Object.values(WebVital),
  216. ...Object.values(MobileVital),
  217. ...Object.values(ReplayFieldKey),
  218. ...Object.values(ReplayClickFieldKey),
  219. ];
  220. // Some data sets support a very limited number of tags. For these cases,
  221. // define all supported tags explicitly
  222. export function datasetSupportedTags(
  223. dataset: Dataset,
  224. org: Organization
  225. ): TagCollection | undefined {
  226. return mapValues(
  227. {
  228. [Dataset.ERRORS]: undefined,
  229. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  230. ? undefined
  231. : transactionSupportedTags(org),
  232. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  233. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  234. ? undefined
  235. : TRANSACTION_SUPPORTED_TAGS,
  236. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  237. },
  238. value => {
  239. return value ? makeTagCollection(value) : undefined;
  240. }
  241. )[dataset];
  242. }
  243. function transactionSupportedTags(org: Organization) {
  244. if (org.features.includes('on-demand-metrics-extraction')) {
  245. return [...TRANSACTION_SUPPORTED_TAGS, FieldKey.TRANSACTION_DURATION];
  246. }
  247. return TRANSACTION_SUPPORTED_TAGS;
  248. }
  249. // Some data sets support all tags except some. For these cases, define the
  250. // omissions only
  251. export function datasetOmittedTags(
  252. dataset: Dataset,
  253. org: Organization
  254. ):
  255. | Array<
  256. | FieldKey
  257. | WebVital
  258. | MobileVital
  259. | SpanOpBreakdown
  260. | ReplayFieldKey
  261. | ReplayClickFieldKey
  262. >
  263. | undefined {
  264. return {
  265. [Dataset.ERRORS]: [
  266. FieldKey.EVENT_TYPE,
  267. FieldKey.RELEASE_VERSION,
  268. FieldKey.RELEASE_STAGE,
  269. FieldKey.RELEASE_BUILD,
  270. FieldKey.PROJECT,
  271. ...Object.values(WebVital),
  272. ...Object.values(MobileVital),
  273. ...Object.values(SpanOpBreakdown),
  274. FieldKey.TRANSACTION,
  275. FieldKey.TRANSACTION_DURATION,
  276. FieldKey.TRANSACTION_OP,
  277. FieldKey.TRANSACTION_STATUS,
  278. ],
  279. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  280. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  281. : undefined,
  282. [Dataset.METRICS]: undefined,
  283. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  284. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  285. : undefined,
  286. [Dataset.SESSIONS]: undefined,
  287. }[dataset];
  288. }
  289. export function getMEPAlertsDataset(
  290. dataset: Dataset,
  291. newAlert: boolean
  292. ): MEPAlertsDataset {
  293. // Dataset.ERRORS overrides all cases
  294. if (dataset === Dataset.ERRORS) {
  295. return MEPAlertsDataset.DISCOVER;
  296. }
  297. if (newAlert) {
  298. return MEPAlertsDataset.METRICS_ENHANCED;
  299. }
  300. if (dataset === Dataset.GENERIC_METRICS) {
  301. return MEPAlertsDataset.METRICS;
  302. }
  303. return MEPAlertsDataset.DISCOVER;
  304. }