options.tsx 11 KB

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