options.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_METRIC_ALERT_FIELD,
  22. DEFAULT_SPAN_METRIC_ALERT_FIELD,
  23. } from 'sentry/utils/metrics/mri';
  24. import {ON_DEMAND_METRICS_UNSUPPORTED_TAGS} from 'sentry/utils/onDemandMetrics/constants';
  25. import {shouldShowOnDemandMetricAlertUI} from 'sentry/utils/onDemandMetrics/features';
  26. import {
  27. Dataset,
  28. EventTypes,
  29. SessionsAggregate,
  30. } from 'sentry/views/alerts/rules/metric/types';
  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. | 'span_metrics'
  48. | 'llm_tokens'
  49. | 'llm_cost';
  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'>;
  61. export const DatasetMEPAlertQueryTypes: Record<
  62. Exclude<Dataset, 'search_issues' | Dataset.SESSIONS>, // 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. };
  70. export const AlertWizardAlertNames: Record<AlertType, string> = {
  71. issues: t('Issues'),
  72. num_errors: t('Number of Errors'),
  73. users_experiencing_errors: t('Users Experiencing Errors'),
  74. throughput: t('Throughput'),
  75. trans_duration: t('Transaction Duration'),
  76. apdex: t('Apdex'),
  77. failure_rate: t('Failure Rate'),
  78. lcp: t('Largest Contentful Paint'),
  79. fid: t('First Input Delay'),
  80. cls: t('Cumulative Layout Shift'),
  81. custom_metrics: t('Custom Metric'),
  82. span_metrics: t('Span 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. };
  89. type AlertWizardCategory = {
  90. categoryHeading: string;
  91. options: AlertType[];
  92. };
  93. export const getAlertWizardCategories = (org: Organization) => {
  94. const result: AlertWizardCategory[] = [
  95. {
  96. categoryHeading: t('Errors'),
  97. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  98. },
  99. ];
  100. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  101. if (!isSelfHostedErrorsOnly) {
  102. if (org.features.includes('crash-rate-alerts')) {
  103. result.push({
  104. categoryHeading: t('Sessions'),
  105. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  106. });
  107. }
  108. result.push({
  109. categoryHeading: t('Performance'),
  110. options: [
  111. 'throughput',
  112. 'trans_duration',
  113. 'apdex',
  114. 'failure_rate',
  115. 'lcp',
  116. 'fid',
  117. 'cls',
  118. ...(hasCustomMetrics(org) ? (['custom_transactions'] satisfies AlertType[]) : []),
  119. ],
  120. });
  121. if (org.features.includes('insights-addon-modules')) {
  122. result.push({
  123. categoryHeading: LLM_MONITORING_MODULE_TITLE,
  124. options: ['llm_tokens', 'llm_cost'],
  125. });
  126. }
  127. result.push({
  128. categoryHeading: hasCustomMetrics(org) ? t('Metrics') : t('Custom'),
  129. options: [
  130. hasCustomMetrics(org) ? 'custom_metrics' : 'custom_transactions',
  131. ...(hasCustomMetricsExtractionRules(org) ? ['span_metrics' as const] : []),
  132. ],
  133. });
  134. }
  135. return result;
  136. };
  137. export type WizardRuleTemplate = {
  138. aggregate: string;
  139. dataset: Dataset;
  140. eventTypes: EventTypes;
  141. query?: string;
  142. };
  143. export const AlertWizardRuleTemplates: Record<
  144. MetricAlertType,
  145. Readonly<WizardRuleTemplate>
  146. > = {
  147. num_errors: {
  148. aggregate: 'count()',
  149. dataset: Dataset.ERRORS,
  150. eventTypes: EventTypes.ERROR,
  151. },
  152. users_experiencing_errors: {
  153. aggregate: 'count_unique(user)',
  154. dataset: Dataset.ERRORS,
  155. eventTypes: EventTypes.ERROR,
  156. },
  157. throughput: {
  158. aggregate: 'count()',
  159. dataset: Dataset.TRANSACTIONS,
  160. eventTypes: EventTypes.TRANSACTION,
  161. },
  162. trans_duration: {
  163. aggregate: 'p95(transaction.duration)',
  164. dataset: Dataset.TRANSACTIONS,
  165. eventTypes: EventTypes.TRANSACTION,
  166. },
  167. apdex: {
  168. aggregate: 'apdex(300)',
  169. dataset: Dataset.TRANSACTIONS,
  170. eventTypes: EventTypes.TRANSACTION,
  171. },
  172. failure_rate: {
  173. aggregate: 'failure_rate()',
  174. dataset: Dataset.TRANSACTIONS,
  175. eventTypes: EventTypes.TRANSACTION,
  176. },
  177. lcp: {
  178. aggregate: 'p95(measurements.lcp)',
  179. dataset: Dataset.TRANSACTIONS,
  180. eventTypes: EventTypes.TRANSACTION,
  181. },
  182. fid: {
  183. aggregate: 'p95(measurements.fid)',
  184. dataset: Dataset.TRANSACTIONS,
  185. eventTypes: EventTypes.TRANSACTION,
  186. },
  187. cls: {
  188. aggregate: 'p95(measurements.cls)',
  189. dataset: Dataset.TRANSACTIONS,
  190. eventTypes: EventTypes.TRANSACTION,
  191. },
  192. custom_transactions: {
  193. aggregate: 'p95(measurements.fp)',
  194. dataset: Dataset.GENERIC_METRICS,
  195. eventTypes: EventTypes.TRANSACTION,
  196. },
  197. custom_metrics: {
  198. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  199. dataset: Dataset.GENERIC_METRICS,
  200. eventTypes: EventTypes.TRANSACTION,
  201. },
  202. span_metrics: {
  203. aggregate: DEFAULT_SPAN_METRIC_ALERT_FIELD,
  204. dataset: Dataset.GENERIC_METRICS,
  205. eventTypes: EventTypes.TRANSACTION,
  206. },
  207. llm_tokens: {
  208. aggregate: 'sum(ai.total_tokens.used)',
  209. dataset: Dataset.GENERIC_METRICS,
  210. eventTypes: EventTypes.TRANSACTION,
  211. },
  212. llm_cost: {
  213. aggregate: 'sum(ai.total_cost)',
  214. dataset: Dataset.GENERIC_METRICS,
  215. eventTypes: EventTypes.TRANSACTION,
  216. },
  217. crash_free_sessions: {
  218. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  219. dataset: Dataset.METRICS,
  220. eventTypes: EventTypes.SESSION,
  221. },
  222. crash_free_users: {
  223. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  224. dataset: Dataset.METRICS,
  225. eventTypes: EventTypes.USER,
  226. },
  227. };
  228. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  229. export const hidePrimarySelectorSet = new Set<AlertType>([
  230. 'num_errors',
  231. 'users_experiencing_errors',
  232. 'throughput',
  233. 'apdex',
  234. 'failure_rate',
  235. 'crash_free_sessions',
  236. 'crash_free_users',
  237. ]);
  238. export const hideParameterSelectorSet = new Set<AlertType>([
  239. 'trans_duration',
  240. 'lcp',
  241. 'fid',
  242. 'cls',
  243. ]);
  244. const TRANSACTION_SUPPORTED_TAGS = [
  245. FieldKey.RELEASE,
  246. FieldKey.TRANSACTION,
  247. FieldKey.TRANSACTION_OP,
  248. FieldKey.TRANSACTION_STATUS,
  249. FieldKey.HTTP_METHOD,
  250. FieldKey.HTTP_STATUS_CODE,
  251. FieldKey.BROWSER_NAME,
  252. FieldKey.GEO_COUNTRY_CODE,
  253. FieldKey.OS_NAME,
  254. ];
  255. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  256. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  257. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  258. FieldKey.AGE,
  259. FieldKey.ASSIGNED,
  260. FieldKey.ASSIGNED_OR_SUGGESTED,
  261. FieldKey.BOOKMARKS,
  262. FieldKey.DEVICE_MODEL_ID,
  263. FieldKey.EVENT_TIMESTAMP,
  264. FieldKey.EVENT_TYPE,
  265. FieldKey.FIRST_RELEASE,
  266. FieldKey.FIRST_SEEN,
  267. FieldKey.IS,
  268. FieldKey.ISSUE_CATEGORY,
  269. FieldKey.ISSUE_TYPE,
  270. FieldKey.LAST_SEEN,
  271. FieldKey.PLATFORM_NAME,
  272. ...Object.values(WebVital),
  273. ...Object.values(MobileVital),
  274. ...Object.values(ReplayFieldKey),
  275. ...Object.values(ReplayClickFieldKey),
  276. ];
  277. const ERROR_SUPPORTED_TAGS = [
  278. FieldKey.IS,
  279. ...Object.keys(STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS).map(
  280. key => key as FieldKey
  281. ),
  282. ];
  283. // Some data sets support a very limited number of tags. For these cases,
  284. // define all supported tags explicitly
  285. export function datasetSupportedTags(
  286. dataset: Dataset,
  287. org: Organization
  288. ): TagCollection | undefined {
  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. return {
  329. [Dataset.ERRORS]: [
  330. FieldKey.EVENT_TYPE,
  331. FieldKey.RELEASE_VERSION,
  332. FieldKey.RELEASE_STAGE,
  333. FieldKey.RELEASE_BUILD,
  334. FieldKey.PROJECT,
  335. ...Object.values(WebVital),
  336. ...Object.values(MobileVital),
  337. ...Object.values(SpanOpBreakdown),
  338. FieldKey.TRANSACTION,
  339. FieldKey.TRANSACTION_DURATION,
  340. FieldKey.TRANSACTION_OP,
  341. FieldKey.TRANSACTION_STATUS,
  342. ],
  343. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  344. [Dataset.METRICS]: undefined,
  345. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  346. [Dataset.SESSIONS]: undefined,
  347. }[dataset];
  348. }
  349. function transactionOmittedTags(org: Organization) {
  350. if (shouldShowOnDemandMetricAlertUI(org)) {
  351. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  352. }
  353. return org.features.includes('alert-allow-indexed')
  354. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  355. : undefined;
  356. }
  357. export function getSupportedAndOmittedTags(
  358. dataset: Dataset,
  359. organization: Organization
  360. ): {
  361. omitTags?: string[];
  362. supportedTags?: TagCollection;
  363. } {
  364. const omitTags = datasetOmittedTags(dataset, organization);
  365. const supportedTags = datasetSupportedTags(dataset, organization);
  366. const result = {omitTags, supportedTags};
  367. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  368. return Object.keys({omitTags, supportedTags}).reduce<{
  369. omitTags?: string[];
  370. supportedTags?: TagCollection;
  371. }>((acc, key) => {
  372. if (result[key] !== undefined) {
  373. acc[key] = result[key];
  374. }
  375. return acc;
  376. }, {});
  377. }
  378. export function getMEPAlertsDataset(
  379. dataset: Dataset,
  380. newAlert: boolean
  381. ): MEPAlertsDataset {
  382. // Dataset.ERRORS overrides all cases
  383. if (dataset === Dataset.ERRORS) {
  384. return MEPAlertsDataset.DISCOVER;
  385. }
  386. if (newAlert) {
  387. return MEPAlertsDataset.METRICS_ENHANCED;
  388. }
  389. if (dataset === Dataset.GENERIC_METRICS) {
  390. return MEPAlertsDataset.METRICS_ENHANCED;
  391. }
  392. return MEPAlertsDataset.DISCOVER;
  393. }