options.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. query?: string;
  113. };
  114. export const AlertWizardRuleTemplates: Record<
  115. MetricAlertType,
  116. Readonly<WizardRuleTemplate>
  117. > = {
  118. num_errors: {
  119. aggregate: 'count()',
  120. dataset: Dataset.ERRORS,
  121. eventTypes: EventTypes.ERROR,
  122. },
  123. users_experiencing_errors: {
  124. aggregate: 'count_unique(user)',
  125. dataset: Dataset.ERRORS,
  126. eventTypes: EventTypes.ERROR,
  127. },
  128. throughput: {
  129. aggregate: 'count()',
  130. dataset: Dataset.TRANSACTIONS,
  131. eventTypes: EventTypes.TRANSACTION,
  132. },
  133. trans_duration: {
  134. aggregate: 'p95(transaction.duration)',
  135. dataset: Dataset.TRANSACTIONS,
  136. eventTypes: EventTypes.TRANSACTION,
  137. },
  138. apdex: {
  139. aggregate: 'apdex(300)',
  140. dataset: Dataset.TRANSACTIONS,
  141. eventTypes: EventTypes.TRANSACTION,
  142. },
  143. failure_rate: {
  144. aggregate: 'failure_rate()',
  145. dataset: Dataset.TRANSACTIONS,
  146. eventTypes: EventTypes.TRANSACTION,
  147. },
  148. lcp: {
  149. aggregate: 'p95(measurements.lcp)',
  150. dataset: Dataset.TRANSACTIONS,
  151. eventTypes: EventTypes.TRANSACTION,
  152. },
  153. fid: {
  154. aggregate: 'p95(measurements.fid)',
  155. dataset: Dataset.TRANSACTIONS,
  156. eventTypes: EventTypes.TRANSACTION,
  157. },
  158. cls: {
  159. aggregate: 'p95(measurements.cls)',
  160. dataset: Dataset.TRANSACTIONS,
  161. eventTypes: EventTypes.TRANSACTION,
  162. },
  163. custom_transactions: {
  164. aggregate: 'p95(measurements.fp)',
  165. dataset: Dataset.GENERIC_METRICS,
  166. eventTypes: EventTypes.TRANSACTION,
  167. },
  168. custom_metrics: {
  169. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  170. dataset: Dataset.GENERIC_METRICS,
  171. eventTypes: EventTypes.TRANSACTION,
  172. },
  173. crash_free_sessions: {
  174. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  175. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  176. dataset: Dataset.SESSIONS,
  177. eventTypes: EventTypes.SESSION,
  178. },
  179. crash_free_users: {
  180. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  181. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  182. dataset: Dataset.SESSIONS,
  183. eventTypes: EventTypes.USER,
  184. },
  185. };
  186. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  187. export const hidePrimarySelectorSet = new Set<AlertType>([
  188. 'num_errors',
  189. 'users_experiencing_errors',
  190. 'throughput',
  191. 'apdex',
  192. 'failure_rate',
  193. 'crash_free_sessions',
  194. 'crash_free_users',
  195. ]);
  196. export const hideParameterSelectorSet = new Set<AlertType>([
  197. 'trans_duration',
  198. 'lcp',
  199. 'fid',
  200. 'cls',
  201. ]);
  202. const TRANSACTION_SUPPORTED_TAGS = [
  203. FieldKey.RELEASE,
  204. FieldKey.TRANSACTION,
  205. FieldKey.TRANSACTION_OP,
  206. FieldKey.TRANSACTION_STATUS,
  207. FieldKey.HTTP_METHOD,
  208. FieldKey.HTTP_STATUS_CODE,
  209. FieldKey.BROWSER_NAME,
  210. FieldKey.GEO_COUNTRY_CODE,
  211. FieldKey.OS_NAME,
  212. ];
  213. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  214. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  215. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  216. FieldKey.AGE,
  217. FieldKey.ASSIGNED,
  218. FieldKey.ASSIGNED_OR_SUGGESTED,
  219. FieldKey.BOOKMARKS,
  220. FieldKey.DEVICE_MODEL_ID,
  221. FieldKey.EVENT_TIMESTAMP,
  222. FieldKey.EVENT_TYPE,
  223. FieldKey.FIRST_RELEASE,
  224. FieldKey.FIRST_SEEN,
  225. FieldKey.IS,
  226. FieldKey.ISSUE_CATEGORY,
  227. FieldKey.ISSUE_TYPE,
  228. FieldKey.LAST_SEEN,
  229. FieldKey.PLATFORM_NAME,
  230. ...Object.values(WebVital),
  231. ...Object.values(MobileVital),
  232. ...Object.values(ReplayFieldKey),
  233. ...Object.values(ReplayClickFieldKey),
  234. ];
  235. // Some data sets support a very limited number of tags. For these cases,
  236. // define all supported tags explicitly
  237. export function datasetSupportedTags(
  238. dataset: Dataset,
  239. org: Organization
  240. ): TagCollection | undefined {
  241. return mapValues(
  242. {
  243. [Dataset.ERRORS]: 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(dataset: Dataset, organization: Organization) {
  310. const omitTags = datasetOmittedTags(dataset, organization);
  311. const supportedTags = datasetSupportedTags(dataset, organization);
  312. const result = {omitTags, supportedTags};
  313. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  314. return Object.keys({omitTags, supportedTags}).reduce((acc, key) => {
  315. if (result[key] !== undefined) {
  316. acc[key] = result[key];
  317. }
  318. return acc;
  319. }, {});
  320. }
  321. export function getMEPAlertsDataset(
  322. dataset: Dataset,
  323. newAlert: boolean
  324. ): MEPAlertsDataset {
  325. // Dataset.ERRORS overrides all cases
  326. if (dataset === Dataset.ERRORS) {
  327. return MEPAlertsDataset.DISCOVER;
  328. }
  329. if (newAlert) {
  330. return MEPAlertsDataset.METRICS_ENHANCED;
  331. }
  332. if (dataset === Dataset.GENERIC_METRICS) {
  333. return MEPAlertsDataset.METRICS_ENHANCED;
  334. }
  335. return MEPAlertsDataset.DISCOVER;
  336. }