options.tsx 11 KB

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