options.tsx 12 KB

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