options.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import mapValues from 'lodash/mapValues';
  2. import FeatureBadge from 'sentry/components/badge/featureBadge';
  3. import {STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS} from 'sentry/components/events/searchBarFieldConstants';
  4. import {t} from 'sentry/locale';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import type {TagCollection} from 'sentry/types/group';
  7. import type {Organization} from 'sentry/types/organization';
  8. import {
  9. FieldKey,
  10. makeTagCollection,
  11. MobileVital,
  12. ReplayClickFieldKey,
  13. ReplayFieldKey,
  14. SpanOpBreakdown,
  15. WebVital,
  16. } from 'sentry/utils/fields';
  17. import {hasCustomMetrics} from 'sentry/utils/metrics/features';
  18. import {
  19. DEFAULT_EAP_METRICS_ALERT_FIELD,
  20. DEFAULT_METRIC_ALERT_FIELD,
  21. } from 'sentry/utils/metrics/mri';
  22. import {ON_DEMAND_METRICS_UNSUPPORTED_TAGS} from 'sentry/utils/onDemandMetrics/constants';
  23. import {shouldShowOnDemandMetricAlertUI} from 'sentry/utils/onDemandMetrics/features';
  24. import {
  25. Dataset,
  26. EventTypes,
  27. SessionsAggregate,
  28. } from 'sentry/views/alerts/rules/metric/types';
  29. import {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  30. export type AlertType =
  31. | 'issues'
  32. | 'num_errors'
  33. | 'users_experiencing_errors'
  34. | 'throughput'
  35. | 'trans_duration'
  36. | 'apdex'
  37. | 'failure_rate'
  38. | 'lcp'
  39. | 'fid'
  40. | 'cls'
  41. | 'crash_free_sessions'
  42. | 'crash_free_users'
  43. | 'custom_transactions'
  44. | 'custom_metrics'
  45. | 'uptime_monitor'
  46. | 'crons_monitor'
  47. | 'eap_metrics';
  48. export enum MEPAlertsQueryType {
  49. ERROR = 0,
  50. PERFORMANCE = 1,
  51. CRASH_RATE = 2,
  52. }
  53. export enum MEPAlertsDataset {
  54. DISCOVER = 'discover',
  55. METRICS = 'metrics',
  56. METRICS_ENHANCED = 'metricsEnhanced',
  57. }
  58. export type MetricAlertType = Exclude<
  59. AlertType,
  60. 'issues' | 'uptime_monitor' | 'crons_monitor'
  61. >;
  62. export const DatasetMEPAlertQueryTypes: Record<
  63. Exclude<Dataset, Dataset.ISSUE_PLATFORM | Dataset.SESSIONS | Dataset.REPLAYS>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  64. MEPAlertsQueryType
  65. > = {
  66. [Dataset.ERRORS]: MEPAlertsQueryType.ERROR,
  67. [Dataset.TRANSACTIONS]: MEPAlertsQueryType.PERFORMANCE,
  68. [Dataset.GENERIC_METRICS]: MEPAlertsQueryType.PERFORMANCE,
  69. [Dataset.METRICS]: MEPAlertsQueryType.CRASH_RATE,
  70. [Dataset.EVENTS_ANALYTICS_PLATFORM]: MEPAlertsQueryType.PERFORMANCE,
  71. };
  72. export const AlertWizardAlertNames: Record<AlertType, string> = {
  73. issues: t('Issues'),
  74. num_errors: t('Number of Errors'),
  75. users_experiencing_errors: t('Users Experiencing Errors'),
  76. throughput: t('Throughput'),
  77. trans_duration: t('Transaction Duration'),
  78. apdex: t('Apdex'),
  79. failure_rate: t('Failure Rate'),
  80. lcp: t('Largest Contentful Paint'),
  81. fid: t('First Input Delay'),
  82. cls: t('Cumulative Layout Shift'),
  83. custom_metrics: t('Custom Metric'),
  84. custom_transactions: t('Custom Measurement'),
  85. crash_free_sessions: t('Crash Free Session Rate'),
  86. crash_free_users: t('Crash Free User Rate'),
  87. uptime_monitor: t('Uptime Monitor'),
  88. eap_metrics: t('Spans'),
  89. crons_monitor: t('Cron Monitor'),
  90. };
  91. /**
  92. * Additional elements to render after the name of the alert rule type. Useful
  93. * for adding feature badges or other call-outs for newer alert types.
  94. */
  95. export const AlertWizardExtraContent: Partial<Record<AlertType, React.ReactNode>> = {
  96. eap_metrics: (
  97. <FeatureBadge
  98. type="beta"
  99. title={t('This feature is available for early adopters and the UX may change')}
  100. />
  101. ),
  102. uptime_monitor: <FeatureBadge type="beta" />,
  103. };
  104. type AlertWizardCategory = {
  105. categoryHeading: string;
  106. options: AlertType[];
  107. };
  108. export const getAlertWizardCategories = (org: Organization) => {
  109. const result: AlertWizardCategory[] = [
  110. {
  111. categoryHeading: t('Errors'),
  112. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  113. },
  114. ];
  115. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  116. if (!isSelfHostedErrorsOnly) {
  117. if (org.features.includes('crash-rate-alerts')) {
  118. result.push({
  119. categoryHeading: t('Sessions'),
  120. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  121. });
  122. }
  123. result.push({
  124. categoryHeading: t('Performance'),
  125. options: [
  126. 'throughput',
  127. 'trans_duration',
  128. 'apdex',
  129. 'failure_rate',
  130. 'lcp',
  131. 'fid',
  132. 'cls',
  133. ...(hasCustomMetrics(org) ? (['custom_transactions'] satisfies AlertType[]) : []),
  134. ...(hasEAPAlerts(org) ? ['eap_metrics' as const] : []),
  135. ],
  136. });
  137. if (
  138. org.features.includes('uptime') &&
  139. !org.features.includes('uptime-create-disabled')
  140. ) {
  141. result.push({
  142. categoryHeading: t('Uptime Monitoring'),
  143. options: ['uptime_monitor'],
  144. });
  145. }
  146. if (org.features.includes('insights-crons')) {
  147. result.push({
  148. categoryHeading: t('Cron Monitoring'),
  149. options: ['crons_monitor'],
  150. });
  151. }
  152. result.push({
  153. categoryHeading: hasCustomMetrics(org) ? t('Metrics') : t('Custom'),
  154. options: [hasCustomMetrics(org) ? 'custom_metrics' : 'custom_transactions'],
  155. });
  156. }
  157. return result;
  158. };
  159. export type WizardRuleTemplate = {
  160. aggregate: string;
  161. dataset: Dataset;
  162. eventTypes: EventTypes;
  163. query?: string;
  164. };
  165. export const AlertWizardRuleTemplates: Record<
  166. MetricAlertType,
  167. Readonly<WizardRuleTemplate>
  168. > = {
  169. num_errors: {
  170. aggregate: 'count()',
  171. dataset: Dataset.ERRORS,
  172. eventTypes: EventTypes.ERROR,
  173. },
  174. users_experiencing_errors: {
  175. aggregate: 'count_unique(user)',
  176. dataset: Dataset.ERRORS,
  177. eventTypes: EventTypes.ERROR,
  178. },
  179. throughput: {
  180. aggregate: 'count()',
  181. dataset: Dataset.TRANSACTIONS,
  182. eventTypes: EventTypes.TRANSACTION,
  183. },
  184. trans_duration: {
  185. aggregate: 'p95(transaction.duration)',
  186. dataset: Dataset.TRANSACTIONS,
  187. eventTypes: EventTypes.TRANSACTION,
  188. },
  189. apdex: {
  190. aggregate: 'apdex(300)',
  191. dataset: Dataset.TRANSACTIONS,
  192. eventTypes: EventTypes.TRANSACTION,
  193. },
  194. failure_rate: {
  195. aggregate: 'failure_rate()',
  196. dataset: Dataset.TRANSACTIONS,
  197. eventTypes: EventTypes.TRANSACTION,
  198. },
  199. lcp: {
  200. aggregate: 'p95(measurements.lcp)',
  201. dataset: Dataset.TRANSACTIONS,
  202. eventTypes: EventTypes.TRANSACTION,
  203. },
  204. fid: {
  205. aggregate: 'p95(measurements.fid)',
  206. dataset: Dataset.TRANSACTIONS,
  207. eventTypes: EventTypes.TRANSACTION,
  208. },
  209. cls: {
  210. aggregate: 'p95(measurements.cls)',
  211. dataset: Dataset.TRANSACTIONS,
  212. eventTypes: EventTypes.TRANSACTION,
  213. },
  214. custom_transactions: {
  215. aggregate: 'p95(measurements.fp)',
  216. dataset: Dataset.GENERIC_METRICS,
  217. eventTypes: EventTypes.TRANSACTION,
  218. },
  219. custom_metrics: {
  220. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  221. dataset: Dataset.GENERIC_METRICS,
  222. eventTypes: EventTypes.TRANSACTION,
  223. },
  224. crash_free_sessions: {
  225. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  226. dataset: Dataset.METRICS,
  227. eventTypes: EventTypes.SESSION,
  228. },
  229. crash_free_users: {
  230. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  231. dataset: Dataset.METRICS,
  232. eventTypes: EventTypes.USER,
  233. },
  234. eap_metrics: {
  235. aggregate: DEFAULT_EAP_METRICS_ALERT_FIELD,
  236. dataset: Dataset.EVENTS_ANALYTICS_PLATFORM,
  237. eventTypes: EventTypes.TRANSACTION,
  238. },
  239. };
  240. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  241. export const hidePrimarySelectorSet = new Set<AlertType>([
  242. 'num_errors',
  243. 'users_experiencing_errors',
  244. 'throughput',
  245. 'apdex',
  246. 'failure_rate',
  247. 'crash_free_sessions',
  248. 'crash_free_users',
  249. ]);
  250. export const hideParameterSelectorSet = new Set<AlertType>([
  251. 'trans_duration',
  252. 'lcp',
  253. 'fid',
  254. 'cls',
  255. ]);
  256. const TRANSACTION_SUPPORTED_TAGS = [
  257. FieldKey.RELEASE,
  258. FieldKey.TRANSACTION,
  259. FieldKey.TRANSACTION_OP,
  260. FieldKey.TRANSACTION_STATUS,
  261. FieldKey.HTTP_METHOD,
  262. FieldKey.HTTP_STATUS_CODE,
  263. FieldKey.BROWSER_NAME,
  264. FieldKey.GEO_COUNTRY_CODE,
  265. FieldKey.OS_NAME,
  266. ];
  267. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  268. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  269. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  270. FieldKey.AGE,
  271. FieldKey.ASSIGNED,
  272. FieldKey.ASSIGNED_OR_SUGGESTED,
  273. FieldKey.BOOKMARKS,
  274. FieldKey.DEVICE_MODEL_ID,
  275. FieldKey.EVENT_TIMESTAMP,
  276. FieldKey.EVENT_TYPE,
  277. FieldKey.FIRST_RELEASE,
  278. FieldKey.FIRST_SEEN,
  279. FieldKey.IS,
  280. FieldKey.ISSUE_CATEGORY,
  281. FieldKey.ISSUE_TYPE,
  282. FieldKey.LAST_SEEN,
  283. FieldKey.PLATFORM_NAME,
  284. ...Object.values(WebVital),
  285. ...Object.values(MobileVital),
  286. ...Object.values(ReplayFieldKey),
  287. ...Object.values(ReplayClickFieldKey),
  288. ];
  289. const ERROR_SUPPORTED_TAGS = [
  290. FieldKey.IS,
  291. ...Object.keys(STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS).map(
  292. key => key as FieldKey
  293. ),
  294. ];
  295. // Some data sets support a very limited number of tags. For these cases,
  296. // define all supported tags explicitly
  297. export function datasetSupportedTags(
  298. dataset: Dataset,
  299. org: Organization
  300. ): TagCollection | undefined {
  301. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  302. return mapValues(
  303. {
  304. [Dataset.ERRORS]: ERROR_SUPPORTED_TAGS,
  305. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  306. ? undefined
  307. : transactionSupportedTags(org),
  308. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  309. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  310. ? undefined
  311. : transactionSupportedTags(org),
  312. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  313. },
  314. value => {
  315. return value ? makeTagCollection(value) : undefined;
  316. }
  317. )[dataset];
  318. }
  319. function transactionSupportedTags(org: Organization) {
  320. if (shouldShowOnDemandMetricAlertUI(org)) {
  321. // on-demand metrics support all tags, except the ones defined in ommited tags
  322. return undefined;
  323. }
  324. return TRANSACTION_SUPPORTED_TAGS;
  325. }
  326. // Some data sets support all tags except some. For these cases, define the
  327. // omissions only
  328. export function datasetOmittedTags(
  329. dataset: Dataset,
  330. org: Organization
  331. ):
  332. | Array<
  333. | FieldKey
  334. | WebVital
  335. | MobileVital
  336. | SpanOpBreakdown
  337. | ReplayFieldKey
  338. | ReplayClickFieldKey
  339. >
  340. | undefined {
  341. // @ts-expect-error TS(2339): Property 'events_analytics_platform' does not exis... Remove this comment to see the full error message
  342. return {
  343. [Dataset.ERRORS]: [
  344. FieldKey.EVENT_TYPE,
  345. FieldKey.RELEASE_VERSION,
  346. FieldKey.RELEASE_STAGE,
  347. FieldKey.RELEASE_BUILD,
  348. FieldKey.PROJECT,
  349. ...Object.values(WebVital),
  350. ...Object.values(MobileVital),
  351. ...Object.values(SpanOpBreakdown),
  352. FieldKey.TRANSACTION,
  353. FieldKey.TRANSACTION_DURATION,
  354. FieldKey.TRANSACTION_OP,
  355. FieldKey.TRANSACTION_STATUS,
  356. ],
  357. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  358. [Dataset.METRICS]: undefined,
  359. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  360. [Dataset.SESSIONS]: undefined,
  361. }[dataset];
  362. }
  363. function transactionOmittedTags(org: Organization) {
  364. if (shouldShowOnDemandMetricAlertUI(org)) {
  365. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  366. }
  367. return org.features.includes('alert-allow-indexed')
  368. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  369. : undefined;
  370. }
  371. export function getSupportedAndOmittedTags(
  372. dataset: Dataset,
  373. organization: Organization
  374. ): {
  375. omitTags?: string[];
  376. supportedTags?: TagCollection;
  377. } {
  378. const omitTags = datasetOmittedTags(dataset, organization);
  379. const supportedTags = datasetSupportedTags(dataset, organization);
  380. const result = {omitTags, supportedTags};
  381. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  382. return Object.keys({omitTags, supportedTags}).reduce<{
  383. omitTags?: string[];
  384. supportedTags?: TagCollection;
  385. }>((acc, key) => {
  386. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  387. if (result[key] !== undefined) {
  388. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  389. acc[key] = result[key];
  390. }
  391. return acc;
  392. }, {});
  393. }
  394. export function getMEPAlertsDataset(
  395. dataset: Dataset,
  396. newAlert: boolean
  397. ): MEPAlertsDataset {
  398. // Dataset.ERRORS overrides all cases
  399. if (dataset === Dataset.ERRORS) {
  400. return MEPAlertsDataset.DISCOVER;
  401. }
  402. if (newAlert) {
  403. return MEPAlertsDataset.METRICS_ENHANCED;
  404. }
  405. if (dataset === Dataset.GENERIC_METRICS) {
  406. return MEPAlertsDataset.METRICS_ENHANCED;
  407. }
  408. return MEPAlertsDataset.DISCOVER;
  409. }