options.tsx 9.0 KB

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