options.tsx 8.9 KB

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