options.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import mapValues from 'lodash/mapValues';
  2. import {STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS} from 'sentry/components/events/searchBarFieldConstants';
  3. import {t} from 'sentry/locale';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import type {TagCollection} from 'sentry/types/group';
  6. import type {Organization} from 'sentry/types/organization';
  7. import {
  8. FieldKey,
  9. makeTagCollection,
  10. MobileVital,
  11. ReplayClickFieldKey,
  12. ReplayFieldKey,
  13. SpanOpBreakdown,
  14. WebVital,
  15. } from 'sentry/utils/fields';
  16. import {
  17. hasCustomMetrics,
  18. hasCustomMetricsExtractionRules,
  19. } from 'sentry/utils/metrics/features';
  20. import {
  21. DEFAULT_INSIGHTS_METRICS_ALERT_FIELD,
  22. DEFAULT_METRIC_ALERT_FIELD,
  23. DEFAULT_SPAN_METRIC_ALERT_FIELD,
  24. } from 'sentry/utils/metrics/mri';
  25. import {ON_DEMAND_METRICS_UNSUPPORTED_TAGS} from 'sentry/utils/onDemandMetrics/constants';
  26. import {shouldShowOnDemandMetricAlertUI} from 'sentry/utils/onDemandMetrics/features';
  27. import {
  28. Dataset,
  29. EventTypes,
  30. SessionsAggregate,
  31. } from 'sentry/views/alerts/rules/metric/types';
  32. import {hasInsightsAlerts} from 'sentry/views/insights/common/utils/hasInsightsAlerts';
  33. import {MODULE_TITLE as LLM_MONITORING_MODULE_TITLE} from 'sentry/views/insights/llmMonitoring/settings';
  34. export type AlertType =
  35. | 'issues'
  36. | 'num_errors'
  37. | 'users_experiencing_errors'
  38. | 'throughput'
  39. | 'trans_duration'
  40. | 'apdex'
  41. | 'failure_rate'
  42. | 'lcp'
  43. | 'fid'
  44. | 'cls'
  45. | 'crash_free_sessions'
  46. | 'crash_free_users'
  47. | 'custom_transactions'
  48. | 'custom_metrics'
  49. | 'span_metrics'
  50. | 'llm_tokens'
  51. | 'llm_cost'
  52. | 'insights_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'>;
  64. export const DatasetMEPAlertQueryTypes: Record<
  65. Exclude<Dataset, 'search_issues' | Dataset.SESSIONS>, // 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. };
  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. span_metrics: t('Span 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. };
  93. type AlertWizardCategory = {
  94. categoryHeading: string;
  95. options: AlertType[];
  96. };
  97. export const getAlertWizardCategories = (org: Organization) => {
  98. const result: AlertWizardCategory[] = [
  99. {
  100. categoryHeading: t('Errors'),
  101. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  102. },
  103. ];
  104. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  105. if (!isSelfHostedErrorsOnly) {
  106. if (org.features.includes('crash-rate-alerts')) {
  107. result.push({
  108. categoryHeading: t('Sessions'),
  109. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  110. });
  111. }
  112. result.push({
  113. categoryHeading: t('Performance'),
  114. options: [
  115. 'throughput',
  116. 'trans_duration',
  117. 'apdex',
  118. 'failure_rate',
  119. 'lcp',
  120. 'fid',
  121. 'cls',
  122. ...(hasCustomMetrics(org) ? (['custom_transactions'] satisfies AlertType[]) : []),
  123. ],
  124. });
  125. if (org.features.includes('insights-addon-modules')) {
  126. result.push({
  127. categoryHeading: LLM_MONITORING_MODULE_TITLE,
  128. options: ['llm_tokens', 'llm_cost'],
  129. });
  130. }
  131. result.push({
  132. categoryHeading: hasCustomMetrics(org) ? t('Metrics') : t('Custom'),
  133. options: [
  134. hasCustomMetrics(org) ? 'custom_metrics' : 'custom_transactions',
  135. ...(hasCustomMetricsExtractionRules(org) ? ['span_metrics' as const] : []),
  136. ...(hasInsightsAlerts(org) ? ['insights_metrics' as const] : []),
  137. ],
  138. });
  139. }
  140. return result;
  141. };
  142. export type WizardRuleTemplate = {
  143. aggregate: string;
  144. dataset: Dataset;
  145. eventTypes: EventTypes;
  146. query?: string;
  147. };
  148. export const AlertWizardRuleTemplates: Record<
  149. MetricAlertType,
  150. Readonly<WizardRuleTemplate>
  151. > = {
  152. num_errors: {
  153. aggregate: 'count()',
  154. dataset: Dataset.ERRORS,
  155. eventTypes: EventTypes.ERROR,
  156. },
  157. users_experiencing_errors: {
  158. aggregate: 'count_unique(user)',
  159. dataset: Dataset.ERRORS,
  160. eventTypes: EventTypes.ERROR,
  161. },
  162. throughput: {
  163. aggregate: 'count()',
  164. dataset: Dataset.TRANSACTIONS,
  165. eventTypes: EventTypes.TRANSACTION,
  166. },
  167. trans_duration: {
  168. aggregate: 'p95(transaction.duration)',
  169. dataset: Dataset.TRANSACTIONS,
  170. eventTypes: EventTypes.TRANSACTION,
  171. },
  172. apdex: {
  173. aggregate: 'apdex(300)',
  174. dataset: Dataset.TRANSACTIONS,
  175. eventTypes: EventTypes.TRANSACTION,
  176. },
  177. failure_rate: {
  178. aggregate: 'failure_rate()',
  179. dataset: Dataset.TRANSACTIONS,
  180. eventTypes: EventTypes.TRANSACTION,
  181. },
  182. lcp: {
  183. aggregate: 'p95(measurements.lcp)',
  184. dataset: Dataset.TRANSACTIONS,
  185. eventTypes: EventTypes.TRANSACTION,
  186. },
  187. fid: {
  188. aggregate: 'p95(measurements.fid)',
  189. dataset: Dataset.TRANSACTIONS,
  190. eventTypes: EventTypes.TRANSACTION,
  191. },
  192. cls: {
  193. aggregate: 'p95(measurements.cls)',
  194. dataset: Dataset.TRANSACTIONS,
  195. eventTypes: EventTypes.TRANSACTION,
  196. },
  197. custom_transactions: {
  198. aggregate: 'p95(measurements.fp)',
  199. dataset: Dataset.GENERIC_METRICS,
  200. eventTypes: EventTypes.TRANSACTION,
  201. },
  202. custom_metrics: {
  203. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  204. dataset: Dataset.GENERIC_METRICS,
  205. eventTypes: EventTypes.TRANSACTION,
  206. },
  207. span_metrics: {
  208. aggregate: DEFAULT_SPAN_METRIC_ALERT_FIELD,
  209. dataset: Dataset.GENERIC_METRICS,
  210. eventTypes: EventTypes.TRANSACTION,
  211. },
  212. llm_tokens: {
  213. aggregate: 'sum(ai.total_tokens.used)',
  214. dataset: Dataset.GENERIC_METRICS,
  215. eventTypes: EventTypes.TRANSACTION,
  216. },
  217. llm_cost: {
  218. aggregate: 'sum(ai.total_cost)',
  219. dataset: Dataset.GENERIC_METRICS,
  220. eventTypes: EventTypes.TRANSACTION,
  221. },
  222. insights_metrics: {
  223. aggregate: DEFAULT_INSIGHTS_METRICS_ALERT_FIELD,
  224. dataset: Dataset.GENERIC_METRICS,
  225. eventTypes: EventTypes.TRANSACTION,
  226. },
  227. crash_free_sessions: {
  228. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  229. dataset: Dataset.METRICS,
  230. eventTypes: EventTypes.SESSION,
  231. },
  232. crash_free_users: {
  233. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  234. dataset: Dataset.METRICS,
  235. eventTypes: EventTypes.USER,
  236. },
  237. };
  238. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  239. export const hidePrimarySelectorSet = new Set<AlertType>([
  240. 'num_errors',
  241. 'users_experiencing_errors',
  242. 'throughput',
  243. 'apdex',
  244. 'failure_rate',
  245. 'crash_free_sessions',
  246. 'crash_free_users',
  247. ]);
  248. export const hideParameterSelectorSet = new Set<AlertType>([
  249. 'trans_duration',
  250. 'lcp',
  251. 'fid',
  252. 'cls',
  253. ]);
  254. const TRANSACTION_SUPPORTED_TAGS = [
  255. FieldKey.RELEASE,
  256. FieldKey.TRANSACTION,
  257. FieldKey.TRANSACTION_OP,
  258. FieldKey.TRANSACTION_STATUS,
  259. FieldKey.HTTP_METHOD,
  260. FieldKey.HTTP_STATUS_CODE,
  261. FieldKey.BROWSER_NAME,
  262. FieldKey.GEO_COUNTRY_CODE,
  263. FieldKey.OS_NAME,
  264. ];
  265. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  266. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  267. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  268. FieldKey.AGE,
  269. FieldKey.ASSIGNED,
  270. FieldKey.ASSIGNED_OR_SUGGESTED,
  271. FieldKey.BOOKMARKS,
  272. FieldKey.DEVICE_MODEL_ID,
  273. FieldKey.EVENT_TIMESTAMP,
  274. FieldKey.EVENT_TYPE,
  275. FieldKey.FIRST_RELEASE,
  276. FieldKey.FIRST_SEEN,
  277. FieldKey.IS,
  278. FieldKey.ISSUE_CATEGORY,
  279. FieldKey.ISSUE_TYPE,
  280. FieldKey.LAST_SEEN,
  281. FieldKey.PLATFORM_NAME,
  282. ...Object.values(WebVital),
  283. ...Object.values(MobileVital),
  284. ...Object.values(ReplayFieldKey),
  285. ...Object.values(ReplayClickFieldKey),
  286. ];
  287. const ERROR_SUPPORTED_TAGS = [
  288. FieldKey.IS,
  289. ...Object.keys(STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS).map(
  290. key => key as FieldKey
  291. ),
  292. ];
  293. // Some data sets support a very limited number of tags. For these cases,
  294. // define all supported tags explicitly
  295. export function datasetSupportedTags(
  296. dataset: Dataset,
  297. org: Organization
  298. ): TagCollection | undefined {
  299. return mapValues(
  300. {
  301. [Dataset.ERRORS]: ERROR_SUPPORTED_TAGS,
  302. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  303. ? undefined
  304. : transactionSupportedTags(org),
  305. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  306. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  307. ? undefined
  308. : transactionSupportedTags(org),
  309. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  310. },
  311. value => {
  312. return value ? makeTagCollection(value) : undefined;
  313. }
  314. )[dataset];
  315. }
  316. function transactionSupportedTags(org: Organization) {
  317. if (shouldShowOnDemandMetricAlertUI(org)) {
  318. // on-demand metrics support all tags, except the ones defined in ommited tags
  319. return undefined;
  320. }
  321. return TRANSACTION_SUPPORTED_TAGS;
  322. }
  323. // Some data sets support all tags except some. For these cases, define the
  324. // omissions only
  325. export function datasetOmittedTags(
  326. dataset: Dataset,
  327. org: Organization
  328. ):
  329. | Array<
  330. | FieldKey
  331. | WebVital
  332. | MobileVital
  333. | SpanOpBreakdown
  334. | ReplayFieldKey
  335. | ReplayClickFieldKey
  336. >
  337. | undefined {
  338. return {
  339. [Dataset.ERRORS]: [
  340. FieldKey.EVENT_TYPE,
  341. FieldKey.RELEASE_VERSION,
  342. FieldKey.RELEASE_STAGE,
  343. FieldKey.RELEASE_BUILD,
  344. FieldKey.PROJECT,
  345. ...Object.values(WebVital),
  346. ...Object.values(MobileVital),
  347. ...Object.values(SpanOpBreakdown),
  348. FieldKey.TRANSACTION,
  349. FieldKey.TRANSACTION_DURATION,
  350. FieldKey.TRANSACTION_OP,
  351. FieldKey.TRANSACTION_STATUS,
  352. ],
  353. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  354. [Dataset.METRICS]: undefined,
  355. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  356. [Dataset.SESSIONS]: undefined,
  357. }[dataset];
  358. }
  359. function transactionOmittedTags(org: Organization) {
  360. if (shouldShowOnDemandMetricAlertUI(org)) {
  361. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  362. }
  363. return org.features.includes('alert-allow-indexed')
  364. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  365. : undefined;
  366. }
  367. export function getSupportedAndOmittedTags(
  368. dataset: Dataset,
  369. organization: Organization
  370. ): {
  371. omitTags?: string[];
  372. supportedTags?: TagCollection;
  373. } {
  374. const omitTags = datasetOmittedTags(dataset, organization);
  375. const supportedTags = datasetSupportedTags(dataset, organization);
  376. const result = {omitTags, supportedTags};
  377. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  378. return Object.keys({omitTags, supportedTags}).reduce<{
  379. omitTags?: string[];
  380. supportedTags?: TagCollection;
  381. }>((acc, key) => {
  382. if (result[key] !== undefined) {
  383. acc[key] = result[key];
  384. }
  385. return acc;
  386. }, {});
  387. }
  388. export function getMEPAlertsDataset(
  389. dataset: Dataset,
  390. newAlert: boolean
  391. ): MEPAlertsDataset {
  392. // Dataset.ERRORS overrides all cases
  393. if (dataset === Dataset.ERRORS) {
  394. return MEPAlertsDataset.DISCOVER;
  395. }
  396. if (newAlert) {
  397. return MEPAlertsDataset.METRICS_ENHANCED;
  398. }
  399. if (dataset === Dataset.GENERIC_METRICS) {
  400. return MEPAlertsDataset.METRICS_ENHANCED;
  401. }
  402. return MEPAlertsDataset.DISCOVER;
  403. }