options.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. import {MODULE_TITLE as LLM_MONITORING_MODULE_TITLE} from 'sentry/views/insights/llmMonitoring/settings';
  31. export type AlertType =
  32. | 'issues'
  33. | 'num_errors'
  34. | 'users_experiencing_errors'
  35. | 'throughput'
  36. | 'trans_duration'
  37. | 'apdex'
  38. | 'failure_rate'
  39. | 'lcp'
  40. | 'fid'
  41. | 'cls'
  42. | 'crash_free_sessions'
  43. | 'crash_free_users'
  44. | 'custom_transactions'
  45. | 'custom_metrics'
  46. | 'llm_tokens'
  47. | 'llm_cost'
  48. | 'uptime_monitor'
  49. | 'eap_metrics';
  50. export enum MEPAlertsQueryType {
  51. ERROR = 0,
  52. PERFORMANCE = 1,
  53. CRASH_RATE = 2,
  54. }
  55. export enum MEPAlertsDataset {
  56. DISCOVER = 'discover',
  57. METRICS = 'metrics',
  58. METRICS_ENHANCED = 'metricsEnhanced',
  59. }
  60. export type MetricAlertType = Exclude<AlertType, 'issues' | 'uptime_monitor'>;
  61. export const DatasetMEPAlertQueryTypes: Record<
  62. Exclude<Dataset, Dataset.ISSUE_PLATFORM | Dataset.SESSIONS | Dataset.REPLAYS>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  63. MEPAlertsQueryType
  64. > = {
  65. [Dataset.ERRORS]: MEPAlertsQueryType.ERROR,
  66. [Dataset.TRANSACTIONS]: MEPAlertsQueryType.PERFORMANCE,
  67. [Dataset.GENERIC_METRICS]: MEPAlertsQueryType.PERFORMANCE,
  68. [Dataset.METRICS]: MEPAlertsQueryType.CRASH_RATE,
  69. [Dataset.EVENTS_ANALYTICS_PLATFORM]: MEPAlertsQueryType.PERFORMANCE,
  70. };
  71. export const AlertWizardAlertNames: Record<AlertType, string> = {
  72. issues: t('Issues'),
  73. num_errors: t('Number of Errors'),
  74. users_experiencing_errors: t('Users Experiencing Errors'),
  75. throughput: t('Throughput'),
  76. trans_duration: t('Transaction Duration'),
  77. apdex: t('Apdex'),
  78. failure_rate: t('Failure Rate'),
  79. lcp: t('Largest Contentful Paint'),
  80. fid: t('First Input Delay'),
  81. cls: t('Cumulative Layout Shift'),
  82. custom_metrics: t('Custom Metric'),
  83. custom_transactions: t('Custom Measurement'),
  84. crash_free_sessions: t('Crash Free Session Rate'),
  85. crash_free_users: t('Crash Free User Rate'),
  86. llm_cost: t('LLM cost'),
  87. llm_tokens: t('LLM token usage'),
  88. uptime_monitor: t('Uptime Monitor'),
  89. eap_metrics: t('EAP Metric'),
  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: <FeatureBadge type="experimental" />,
  97. uptime_monitor: <FeatureBadge type="beta" />,
  98. };
  99. type AlertWizardCategory = {
  100. categoryHeading: string;
  101. options: AlertType[];
  102. };
  103. export const getAlertWizardCategories = (org: Organization) => {
  104. const result: AlertWizardCategory[] = [
  105. {
  106. categoryHeading: t('Errors'),
  107. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  108. },
  109. ];
  110. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  111. if (!isSelfHostedErrorsOnly) {
  112. if (org.features.includes('crash-rate-alerts')) {
  113. result.push({
  114. categoryHeading: t('Sessions'),
  115. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  116. });
  117. }
  118. result.push({
  119. categoryHeading: t('Performance'),
  120. options: [
  121. 'throughput',
  122. 'trans_duration',
  123. 'apdex',
  124. 'failure_rate',
  125. 'lcp',
  126. 'fid',
  127. 'cls',
  128. ...(hasCustomMetrics(org) ? (['custom_transactions'] satisfies AlertType[]) : []),
  129. ...(hasEAPAlerts(org) ? ['eap_metrics' as const] : []),
  130. ],
  131. });
  132. if (org.features.includes('insights-addon-modules')) {
  133. result.push({
  134. categoryHeading: LLM_MONITORING_MODULE_TITLE,
  135. options: ['llm_tokens', 'llm_cost'],
  136. });
  137. }
  138. result.push({
  139. categoryHeading: t('Uptime Monitoring'),
  140. options: ['uptime_monitor'],
  141. });
  142. result.push({
  143. categoryHeading: hasCustomMetrics(org) ? t('Metrics') : t('Custom'),
  144. options: [hasCustomMetrics(org) ? 'custom_metrics' : 'custom_transactions'],
  145. });
  146. }
  147. return result;
  148. };
  149. export type WizardRuleTemplate = {
  150. aggregate: string;
  151. dataset: Dataset;
  152. eventTypes: EventTypes;
  153. query?: string;
  154. };
  155. export const AlertWizardRuleTemplates: Record<
  156. MetricAlertType,
  157. Readonly<WizardRuleTemplate>
  158. > = {
  159. num_errors: {
  160. aggregate: 'count()',
  161. dataset: Dataset.ERRORS,
  162. eventTypes: EventTypes.ERROR,
  163. },
  164. users_experiencing_errors: {
  165. aggregate: 'count_unique(user)',
  166. dataset: Dataset.ERRORS,
  167. eventTypes: EventTypes.ERROR,
  168. },
  169. throughput: {
  170. aggregate: 'count()',
  171. dataset: Dataset.TRANSACTIONS,
  172. eventTypes: EventTypes.TRANSACTION,
  173. },
  174. trans_duration: {
  175. aggregate: 'p95(transaction.duration)',
  176. dataset: Dataset.TRANSACTIONS,
  177. eventTypes: EventTypes.TRANSACTION,
  178. },
  179. apdex: {
  180. aggregate: 'apdex(300)',
  181. dataset: Dataset.TRANSACTIONS,
  182. eventTypes: EventTypes.TRANSACTION,
  183. },
  184. failure_rate: {
  185. aggregate: 'failure_rate()',
  186. dataset: Dataset.TRANSACTIONS,
  187. eventTypes: EventTypes.TRANSACTION,
  188. },
  189. lcp: {
  190. aggregate: 'p95(measurements.lcp)',
  191. dataset: Dataset.TRANSACTIONS,
  192. eventTypes: EventTypes.TRANSACTION,
  193. },
  194. fid: {
  195. aggregate: 'p95(measurements.fid)',
  196. dataset: Dataset.TRANSACTIONS,
  197. eventTypes: EventTypes.TRANSACTION,
  198. },
  199. cls: {
  200. aggregate: 'p95(measurements.cls)',
  201. dataset: Dataset.TRANSACTIONS,
  202. eventTypes: EventTypes.TRANSACTION,
  203. },
  204. custom_transactions: {
  205. aggregate: 'p95(measurements.fp)',
  206. dataset: Dataset.GENERIC_METRICS,
  207. eventTypes: EventTypes.TRANSACTION,
  208. },
  209. custom_metrics: {
  210. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  211. dataset: Dataset.GENERIC_METRICS,
  212. eventTypes: EventTypes.TRANSACTION,
  213. },
  214. llm_tokens: {
  215. aggregate: 'sum(ai.total_tokens.used)',
  216. dataset: Dataset.GENERIC_METRICS,
  217. eventTypes: EventTypes.TRANSACTION,
  218. },
  219. llm_cost: {
  220. aggregate: 'sum(ai.total_cost)',
  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. return mapValues(
  302. {
  303. [Dataset.ERRORS]: ERROR_SUPPORTED_TAGS,
  304. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  305. ? undefined
  306. : transactionSupportedTags(org),
  307. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  308. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  309. ? undefined
  310. : transactionSupportedTags(org),
  311. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  312. },
  313. value => {
  314. return value ? makeTagCollection(value) : undefined;
  315. }
  316. )[dataset];
  317. }
  318. function transactionSupportedTags(org: Organization) {
  319. if (shouldShowOnDemandMetricAlertUI(org)) {
  320. // on-demand metrics support all tags, except the ones defined in ommited tags
  321. return undefined;
  322. }
  323. return TRANSACTION_SUPPORTED_TAGS;
  324. }
  325. // Some data sets support all tags except some. For these cases, define the
  326. // omissions only
  327. export function datasetOmittedTags(
  328. dataset: Dataset,
  329. org: Organization
  330. ):
  331. | Array<
  332. | FieldKey
  333. | WebVital
  334. | MobileVital
  335. | SpanOpBreakdown
  336. | ReplayFieldKey
  337. | ReplayClickFieldKey
  338. >
  339. | undefined {
  340. return {
  341. [Dataset.ERRORS]: [
  342. FieldKey.EVENT_TYPE,
  343. FieldKey.RELEASE_VERSION,
  344. FieldKey.RELEASE_STAGE,
  345. FieldKey.RELEASE_BUILD,
  346. FieldKey.PROJECT,
  347. ...Object.values(WebVital),
  348. ...Object.values(MobileVital),
  349. ...Object.values(SpanOpBreakdown),
  350. FieldKey.TRANSACTION,
  351. FieldKey.TRANSACTION_DURATION,
  352. FieldKey.TRANSACTION_OP,
  353. FieldKey.TRANSACTION_STATUS,
  354. ],
  355. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  356. [Dataset.METRICS]: undefined,
  357. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  358. [Dataset.SESSIONS]: undefined,
  359. }[dataset];
  360. }
  361. function transactionOmittedTags(org: Organization) {
  362. if (shouldShowOnDemandMetricAlertUI(org)) {
  363. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  364. }
  365. return org.features.includes('alert-allow-indexed')
  366. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  367. : undefined;
  368. }
  369. export function getSupportedAndOmittedTags(
  370. dataset: Dataset,
  371. organization: Organization
  372. ): {
  373. omitTags?: string[];
  374. supportedTags?: TagCollection;
  375. } {
  376. const omitTags = datasetOmittedTags(dataset, organization);
  377. const supportedTags = datasetSupportedTags(dataset, organization);
  378. const result = {omitTags, supportedTags};
  379. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  380. return Object.keys({omitTags, supportedTags}).reduce<{
  381. omitTags?: string[];
  382. supportedTags?: TagCollection;
  383. }>((acc, key) => {
  384. if (result[key] !== undefined) {
  385. acc[key] = result[key];
  386. }
  387. return acc;
  388. }, {});
  389. }
  390. export function getMEPAlertsDataset(
  391. dataset: Dataset,
  392. newAlert: boolean
  393. ): MEPAlertsDataset {
  394. // Dataset.ERRORS overrides all cases
  395. if (dataset === Dataset.ERRORS) {
  396. return MEPAlertsDataset.DISCOVER;
  397. }
  398. if (newAlert) {
  399. return MEPAlertsDataset.METRICS_ENHANCED;
  400. }
  401. if (dataset === Dataset.GENERIC_METRICS) {
  402. return MEPAlertsDataset.METRICS_ENHANCED;
  403. }
  404. return MEPAlertsDataset.DISCOVER;
  405. }