utils.tsx 2.7 KB

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