constants.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {t} from 'sentry/locale';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {AggregationKeyWithAlias, LooseFieldKey} from 'sentry/utils/discover/fields';
  4. import {AggregationKey} from 'sentry/utils/fields';
  5. import {WEB_VITAL_DETAILS} from 'sentry/utils/performance/vitals/constants';
  6. import {
  7. AlertRuleComparisonType,
  8. AlertRuleThresholdType,
  9. AlertRuleTriggerType,
  10. Dataset,
  11. Datasource,
  12. EventTypes,
  13. TimeWindow,
  14. Trigger,
  15. UnsavedMetricRule,
  16. } from 'sentry/views/alerts/rules/metric/types';
  17. import {
  18. DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
  19. getQueryDatasource,
  20. isSessionAggregate,
  21. } from 'sentry/views/alerts/utils';
  22. import {AlertType, WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  23. export const DEFAULT_COUNT_TIME_WINDOW = 1; // 1min
  24. export const DEFAULT_CHANGE_TIME_WINDOW = 60; // 1h
  25. export const DEFAULT_CHANGE_COMP_DELTA = 10080; // 1w
  26. export const DEFAULT_AGGREGATE = 'count()';
  27. export const DEFAULT_TRANSACTION_AGGREGATE = 'p95(transaction.duration)';
  28. export const DATASET_EVENT_TYPE_FILTERS = {
  29. [Dataset.ERRORS]: 'event.type:error',
  30. [Dataset.TRANSACTIONS]: 'event.type:transaction',
  31. [Dataset.GENERIC_METRICS]: 'event.type:transaction',
  32. } as const;
  33. export const DATASOURCE_EVENT_TYPE_FILTERS = {
  34. [Datasource.ERROR_DEFAULT]: 'event.type:[error, default]',
  35. [Datasource.ERROR]: 'event.type:error',
  36. [Datasource.DEFAULT]: 'event.type:default',
  37. [Datasource.TRANSACTION]: 'event.type:transaction',
  38. } as const;
  39. export type OptionConfig = {
  40. aggregations: AggregationKeyWithAlias[];
  41. fields: LooseFieldKey[];
  42. measurementKeys?: string[];
  43. };
  44. /**
  45. * Allowed error aggregations for alerts
  46. */
  47. export const errorFieldConfig: OptionConfig = {
  48. aggregations: [AggregationKey.COUNT, AggregationKey.COUNT_UNIQUE],
  49. fields: ['user'],
  50. };
  51. const commonAggregations = [
  52. AggregationKey.AVG,
  53. AggregationKey.PERCENTILE,
  54. AggregationKey.P50,
  55. AggregationKey.P75,
  56. AggregationKey.P95,
  57. AggregationKey.P99,
  58. AggregationKey.P100,
  59. ];
  60. const allAggregations = [
  61. ...commonAggregations,
  62. AggregationKey.FAILURE_RATE,
  63. AggregationKey.APDEX,
  64. AggregationKey.COUNT,
  65. ];
  66. export const DuplicateMetricFields: string[] = [
  67. 'dataset',
  68. 'eventTypes',
  69. 'aggregate',
  70. 'query',
  71. 'timeWindow',
  72. 'thresholdPeriod',
  73. 'projects',
  74. 'environment',
  75. 'resolveThreshold',
  76. 'thresholdType',
  77. 'owner',
  78. 'name',
  79. 'projectId',
  80. 'comparisonDelta',
  81. ];
  82. export const DuplicateTriggerFields: string[] = ['alertThreshold', 'label'];
  83. export const DuplicateActionFields: string[] = [
  84. 'type',
  85. 'targetType',
  86. 'targetIdentifier',
  87. 'inputChannelId',
  88. 'options',
  89. ];
  90. export const COMPARISON_DELTA_OPTIONS = [
  91. {value: 5, label: t('same time 5 minutes ago')}, // 5 minutes
  92. {value: 15, label: t('same time 15 minutes ago')}, // 15 minutes
  93. {value: 60, label: t('same time one hour ago')}, // one hour
  94. {value: 1440, label: t('same time one day ago')}, // one day
  95. {value: 10080, label: t('same time one week ago')}, // one week
  96. {value: 43200, label: t('same time one month ago')}, // 30 days
  97. ];
  98. export function getWizardAlertFieldConfig(
  99. alertType: AlertType,
  100. dataset: Dataset
  101. ): OptionConfig {
  102. if (alertType === 'custom' && dataset === Dataset.ERRORS) {
  103. return errorFieldConfig;
  104. }
  105. // If user selected apdex we must include that in the OptionConfig as it has a user specified column
  106. const aggregations =
  107. alertType === 'apdex' || alertType === 'custom'
  108. ? allAggregations
  109. : commonAggregations;
  110. return {
  111. aggregations,
  112. fields: ['transaction.duration'],
  113. measurementKeys: Object.keys(WEB_VITAL_DETAILS),
  114. };
  115. }
  116. /**
  117. * Allowed transaction aggregations for alerts
  118. */
  119. export const transactionFieldConfig: OptionConfig = {
  120. aggregations: allAggregations,
  121. fields: ['transaction.duration'],
  122. measurementKeys: Object.keys(WEB_VITAL_DETAILS),
  123. };
  124. export function createDefaultTrigger(label: AlertRuleTriggerType): Trigger {
  125. return {
  126. label,
  127. alertThreshold: '',
  128. actions: [],
  129. };
  130. }
  131. export function createDefaultRule(
  132. defaultRuleOptions: Partial<UnsavedMetricRule> = {}
  133. ): UnsavedMetricRule {
  134. return {
  135. dataset: Dataset.ERRORS,
  136. eventTypes: [EventTypes.ERROR],
  137. aggregate: DEFAULT_AGGREGATE,
  138. query: '',
  139. timeWindow: 60,
  140. thresholdPeriod: 1,
  141. triggers: [
  142. createDefaultTrigger(AlertRuleTriggerType.CRITICAL),
  143. createDefaultTrigger(AlertRuleTriggerType.WARNING),
  144. ],
  145. projects: [],
  146. environment: null,
  147. resolveThreshold: '',
  148. thresholdType: AlertRuleThresholdType.ABOVE,
  149. ...defaultRuleOptions,
  150. };
  151. }
  152. /**
  153. * Create an unsaved alert from a discover EventView object
  154. */
  155. export function createRuleFromEventView(eventView: EventView): UnsavedMetricRule {
  156. const parsedQuery = getQueryDatasource(eventView.query);
  157. const datasetAndEventtypes = parsedQuery
  158. ? DATA_SOURCE_TO_SET_AND_EVENT_TYPES[parsedQuery.source]
  159. : DATA_SOURCE_TO_SET_AND_EVENT_TYPES.error;
  160. let aggregate = eventView.getYAxis();
  161. if (
  162. datasetAndEventtypes.dataset === 'transactions' &&
  163. /^p\d{2,3}\(\)/.test(eventView.getYAxis())
  164. ) {
  165. // p95() -> p95(transaction.duration)
  166. aggregate = eventView.getYAxis().slice(0, 3) + '(transaction.duration)';
  167. }
  168. return {
  169. ...createDefaultRule(),
  170. ...datasetAndEventtypes,
  171. query: parsedQuery?.query ?? eventView.query,
  172. aggregate,
  173. environment: eventView.environment.length ? eventView.environment[0] : null,
  174. };
  175. }
  176. export function createRuleFromWizardTemplate(
  177. wizardTemplate: WizardRuleTemplate
  178. ): UnsavedMetricRule {
  179. const {eventTypes, aggregate, dataset} = wizardTemplate;
  180. const defaultRuleOptions: Partial<UnsavedMetricRule> = {};
  181. if (isSessionAggregate(aggregate)) {
  182. defaultRuleOptions.thresholdType = AlertRuleThresholdType.BELOW;
  183. defaultRuleOptions.timeWindow = TimeWindow.ONE_HOUR;
  184. }
  185. if (aggregate.includes('apdex')) {
  186. defaultRuleOptions.thresholdType = AlertRuleThresholdType.BELOW;
  187. }
  188. return {
  189. ...createDefaultRule(defaultRuleOptions),
  190. eventTypes: [eventTypes],
  191. aggregate,
  192. dataset,
  193. };
  194. }
  195. export function getThresholdUnits(
  196. aggregate: string,
  197. comparisonType: AlertRuleComparisonType
  198. ): string {
  199. // cls is a number not a measurement of time
  200. if (
  201. isSessionAggregate(aggregate) ||
  202. comparisonType === AlertRuleComparisonType.CHANGE
  203. ) {
  204. return '%';
  205. }
  206. if (aggregate.includes('measurements.cls')) {
  207. return '';
  208. }
  209. if (aggregate.includes('duration') || aggregate.includes('measurements')) {
  210. return 'ms';
  211. }
  212. return '';
  213. }