options.tsx 9.9 KB

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