options.tsx 11 KB

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