utils.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 type {MetricAlertType, WizardRuleTemplate} from './options';
  8. // A set of unique identifiers to be able to tie aggregate and dataset back to a wizard alert type
  9. const alertTypeIdentifiers: Record<
  10. Exclude<Dataset, 'search_issues'>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  11. Partial<Record<MetricAlertType, string>>
  12. > = {
  13. [Dataset.ERRORS]: {
  14. num_errors: 'count()',
  15. users_experiencing_errors: 'count_unique(user)',
  16. },
  17. [Dataset.TRANSACTIONS]: {
  18. throughput: 'count()',
  19. trans_duration: 'transaction.duration',
  20. apdex: 'apdex',
  21. failure_rate: 'failure_rate()',
  22. lcp: 'measurements.lcp',
  23. fid: 'measurements.fid',
  24. cls: 'measurements.cls',
  25. },
  26. [Dataset.GENERIC_METRICS]: {
  27. throughput: 'count()',
  28. trans_duration: 'transaction.duration',
  29. apdex: 'apdex',
  30. failure_rate: 'failure_rate()',
  31. lcp: 'measurements.lcp',
  32. fid: 'measurements.fid',
  33. cls: 'measurements.cls',
  34. },
  35. [Dataset.SESSIONS]: {
  36. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  37. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  38. },
  39. [Dataset.METRICS]: {
  40. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  41. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  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 (mri && getUseCaseFromMRI(mri) === 'spans') {
  55. return 'custom_metrics';
  56. }
  57. if (mri && getUseCaseFromMRI(mri) === 'custom') {
  58. return isExtractedCustomMetric({mri}) ? 'span_metrics' : 'custom_metrics';
  59. }
  60. const identifierForDataset = alertTypeIdentifiers[dataset];
  61. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  62. ([_alertType, identifier]) => identifier && aggregate.includes(identifier as string)
  63. );
  64. const alertType =
  65. matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as MetricAlertType);
  66. return alertType ? alertType : 'custom_transactions';
  67. }