options.tsx 9.2 KB

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