options.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import mapValues from 'lodash/mapValues';
  2. import {FeatureBadge} from 'sentry/components/core/badge/featureBadge';
  3. import {STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS} from 'sentry/components/events/searchBarFieldConstants';
  4. import {t} from 'sentry/locale';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import type {TagCollection} from 'sentry/types/group';
  7. import type {Organization} from 'sentry/types/organization';
  8. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  9. import {
  10. FieldKey,
  11. makeTagCollection,
  12. MobileVital,
  13. ReplayClickFieldKey,
  14. ReplayFieldKey,
  15. SpanOpBreakdown,
  16. WebVital,
  17. } from 'sentry/utils/fields';
  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 {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  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. | 'uptime_monitor'
  41. | 'crons_monitor'
  42. | 'eap_metrics';
  43. export enum MEPAlertsQueryType {
  44. ERROR = 0,
  45. PERFORMANCE = 1,
  46. CRASH_RATE = 2,
  47. }
  48. export type MetricAlertType = Exclude<
  49. AlertType,
  50. 'issues' | 'uptime_monitor' | 'crons_monitor'
  51. >;
  52. export const DatasetMEPAlertQueryTypes: Record<
  53. Exclude<Dataset, Dataset.ISSUE_PLATFORM | Dataset.SESSIONS | Dataset.REPLAYS>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  54. MEPAlertsQueryType
  55. > = {
  56. [Dataset.ERRORS]: MEPAlertsQueryType.ERROR,
  57. [Dataset.TRANSACTIONS]: MEPAlertsQueryType.PERFORMANCE,
  58. [Dataset.GENERIC_METRICS]: MEPAlertsQueryType.PERFORMANCE,
  59. [Dataset.METRICS]: MEPAlertsQueryType.CRASH_RATE,
  60. [Dataset.EVENTS_ANALYTICS_PLATFORM]: MEPAlertsQueryType.PERFORMANCE,
  61. };
  62. export const AlertWizardAlertNames: Record<AlertType, string> = {
  63. issues: t('Issues'),
  64. num_errors: t('Number of Errors'),
  65. users_experiencing_errors: t('Users Experiencing Errors'),
  66. throughput: t('Throughput'),
  67. trans_duration: t('Transaction Duration'),
  68. apdex: t('Apdex'),
  69. failure_rate: t('Failure Rate'),
  70. lcp: t('Largest Contentful Paint'),
  71. fid: t('First Input Delay'),
  72. cls: t('Cumulative Layout Shift'),
  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. uptime_monitor: t('Uptime Monitor'),
  77. eap_metrics: t('Spans'),
  78. crons_monitor: t('Cron Monitor'),
  79. };
  80. /**
  81. * Additional elements to render after the name of the alert rule type. Useful
  82. * for adding feature badges or other call-outs for newer alert types.
  83. */
  84. export const AlertWizardExtraContent: Partial<Record<AlertType, React.ReactNode>> = {
  85. eap_metrics: (
  86. <FeatureBadge
  87. type="beta"
  88. tooltipProps={{
  89. title: t('This feature is available for early adopters and the UX may change'),
  90. }}
  91. />
  92. ),
  93. uptime_monitor: <FeatureBadge type="new" />,
  94. };
  95. type AlertWizardCategory = {
  96. categoryHeading: string;
  97. options: AlertType[];
  98. };
  99. export const getAlertWizardCategories = (org: Organization) => {
  100. const result: AlertWizardCategory[] = [
  101. {
  102. categoryHeading: t('Errors'),
  103. options: ['issues', 'num_errors', 'users_experiencing_errors'],
  104. },
  105. ];
  106. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  107. if (!isSelfHostedErrorsOnly) {
  108. if (org.features.includes('crash-rate-alerts')) {
  109. result.push({
  110. categoryHeading: t('Sessions'),
  111. options: ['crash_free_sessions', 'crash_free_users'] satisfies AlertType[],
  112. });
  113. }
  114. result.push({
  115. categoryHeading: t('Performance'),
  116. options: [
  117. 'throughput',
  118. 'trans_duration',
  119. 'apdex',
  120. 'failure_rate',
  121. 'lcp',
  122. 'fid',
  123. 'cls',
  124. ...(hasEAPAlerts(org) ? ['eap_metrics' as const] : []),
  125. ],
  126. });
  127. if (org.features.includes('uptime')) {
  128. result.push({
  129. categoryHeading: t('Uptime Monitoring'),
  130. options: ['uptime_monitor'],
  131. });
  132. }
  133. if (org.features.includes('insights-crons')) {
  134. result.push({
  135. categoryHeading: t('Cron Monitoring'),
  136. options: ['crons_monitor'],
  137. });
  138. }
  139. result.push({
  140. categoryHeading: t('Custom'),
  141. options: ['custom_transactions'],
  142. });
  143. }
  144. return result;
  145. };
  146. export type WizardRuleTemplate = {
  147. aggregate: string;
  148. dataset: Dataset;
  149. eventTypes: EventTypes;
  150. query?: string;
  151. };
  152. export const AlertWizardRuleTemplates: Record<
  153. MetricAlertType,
  154. Readonly<WizardRuleTemplate>
  155. > = {
  156. num_errors: {
  157. aggregate: 'count()',
  158. dataset: Dataset.ERRORS,
  159. eventTypes: EventTypes.ERROR,
  160. },
  161. users_experiencing_errors: {
  162. aggregate: 'count_unique(user)',
  163. dataset: Dataset.ERRORS,
  164. eventTypes: EventTypes.ERROR,
  165. },
  166. throughput: {
  167. aggregate: 'count()',
  168. dataset: Dataset.TRANSACTIONS,
  169. eventTypes: EventTypes.TRANSACTION,
  170. },
  171. trans_duration: {
  172. aggregate: 'p95(transaction.duration)',
  173. dataset: Dataset.TRANSACTIONS,
  174. eventTypes: EventTypes.TRANSACTION,
  175. },
  176. apdex: {
  177. aggregate: 'apdex(300)',
  178. dataset: Dataset.TRANSACTIONS,
  179. eventTypes: EventTypes.TRANSACTION,
  180. },
  181. failure_rate: {
  182. aggregate: 'failure_rate()',
  183. dataset: Dataset.TRANSACTIONS,
  184. eventTypes: EventTypes.TRANSACTION,
  185. },
  186. lcp: {
  187. aggregate: 'p95(measurements.lcp)',
  188. dataset: Dataset.TRANSACTIONS,
  189. eventTypes: EventTypes.TRANSACTION,
  190. },
  191. fid: {
  192. aggregate: 'p95(measurements.fid)',
  193. dataset: Dataset.TRANSACTIONS,
  194. eventTypes: EventTypes.TRANSACTION,
  195. },
  196. cls: {
  197. aggregate: 'p95(measurements.cls)',
  198. dataset: Dataset.TRANSACTIONS,
  199. eventTypes: EventTypes.TRANSACTION,
  200. },
  201. custom_transactions: {
  202. aggregate: 'p95(measurements.fp)',
  203. dataset: Dataset.GENERIC_METRICS,
  204. eventTypes: EventTypes.TRANSACTION,
  205. },
  206. crash_free_sessions: {
  207. aggregate: SessionsAggregate.CRASH_FREE_SESSIONS,
  208. dataset: Dataset.METRICS,
  209. eventTypes: EventTypes.SESSION,
  210. },
  211. crash_free_users: {
  212. aggregate: SessionsAggregate.CRASH_FREE_USERS,
  213. dataset: Dataset.METRICS,
  214. eventTypes: EventTypes.USER,
  215. },
  216. eap_metrics: {
  217. aggregate: 'count(span.duration)',
  218. dataset: Dataset.EVENTS_ANALYTICS_PLATFORM,
  219. eventTypes: EventTypes.TRANSACTION,
  220. },
  221. };
  222. export const DEFAULT_WIZARD_TEMPLATE = AlertWizardRuleTemplates.num_errors;
  223. export const hidePrimarySelectorSet = new Set<AlertType>([
  224. 'num_errors',
  225. 'users_experiencing_errors',
  226. 'throughput',
  227. 'apdex',
  228. 'failure_rate',
  229. 'crash_free_sessions',
  230. 'crash_free_users',
  231. ]);
  232. export const hideParameterSelectorSet = new Set<AlertType>([
  233. 'trans_duration',
  234. 'lcp',
  235. 'fid',
  236. 'cls',
  237. ]);
  238. const TRANSACTION_SUPPORTED_TAGS = [
  239. FieldKey.RELEASE,
  240. FieldKey.TRANSACTION,
  241. FieldKey.TRANSACTION_OP,
  242. FieldKey.TRANSACTION_STATUS,
  243. FieldKey.HTTP_METHOD,
  244. FieldKey.HTTP_STATUS_CODE,
  245. FieldKey.BROWSER_NAME,
  246. FieldKey.GEO_COUNTRY_CODE,
  247. FieldKey.OS_NAME,
  248. ];
  249. const SESSION_SUPPORTED_TAGS = [FieldKey.RELEASE];
  250. // This is purely for testing purposes, use with alert-allow-indexed feature flag
  251. const INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS = [
  252. FieldKey.AGE,
  253. FieldKey.ASSIGNED,
  254. FieldKey.ASSIGNED_OR_SUGGESTED,
  255. FieldKey.BOOKMARKS,
  256. FieldKey.DEVICE_MODEL_ID,
  257. FieldKey.EVENT_TIMESTAMP,
  258. FieldKey.EVENT_TYPE,
  259. FieldKey.FIRST_RELEASE,
  260. FieldKey.FIRST_SEEN,
  261. FieldKey.IS,
  262. FieldKey.ISSUE_CATEGORY,
  263. FieldKey.ISSUE_TYPE,
  264. FieldKey.LAST_SEEN,
  265. FieldKey.PLATFORM_NAME,
  266. ...Object.values(WebVital),
  267. ...Object.values(MobileVital),
  268. ...Object.values(ReplayFieldKey),
  269. ...Object.values(ReplayClickFieldKey),
  270. ];
  271. const ERROR_SUPPORTED_TAGS = [
  272. FieldKey.IS,
  273. ...Object.keys(STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS).map(
  274. key => key as FieldKey
  275. ),
  276. ];
  277. // Some data sets support a very limited number of tags. For these cases,
  278. // define all supported tags explicitly
  279. export function datasetSupportedTags(
  280. dataset: Dataset,
  281. org: Organization
  282. ): TagCollection | undefined {
  283. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  284. return mapValues(
  285. {
  286. [Dataset.ERRORS]: ERROR_SUPPORTED_TAGS,
  287. [Dataset.TRANSACTIONS]: org.features.includes('alert-allow-indexed')
  288. ? undefined
  289. : transactionSupportedTags(org),
  290. [Dataset.METRICS]: SESSION_SUPPORTED_TAGS,
  291. [Dataset.GENERIC_METRICS]: org.features.includes('alert-allow-indexed')
  292. ? undefined
  293. : transactionSupportedTags(org),
  294. [Dataset.SESSIONS]: SESSION_SUPPORTED_TAGS,
  295. },
  296. value => {
  297. return value ? makeTagCollection(value) : undefined;
  298. }
  299. )[dataset];
  300. }
  301. function transactionSupportedTags(org: Organization) {
  302. if (shouldShowOnDemandMetricAlertUI(org)) {
  303. // on-demand metrics support all tags, except the ones defined in ommited tags
  304. return undefined;
  305. }
  306. return TRANSACTION_SUPPORTED_TAGS;
  307. }
  308. // Some data sets support all tags except some. For these cases, define the
  309. // omissions only
  310. export function datasetOmittedTags(
  311. dataset: Dataset,
  312. org: Organization
  313. ):
  314. | Array<
  315. | FieldKey
  316. | WebVital
  317. | MobileVital
  318. | SpanOpBreakdown
  319. | ReplayFieldKey
  320. | ReplayClickFieldKey
  321. >
  322. | undefined {
  323. // @ts-expect-error TS(2339): Property 'events_analytics_platform' does not exis... Remove this comment to see the full error message
  324. return {
  325. [Dataset.ERRORS]: [
  326. FieldKey.EVENT_TYPE,
  327. FieldKey.RELEASE_VERSION,
  328. FieldKey.RELEASE_STAGE,
  329. FieldKey.RELEASE_BUILD,
  330. FieldKey.PROJECT,
  331. ...Object.values(WebVital),
  332. ...Object.values(MobileVital),
  333. ...Object.values(SpanOpBreakdown),
  334. FieldKey.TRANSACTION,
  335. FieldKey.TRANSACTION_DURATION,
  336. FieldKey.TRANSACTION_OP,
  337. FieldKey.TRANSACTION_STATUS,
  338. ],
  339. [Dataset.TRANSACTIONS]: transactionOmittedTags(org),
  340. [Dataset.METRICS]: undefined,
  341. [Dataset.GENERIC_METRICS]: transactionOmittedTags(org),
  342. [Dataset.SESSIONS]: undefined,
  343. }[dataset];
  344. }
  345. function transactionOmittedTags(org: Organization) {
  346. if (shouldShowOnDemandMetricAlertUI(org)) {
  347. return [...ON_DEMAND_METRICS_UNSUPPORTED_TAGS];
  348. }
  349. return org.features.includes('alert-allow-indexed')
  350. ? INDEXED_PERFORMANCE_ALERTS_OMITTED_TAGS
  351. : undefined;
  352. }
  353. export function getSupportedAndOmittedTags(
  354. dataset: Dataset,
  355. organization: Organization
  356. ): {
  357. omitTags?: string[];
  358. supportedTags?: TagCollection;
  359. } {
  360. const omitTags = datasetOmittedTags(dataset, organization);
  361. const supportedTags = datasetSupportedTags(dataset, organization);
  362. const result = {omitTags, supportedTags};
  363. // remove undefined values, since passing explicit undefined to the SeachBar overrides its defaults
  364. return Object.keys({omitTags, supportedTags}).reduce<{
  365. omitTags?: string[];
  366. supportedTags?: TagCollection;
  367. }>((acc, key) => {
  368. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  369. if (result[key] !== undefined) {
  370. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  371. acc[key] = result[key];
  372. }
  373. return acc;
  374. }, {});
  375. }
  376. export function getDiscoverDataset(
  377. dataset: Dataset,
  378. newAlert: boolean
  379. ): DiscoverDatasets {
  380. // Dataset.ERRORS overrides all cases
  381. if (dataset === Dataset.ERRORS) {
  382. return DiscoverDatasets.DISCOVER;
  383. }
  384. if (newAlert) {
  385. return DiscoverDatasets.METRICS_ENHANCED;
  386. }
  387. if (dataset === Dataset.GENERIC_METRICS) {
  388. return DiscoverDatasets.METRICS_ENHANCED;
  389. }
  390. return DiscoverDatasets.DISCOVER;
  391. }