options.tsx 9.8 KB

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