options.tsx 12 KB

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