extractMetric.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {Fragment, useCallback} from 'react';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import Alert from 'sentry/components/alert';
  4. import Panel from 'sentry/components/panels/panel';
  5. import PanelBody from 'sentry/components/panels/panelBody';
  6. import PanelHeader from 'sentry/components/panels/panelHeader';
  7. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  8. import {t} from 'sentry/locale';
  9. import type {Project} from 'sentry/types/project';
  10. import {hasCustomMetricsExtractionRules} from 'sentry/utils/metrics/features';
  11. import routeTitleGen from 'sentry/utils/routeTitle';
  12. import {useNavigate} from 'sentry/utils/useNavigate';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  15. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  16. import {
  17. type FormData,
  18. MetricsExtractionRuleForm,
  19. } from 'sentry/views/settings/projectMetrics/metricsExtractionRuleForm';
  20. import {
  21. type MetricsExtractionRule,
  22. useCreateMetricsExtractionRules,
  23. } from 'sentry/views/settings/projectMetrics/utils/api';
  24. const INITIAL_DATA: FormData = {
  25. spanAttribute: null,
  26. type: 'c',
  27. tags: ['release', 'environment'],
  28. conditions: [''],
  29. };
  30. const PAGE_TITLE = t('Extract Metric');
  31. function ExtractMetric({project}: {project: Project}) {
  32. const navigate = useNavigate();
  33. const organization = useOrganization();
  34. const createExtractionRuleMutation = useCreateMetricsExtractionRules(
  35. organization.slug,
  36. project.slug
  37. );
  38. const handleSubmit = useCallback(
  39. (
  40. formData: FormData,
  41. onSubmitSuccess: (data: FormData) => void,
  42. onSubmitError: (error: any) => void
  43. ) => {
  44. const data = formData as FormData;
  45. const extractionRule: MetricsExtractionRule = {
  46. spanAttribute: data.spanAttribute!,
  47. tags: data.tags,
  48. type: data.type!,
  49. unit: 'none',
  50. conditions: data.conditions.filter(Boolean),
  51. };
  52. createExtractionRuleMutation.mutate(
  53. {
  54. metricsExtractionRules: [extractionRule],
  55. },
  56. {
  57. onSuccess: () => {
  58. onSubmitSuccess(data);
  59. addSuccessMessage(t('Metric extraction rule created'));
  60. navigate(-1);
  61. },
  62. onError: error => {
  63. const message = error?.responseJSON?.detail
  64. ? (error.responseJSON.detail as string)
  65. : t('Unable to save your changes.');
  66. onSubmitError(message);
  67. addErrorMessage(message);
  68. },
  69. }
  70. );
  71. onSubmitSuccess(data);
  72. },
  73. [createExtractionRuleMutation, navigate]
  74. );
  75. if (!hasCustomMetricsExtractionRules(organization)) {
  76. return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
  77. }
  78. return (
  79. <Fragment>
  80. <SentryDocumentTitle title={routeTitleGen(PAGE_TITLE, project.slug, false)} />
  81. <SettingsPageHeader title={PAGE_TITLE} />
  82. <TextBlock>
  83. {t(
  84. 'Metric Extraction Rules enable you to derive meaningful metrics from the attributes present on spans within your application.'
  85. )}
  86. </TextBlock>
  87. <TextBlock>
  88. {t(
  89. "By defining these rules, you can specify how and which attributes should be processed to generate useful metrics that provide detailed insights into your application's performance and behavior."
  90. )}
  91. </TextBlock>
  92. <Panel>
  93. <PanelHeader>{t('Create Extraction Rule')}</PanelHeader>
  94. <PanelBody>
  95. <MetricsExtractionRuleForm
  96. project={project}
  97. initialData={INITIAL_DATA}
  98. onCancel={() => navigate(-1)}
  99. submitLabel={t('Confirm')}
  100. onSubmit={handleSubmit}
  101. requireChanges
  102. />
  103. </PanelBody>
  104. </Panel>
  105. </Fragment>
  106. );
  107. }
  108. export default ExtractMetric;