constants.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {t} from 'sentry/locale';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {AggregationKey, LooseFieldKey} from 'sentry/utils/discover/fields';
  4. import {WEB_VITAL_DETAILS} from 'sentry/utils/performance/vitals/constants';
  5. import {
  6. AlertRuleThresholdType,
  7. AlertRuleTriggerType,
  8. Dataset,
  9. Datasource,
  10. EventTypes,
  11. TimeWindow,
  12. Trigger,
  13. UnsavedIncidentRule,
  14. } from 'sentry/views/alerts/incidentRules/types';
  15. import {
  16. DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
  17. getQueryDatasource,
  18. isSessionAggregate,
  19. } from 'sentry/views/alerts/utils';
  20. import {AlertType, WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  21. export const DEFAULT_COUNT_TIME_WINDOW = 1; // 1min
  22. export const DEFAULT_CHANGE_TIME_WINDOW = 60; // 1h
  23. export const DEFAULT_CHANGE_COMP_DELTA = 10080; // 1w
  24. export const DEFAULT_AGGREGATE = 'count()';
  25. export const DEFAULT_TRANSACTION_AGGREGATE = 'p95(transaction.duration)';
  26. export const DATASET_EVENT_TYPE_FILTERS = {
  27. [Dataset.ERRORS]: 'event.type:error',
  28. [Dataset.TRANSACTIONS]: 'event.type:transaction',
  29. } as const;
  30. export const DATASOURCE_EVENT_TYPE_FILTERS = {
  31. [Datasource.ERROR_DEFAULT]: 'event.type:[error, default]',
  32. [Datasource.ERROR]: 'event.type:error',
  33. [Datasource.DEFAULT]: 'event.type:default',
  34. [Datasource.TRANSACTION]: 'event.type:transaction',
  35. } as const;
  36. export type OptionConfig = {
  37. aggregations: AggregationKey[];
  38. fields: LooseFieldKey[];
  39. measurementKeys?: string[];
  40. };
  41. /**
  42. * Allowed error aggregations for alerts
  43. */
  44. export const errorFieldConfig: OptionConfig = {
  45. aggregations: ['count', 'count_unique'],
  46. fields: ['user'],
  47. };
  48. const commonAggregations: AggregationKey[] = [
  49. 'avg',
  50. 'percentile',
  51. 'p50',
  52. 'p75',
  53. 'p95',
  54. 'p99',
  55. 'p100',
  56. ];
  57. const allAggregations: AggregationKey[] = [
  58. ...commonAggregations,
  59. 'failure_rate',
  60. 'apdex',
  61. 'count',
  62. ];
  63. export const COMPARISON_DELTA_OPTIONS = [
  64. {value: 5, label: t('same time 5 minutes ago')}, // 5 minutes
  65. {value: 15, label: t('same time 15 minutes ago')}, // 15 minutes
  66. {value: 60, label: t('same time one hour ago')}, // one hour
  67. {value: 1440, label: t('same time one day ago')}, // one day
  68. {value: 10080, label: t('same time one week ago')}, // one week
  69. {value: 43200, label: t('same time one month ago')}, // 30 days
  70. ];
  71. export function getWizardAlertFieldConfig(
  72. alertType: AlertType,
  73. dataset: Dataset
  74. ): OptionConfig {
  75. if (alertType === 'custom' && dataset === Dataset.ERRORS) {
  76. return errorFieldConfig;
  77. }
  78. // If user selected apdex we must include that in the OptionConfig as it has a user specified column
  79. const aggregations =
  80. alertType === 'apdex' || alertType === 'custom'
  81. ? allAggregations
  82. : commonAggregations;
  83. return {
  84. aggregations,
  85. fields: ['transaction.duration'],
  86. measurementKeys: Object.keys(WEB_VITAL_DETAILS),
  87. };
  88. }
  89. /**
  90. * Allowed transaction aggregations for alerts
  91. */
  92. export const transactionFieldConfig: OptionConfig = {
  93. aggregations: allAggregations,
  94. fields: ['transaction.duration'],
  95. measurementKeys: Object.keys(WEB_VITAL_DETAILS),
  96. };
  97. export function createDefaultTrigger(label: AlertRuleTriggerType): Trigger {
  98. return {
  99. label,
  100. alertThreshold: '',
  101. actions: [],
  102. };
  103. }
  104. export function createDefaultRule(
  105. defaultRuleOptions: Partial<UnsavedIncidentRule> = {}
  106. ): UnsavedIncidentRule {
  107. return {
  108. dataset: Dataset.ERRORS,
  109. eventTypes: [EventTypes.ERROR],
  110. aggregate: DEFAULT_AGGREGATE,
  111. query: '',
  112. timeWindow: 60,
  113. thresholdPeriod: 1,
  114. triggers: [
  115. createDefaultTrigger(AlertRuleTriggerType.CRITICAL),
  116. createDefaultTrigger(AlertRuleTriggerType.WARNING),
  117. ],
  118. projects: [],
  119. environment: null,
  120. resolveThreshold: '',
  121. thresholdType: AlertRuleThresholdType.ABOVE,
  122. ...defaultRuleOptions,
  123. };
  124. }
  125. /**
  126. * Create an unsaved alert from a discover EventView object
  127. */
  128. export function createRuleFromEventView(eventView: EventView): UnsavedIncidentRule {
  129. const parsedQuery = getQueryDatasource(eventView.query);
  130. const datasetAndEventtypes = parsedQuery
  131. ? DATA_SOURCE_TO_SET_AND_EVENT_TYPES[parsedQuery.source]
  132. : DATA_SOURCE_TO_SET_AND_EVENT_TYPES.error;
  133. let aggregate = eventView.getYAxis();
  134. if (
  135. datasetAndEventtypes.dataset === 'transactions' &&
  136. /^p\d{2,3}\(\)/.test(eventView.getYAxis())
  137. ) {
  138. // p95() -> p95(transaction.duration)
  139. aggregate = eventView.getYAxis().slice(0, 3) + '(transaction.duration)';
  140. }
  141. return {
  142. ...createDefaultRule(),
  143. ...datasetAndEventtypes,
  144. query: parsedQuery?.query ?? eventView.query,
  145. aggregate,
  146. environment: eventView.environment.length ? eventView.environment[0] : null,
  147. };
  148. }
  149. export function createRuleFromWizardTemplate(
  150. wizardTemplate: WizardRuleTemplate
  151. ): UnsavedIncidentRule {
  152. const {eventTypes, aggregate, dataset} = wizardTemplate;
  153. const defaultRuleOptions: Partial<UnsavedIncidentRule> = {};
  154. if (isSessionAggregate(aggregate)) {
  155. defaultRuleOptions.thresholdType = AlertRuleThresholdType.BELOW;
  156. defaultRuleOptions.timeWindow = TimeWindow.ONE_HOUR;
  157. }
  158. if (aggregate.includes('apdex')) {
  159. defaultRuleOptions.thresholdType = AlertRuleThresholdType.BELOW;
  160. }
  161. return {
  162. ...createDefaultRule(defaultRuleOptions),
  163. eventTypes: [eventTypes],
  164. aggregate,
  165. dataset,
  166. };
  167. }