options.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import mapValues from 'lodash/mapValues';
  2. import {t} from 'sentry/locale';
  3. import type {TagCollection} from 'sentry/types/group';
  4. import type {Organization} from 'sentry/types/organization';
  5. import {
  6. FieldKey,
  7. makeTagCollection,
  8. MobileVital,
  9. ReplayClickFieldKey,
  10. ReplayFieldKey,
  11. SpanOpBreakdown,
  12. WebVital,
  13. } from 'sentry/utils/fields';
  14. import {hasCustomMetrics} from 'sentry/utils/metrics/features';
  15. import {DEFAULT_METRIC_ALERT_FIELD} from 'sentry/utils/metrics/mri';
  16. import {ON_DEMAND_METRICS_UNSUPPORTED_TAGS} from 'sentry/utils/onDemandMetrics/constants';
  17. import {shouldShowOnDemandMetricAlertUI} from 'sentry/utils/onDemandMetrics/features';
  18. import {
  19. Dataset,
  20. EventTypes,
  21. SessionsAggregate,
  22. } from 'sentry/views/alerts/rules/metric/types';
  23. export type AlertType =
  24. | 'issues'
  25. | 'num_errors'
  26. | 'users_experiencing_errors'
  27. | 'throughput'
  28. | 'trans_duration'
  29. | 'apdex'
  30. | 'failure_rate'
  31. | 'lcp'
  32. | 'fid'
  33. | 'cls'
  34. | 'crash_free_sessions'
  35. | 'crash_free_users'
  36. | 'custom_transactions'
  37. | 'custom_metrics'
  38. | 'llm_tokens'
  39. | 'llm_cost';
  40. export enum MEPAlertsQueryType {
  41. ERROR = 0,
  42. PERFORMANCE = 1,
  43. CRASH_RATE = 2,
  44. }
  45. export enum MEPAlertsDataset {
  46. DISCOVER = 'discover',
  47. METRICS = 'metrics',
  48. METRICS_ENHANCED = 'metricsEnhanced',
  49. }
  50. export type MetricAlertType = Exclude<AlertType, 'issues'>;
  51. export const DatasetMEPAlertQueryTypes: Record<Dataset, MEPAlertsQueryType> = {
  52. [Dataset.ERRORS]: MEPAlertsQueryType.ERROR,
  53. [Dataset.TRANSACTIONS]: MEPAlertsQueryType.PERFORMANCE,
  54. [Dataset.GENERIC_METRICS]: MEPAlertsQueryType.PERFORMANCE,
  55. [Dataset.METRICS]: MEPAlertsQueryType.CRASH_RATE,
  56. [Dataset.SESSIONS]: MEPAlertsQueryType.CRASH_RATE,
  57. };
  58. export const AlertWizardAlertNames: Record<AlertType, string> = {
  59. issues: t('Issues'),
  60. num_errors: t('Number of Errors'),
  61. users_experiencing_errors: t('Users Experiencing Errors'),
  62. throughput: t('Throughput'),
  63. trans_duration: t('Transaction Duration'),
  64. apdex: t('Apdex'),
  65. failure_rate: t('Failure Rate'),
  66. lcp: t('Largest Contentful Paint'),
  67. fid: t('First Input Delay'),
  68. cls: t('Cumulative Layout Shift'),
  69. custom_metrics: t('Custom Metric'),
  70. custom_transactions: t('Custom Measurement'),
  71. crash_free_sessions: t('Crash Free Session Rate'),
  72. crash_free_users: t('Crash Free User Rate'),
  73. llm_cost: t('LLM cost'),
  74. llm_tokens: t('LLM token usage'),
  75. };
  76. type AlertWizardCategory = {
  77. categoryHeading: string;
  78. options: AlertType[];
  79. };
  80. export const getAlertWizardCategories = (org: Organization) => {
  81. const result: AlertWizardCategory[] = [
  82. {
  83. categoryHeading: t('Errors'),
  84. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  85. },
  86. ];
  87. if (org.features.includes('crash-rate-alerts')) {
  88. result.push({
  89. categoryHeading: t('Sessions'),
  90. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  91. });
  92. }
  93. result.push({
  94. categoryHeading: t('Performance'),
  95. options: [
  96. 'throughput',
  97. 'trans_duration',
  98. 'apdex',
  99. 'failure_rate',
  100. 'lcp',
  101. 'fid',
  102. 'cls',
  103. ...(hasCustomMetrics(org) ? (['custom_transactions'] satisfies AlertType[]) : []),
  104. ],
  105. });
  106. if (org.features.includes('ai-analytics')) {
  107. result.push({
  108. categoryHeading: t('LLM Monitoring'),
  109. options: ['llm_tokens', 'llm_cost'],
  110. });
  111. }
  112. result.push({
  113. categoryHeading: hasCustomMetrics(org) ? t('Metrics') : t('Custom'),
  114. options: [hasCustomMetrics(org) ? 'custom_metrics' : 'custom_transactions'],
  115. });
  116. return result;
  117. };
  118. export type WizardRuleTemplate = {
  119. aggregate: string;
  120. dataset: Dataset;
  121. eventTypes: EventTypes;
  122. query?: string;
  123. };
  124. export const AlertWizardRuleTemplates: Record<
  125. MetricAlertType,
  126. Readonly<WizardRuleTemplate>
  127. > = {
  128. num_errors: {
  129. aggregate: 'count()',
  130. dataset: Dataset.ERRORS,
  131. eventTypes: EventTypes.ERROR,
  132. },
  133. users_experiencing_errors: {
  134. aggregate: 'count_unique(user)',
  135. dataset: Dataset.ERRORS,
  136. eventTypes: EventTypes.ERROR,
  137. },
  138. throughput: {
  139. aggregate: 'count()',
  140. dataset: Dataset.TRANSACTIONS,
  141. eventTypes: EventTypes.TRANSACTION,
  142. },
  143. trans_duration: {
  144. aggregate: 'p95(transaction.duration)',
  145. dataset: Dataset.TRANSACTIONS,
  146. eventTypes: EventTypes.TRANSACTION,
  147. },
  148. apdex: {
  149. aggregate: 'apdex(300)',
  150. dataset: Dataset.TRANSACTIONS,
  151. eventTypes: EventTypes.TRANSACTION,
  152. },
  153. failure_rate: {
  154. aggregate: 'failure_rate()',
  155. dataset: Dataset.TRANSACTIONS,
  156. eventTypes: EventTypes.TRANSACTION,
  157. },
  158. lcp: {
  159. aggregate: 'p95(measurements.lcp)',
  160. dataset: Dataset.TRANSACTIONS,
  161. eventTypes: EventTypes.TRANSACTION,
  162. },
  163. fid: {
  164. aggregate: 'p95(measurements.fid)',
  165. dataset: Dataset.TRANSACTIONS,
  166. eventTypes: EventTypes.TRANSACTION,
  167. },
  168. cls: {
  169. aggregate: 'p95(measurements.cls)',
  170. dataset: Dataset.TRANSACTIONS,
  171. eventTypes: EventTypes.TRANSACTION,
  172. },
  173. custom_transactions: {
  174. aggregate: 'p95(measurements.fp)',
  175. dataset: Dataset.GENERIC_METRICS,
  176. eventTypes: EventTypes.TRANSACTION,
  177. },
  178. custom_metrics: {
  179. aggregate: DEFAULT_METRIC_ALERT_FIELD,
  180. dataset: Dataset.GENERIC_METRICS,
  181. eventTypes: EventTypes.TRANSACTION,
  182. },
  183. llm_tokens: {
  184. aggregate: 'sum(c:spans/ai.total_tokens.used@none)',
  185. dataset: Dataset.GENERIC_METRICS,
  186. eventTypes: EventTypes.TRANSACTION,
  187. },
  188. llm_cost: {
  189. aggregate: 'sum(c:spans/ai.total_cost@usd)',
  190. dataset: Dataset.GENERIC_METRICS,
  191. eventTypes: EventTypes.TRANSACTION,
  192. },
  193. crash_free_sessions: {
  194. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  195. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  196. dataset: Dataset.SESSIONS,
  197. eventTypes: EventTypes.SESSION,
  198. },
  199. crash_free_users: {
  200. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  201. // TODO(scttcper): Use Dataset.Metric on GA of alert-crash-free-metrics
  202. dataset: Dataset.SESSIONS,
  203. eventTypes: EventTypes.USER,
  204. },
  205. };
  206. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  207. export const hidePrimarySelectorSet = new Set<AlertType>([
  208. 'num_errors',
  209. 'users_experiencing_errors',
  210. 'throughput',
  211. 'apdex',
  212. 'failure_rate',
  213. 'crash_free_sessions',
  214. 'crash_free_users',
  215. ]);
  216. export const hideParameterSelectorSet = new Set<AlertType>([
  217. 'trans_duration',
  218. 'lcp',
  219. 'fid',
  220. 'cls',
  221. ]);
  222. const TRANSACTION_SUPPORTED_TAGS = [
  223. FieldKey.RELEASE,
  224. FieldKey.TRANSACTION,
  225. FieldKey.TRANSACTION_OP,
  226. FieldKey.TRANSACTION_STATUS,
  227. FieldKey.HTTP_METHOD,
  228. FieldKey.HTTP_STATUS_CODE,
  229. FieldKey.BROWSER_NAME,
  230. FieldKey.GEO_COUNTRY_CODE,
  231. FieldKey.OS_NAME,
  232. ];
  233. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  234. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  235. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  236. FieldKey.AGE,
  237. FieldKey.ASSIGNED,
  238. FieldKey.ASSIGNED_OR_SUGGESTED,
  239. FieldKey.BOOKMARKS,
  240. FieldKey.DEVICE_MODEL_ID,
  241. FieldKey.EVENT_TIMESTAMP,
  242. FieldKey.EVENT_TYPE,
  243. FieldKey.FIRST_RELEASE,
  244. FieldKey.FIRST_SEEN,
  245. FieldKey.IS,
  246. FieldKey.ISSUE_CATEGORY,
  247. FieldKey.ISSUE_TYPE,
  248. FieldKey.LAST_SEEN,
  249. FieldKey.PLATFORM_NAME,
  250. ...Object.values(WebVital),
  251. ...Object.values(MobileVital),
  252. ...Object.values(ReplayFieldKey),
  253. ...Object.values(ReplayClickFieldKey),
  254. ];
  255. // Some data sets support a very limited number of tags. For these cases,
  256. // define all supported tags explicitly
  257. export function datasetSupportedTags(
  258. dataset: Dataset,
  259. org: Organization
  260. ): TagCollection | undefined {
  261. return mapValues(
  262. {
  263. [Dataset.ERRORS]: org.features.includes('metric-alert-ignore-archived')
  264. ? [FieldKey.IS]
  265. : undefined,
  266. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  267. ? undefined
  268. : transactionSupportedTags(org),
  269. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  270. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  271. ? undefined
  272. : transactionSupportedTags(org),
  273. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  274. },
  275. value => {
  276. return value ? makeTagCollection(value) : undefined;
  277. }
  278. )[dataset];
  279. }
  280. function transactionSupportedTags(org: Organization) {
  281. if (shouldShowOnDemandMetricAlertUI(org)) {
  282. // on-demand metrics support all tags, except the ones defined in ommited tags
  283. return undefined;
  284. }
  285. return TRANSACTION_SUPPORTED_TAGS;
  286. }
  287. // Some data sets support all tags except some. For these cases, define the
  288. // omissions only
  289. export function datasetOmittedTags(
  290. dataset: Dataset,
  291. org: Organization
  292. ):
  293. | Array<
  294. | FieldKey
  295. | WebVital
  296. | MobileVital
  297. | SpanOpBreakdown
  298. | ReplayFieldKey
  299. | ReplayClickFieldKey
  300. >
  301. | undefined {
  302. return {
  303. [Dataset.ERRORS]: [
  304. FieldKey.EVENT_TYPE,
  305. FieldKey.RELEASE_VERSION,
  306. FieldKey.RELEASE_STAGE,
  307. FieldKey.RELEASE_BUILD,
  308. FieldKey.PROJECT,
  309. ...Object.values(WebVital),
  310. ...Object.values(MobileVital),
  311. ...Object.values(SpanOpBreakdown),
  312. FieldKey.TRANSACTION,
  313. FieldKey.TRANSACTION_DURATION,
  314. FieldKey.TRANSACTION_OP,
  315. FieldKey.TRANSACTION_STATUS,
  316. ],
  317. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  318. [Dataset.METRICS]: undefined,
  319. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  320. [Dataset.SESSIONS]: undefined,
  321. }[dataset];
  322. }
  323. function transactionOmittedTags(org: Organization) {
  324. if (shouldShowOnDemandMetricAlertUI(org)) {
  325. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  326. }
  327. return org.features.includes('alert-allow-indexed')
  328. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  329. : undefined;
  330. }
  331. export function getSupportedAndOmittedTags(
  332. dataset: Dataset,
  333. organization: Organization
  334. ): {
  335. omitTags?: string[];
  336. supportedTags?: TagCollection;
  337. } {
  338. const omitTags = datasetOmittedTags(dataset, organization);
  339. const supportedTags = datasetSupportedTags(dataset, organization);
  340. const result = {omitTags, supportedTags};
  341. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  342. return Object.keys({omitTags, supportedTags}).reduce<{
  343. omitTags?: string[];
  344. supportedTags?: TagCollection;
  345. }>((acc, key) => {
  346. if (result[key] !== undefined) {
  347. acc[key] = result[key];
  348. }
  349. return acc;
  350. }, {});
  351. }
  352. export function getMEPAlertsDataset(
  353. dataset: Dataset,
  354. newAlert: boolean
  355. ): MEPAlertsDataset {
  356. // Dataset.ERRORS overrides all cases
  357. if (dataset === Dataset.ERRORS) {
  358. return MEPAlertsDataset.DISCOVER;
  359. }
  360. if (newAlert) {
  361. return MEPAlertsDataset.METRICS_ENHANCED;
  362. }
  363. if (dataset === Dataset.GENERIC_METRICS) {
  364. return MEPAlertsDataset.METRICS_ENHANCED;
  365. }
  366. return MEPAlertsDataset.DISCOVER;
  367. }