utils.tsx 2.5 KB

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