options.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 {hasDdmAlertsSupport} 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 Metric'),
  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. ...(hasDdmAlertsSupport(org)
  99. ? (['custom_transactions'] satisfies AlertType[])
  100. : []),
  101. ],
  102. },
  103. {
  104. categoryHeading: hasDdmAlertsSupport(org) ? t('Metrics') : t('Custom'),
  105. options: [hasDdmAlertsSupport(org) ? 'custom_metrics' : 'custom_transactions'],
  106. },
  107. ];
  108. export type WizardRuleTemplate = {
  109. aggregate: string;
  110. dataset: Dataset;
  111. eventTypes: EventTypes;
  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]: undefined,
  243. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  244. ? undefined
  245. : transactionSupportedTags(org),
  246. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  247. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  248. ? undefined
  249. : transactionSupportedTags(org),
  250. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  251. },
  252. value => {
  253. return value ? makeTagCollection(value) : undefined;
  254. }
  255. )[dataset];
  256. }
  257. function transactionSupportedTags(org: Organization) {
  258. if (shouldShowOnDemandMetricAlertUI(org)) {
  259. // on-demand metrics support all tags, except the ones defined in ommited tags
  260. return undefined;
  261. }
  262. return TRANSACTION_SUPPORTED_TAGS;
  263. }
  264. // Some data sets support all tags except some. For these cases, define the
  265. // omissions only
  266. export function datasetOmittedTags(
  267. dataset: Dataset,
  268. org: Organization
  269. ):
  270. | Array<
  271. | FieldKey
  272. | WebVital
  273. | MobileVital
  274. | SpanOpBreakdown
  275. | ReplayFieldKey
  276. | ReplayClickFieldKey
  277. >
  278. | undefined {
  279. return {
  280. [Dataset.ERRORS]: [
  281. FieldKey.EVENT_TYPE,
  282. FieldKey.RELEASE_VERSION,
  283. FieldKey.RELEASE_STAGE,
  284. FieldKey.RELEASE_BUILD,
  285. FieldKey.PROJECT,
  286. ...Object.values(WebVital),
  287. ...Object.values(MobileVital),
  288. ...Object.values(SpanOpBreakdown),
  289. FieldKey.TRANSACTION,
  290. FieldKey.TRANSACTION_DURATION,
  291. FieldKey.TRANSACTION_OP,
  292. FieldKey.TRANSACTION_STATUS,
  293. ],
  294. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  295. [Dataset.METRICS]: undefined,
  296. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  297. [Dataset.SESSIONS]: undefined,
  298. }[dataset];
  299. }
  300. function transactionOmittedTags(org: Organization) {
  301. if (shouldShowOnDemandMetricAlertUI(org)) {
  302. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  303. }
  304. return org.features.includes('alert-allow-indexed')
  305. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  306. : undefined;
  307. }
  308. export function getSupportedAndOmittedTags(dataset: Dataset, organization: Organization) {
  309. const omitTags = datasetOmittedTags(dataset, organization);
  310. const supportedTags = datasetSupportedTags(dataset, organization);
  311. const result = {omitTags, supportedTags};
  312. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  313. return Object.keys({omitTags, supportedTags}).reduce((acc, key) => {
  314. if (result[key] !== undefined) {
  315. acc[key] = result[key];
  316. }
  317. return acc;
  318. }, {});
  319. }
  320. export function getMEPAlertsDataset(
  321. dataset: Dataset,
  322. newAlert: boolean
  323. ): MEPAlertsDataset {
  324. // Dataset.ERRORS overrides all cases
  325. if (dataset === Dataset.ERRORS) {
  326. return MEPAlertsDataset.DISCOVER;
  327. }
  328. if (newAlert) {
  329. return MEPAlertsDataset.METRICS_ENHANCED;
  330. }
  331. if (dataset === Dataset.GENERIC_METRICS) {
  332. return MEPAlertsDataset.METRICS_ENHANCED;
  333. }
  334. return MEPAlertsDataset.DISCOVER;
  335. }