options.tsx 12 KB

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