onDemandMetricAlert.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {ParseResult, parseSearch, Token} from 'sentry/components/searchSyntax/parser';
  2. import {AggregationKey, FieldKey} from 'sentry/utils/fields';
  3. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  4. export function isValidOnDemandMetricAlert(
  5. dataset: Dataset,
  6. aggregate: string,
  7. query: string
  8. ): boolean {
  9. if (!isOnDemandMetricAlert(dataset, query)) {
  10. return true;
  11. }
  12. const unsupportedAggregates = [
  13. AggregationKey.PERCENTILE,
  14. AggregationKey.APDEX,
  15. AggregationKey.FAILURE_RATE,
  16. ];
  17. return !unsupportedAggregates.some(agg => aggregate.includes(agg));
  18. }
  19. const STANDARD_SEARCH_FIELD_KEYS = new Set([
  20. FieldKey.RELEASE,
  21. FieldKey.DIST,
  22. FieldKey.ENVIRONMENT,
  23. FieldKey.TRANSACTION,
  24. FieldKey.PLATFORM,
  25. FieldKey.TRANSACTION_OP,
  26. FieldKey.TRANSACTION_STATUS,
  27. FieldKey.HTTP_METHOD,
  28. FieldKey.HTTP_STATUS_CODE,
  29. FieldKey.BROWSER_NAME,
  30. FieldKey.OS_NAME,
  31. FieldKey.GEO_COUNTRY_CODE,
  32. ]);
  33. /**
  34. * We determine that an alert is an on-demand metric alert if the query contains
  35. * one of the tags that are not supported by the standard metrics.
  36. */
  37. export function isOnDemandMetricAlert(dataset: Dataset, query: string): boolean {
  38. return dataset === Dataset.GENERIC_METRICS && hasNonStandardMetricSearchFilters(query);
  39. }
  40. export function hasNonStandardMetricSearchFilters(query: string): boolean {
  41. const tokens = parseSearch(query);
  42. if (!tokens) {
  43. return false;
  44. }
  45. const searchFilterKeys = getSearchFilterKeys(tokens);
  46. return searchFilterKeys.some(key => !STANDARD_SEARCH_FIELD_KEYS.has(key));
  47. }
  48. type SearchFilterKey = FieldKey | null;
  49. function getSearchFilterKeys(tokens: ParseResult): FieldKey[] {
  50. try {
  51. return getTokenKeys(tokens).filter(Boolean) as FieldKey[];
  52. } catch (e) {
  53. return [];
  54. }
  55. }
  56. function getTokenKeys(tokens: ParseResult): SearchFilterKey[] {
  57. return tokens.flatMap(getTokenKey);
  58. }
  59. function getTokenKey(token): SearchFilterKey[] | SearchFilterKey {
  60. if (token.type === Token.LOGIC_GROUP) {
  61. return getTokenKeys(token.inner);
  62. }
  63. if (token.type === Token.FILTER) {
  64. return token.key.value;
  65. }
  66. return null;
  67. }