options.tsx 11 KB

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