utils.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. getUseCaseFromMRI,
  3. isExtractedCustomMetric,
  4. parseField,
  5. } from 'sentry/utils/metrics/mri';
  6. import {Dataset, SessionsAggregate} from 'sentry/views/alerts/rules/metric/types';
  7. import {isInsightsMetricAlert} from 'sentry/views/alerts/rules/metric/utils/isInsightsMetricAlert';
  8. import type {MetricAlertType, WizardRuleTemplate} from './options';
  9. // A set of unique identifiers to be able to tie aggregate and dataset back to a wizard alert type
  10. const alertTypeIdentifiers: Record<
  11. Exclude<Dataset, 'search_issues'>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  12. Partial<Record<MetricAlertType, string>>
  13. > = {
  14. [Dataset.ERRORS]: {
  15. num_errors: 'count()',
  16. users_experiencing_errors: 'count_unique(user)',
  17. },
  18. [Dataset.TRANSACTIONS]: {
  19. throughput: 'count()',
  20. trans_duration: 'transaction.duration',
  21. apdex: 'apdex',
  22. failure_rate: 'failure_rate()',
  23. lcp: 'measurements.lcp',
  24. fid: 'measurements.fid',
  25. cls: 'measurements.cls',
  26. },
  27. [Dataset.GENERIC_METRICS]: {
  28. throughput: 'count()',
  29. trans_duration: 'transaction.duration',
  30. apdex: 'apdex',
  31. failure_rate: 'failure_rate()',
  32. lcp: 'measurements.lcp',
  33. fid: 'measurements.fid',
  34. cls: 'measurements.cls',
  35. },
  36. [Dataset.SESSIONS]: {
  37. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  38. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  39. },
  40. [Dataset.METRICS]: {
  41. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  42. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  43. },
  44. };
  45. /**
  46. * Given an aggregate and dataset object, will return the corresponding wizard alert type
  47. * e.g. {aggregate: 'count()', dataset: 'events'} will yield 'num_errors'
  48. * @param template
  49. */
  50. export function getAlertTypeFromAggregateDataset({
  51. aggregate,
  52. dataset,
  53. }: Pick<WizardRuleTemplate, 'aggregate' | 'dataset'>): MetricAlertType {
  54. const {mri: mri} = parseField(aggregate) ?? {};
  55. if (isInsightsMetricAlert(aggregate)) {
  56. return 'insights_metrics';
  57. }
  58. if (mri && getUseCaseFromMRI(mri) === 'spans') {
  59. return 'custom_metrics';
  60. }
  61. if (mri && getUseCaseFromMRI(mri) === 'custom') {
  62. return isExtractedCustomMetric({mri}) ? 'span_metrics' : 'custom_metrics';
  63. }
  64. const identifierForDataset = alertTypeIdentifiers[dataset];
  65. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  66. ([_alertType, identifier]) => identifier && aggregate.includes(identifier as string)
  67. );
  68. const alertType =
  69. matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as MetricAlertType);
  70. return alertType ? alertType : 'custom_transactions';
  71. }