ruleConditionsForm.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. import {Fragment, PureComponent} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import omit from 'lodash/omit';
  5. import pick from 'lodash/pick';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import {fetchTagValues} from 'sentry/actionCreators/tags';
  8. import type {Client} from 'sentry/api';
  9. import {
  10. OnDemandMetricAlert,
  11. OnDemandWarningIcon,
  12. } from 'sentry/components/alerts/onDemandMetricAlert';
  13. import {Alert} from 'sentry/components/core/alert';
  14. import {Select} from 'sentry/components/core/select';
  15. import {getHasTag} from 'sentry/components/events/searchBar';
  16. import {
  17. STATIC_FIELD_TAGS,
  18. STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS,
  19. STATIC_FIELD_TAGS_WITHOUT_TRACING,
  20. STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS,
  21. STATIC_SEMVER_TAGS,
  22. STATIC_SPAN_TAGS,
  23. } from 'sentry/components/events/searchBarFieldConstants';
  24. import {components} from 'sentry/components/forms/controls/reactSelectWrapper';
  25. import SelectField from 'sentry/components/forms/fields/selectField';
  26. import FormField from 'sentry/components/forms/formField';
  27. import IdBadge from 'sentry/components/idBadge';
  28. import ExternalLink from 'sentry/components/links/externalLink';
  29. import ListItem from 'sentry/components/list/listItem';
  30. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  31. import Panel from 'sentry/components/panels/panel';
  32. import PanelBody from 'sentry/components/panels/panelBody';
  33. import {EAPSpanSearchQueryBuilder} from 'sentry/components/performance/spanSearchQueryBuilder';
  34. import {SearchQueryBuilder} from 'sentry/components/searchQueryBuilder';
  35. import {InvalidReason} from 'sentry/components/searchSyntax/parser';
  36. import {t, tct} from 'sentry/locale';
  37. import {space} from 'sentry/styles/space';
  38. import type {SelectValue} from 'sentry/types/core';
  39. import type {Tag, TagCollection} from 'sentry/types/group';
  40. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  41. import type {Organization} from 'sentry/types/organization';
  42. import type {Environment, Project} from 'sentry/types/project';
  43. import {defined} from 'sentry/utils';
  44. import {isAggregateField, isMeasurement} from 'sentry/utils/discover/fields';
  45. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  46. import {getDisplayName} from 'sentry/utils/environment';
  47. import {
  48. ALLOWED_EXPLORE_VISUALIZE_AGGREGATES,
  49. DEVICE_CLASS_TAG_VALUES,
  50. FieldKind,
  51. isDeviceClass,
  52. } from 'sentry/utils/fields';
  53. import {
  54. getMeasurements,
  55. type MeasurementCollection,
  56. } from 'sentry/utils/measurements/measurements';
  57. import {getOnDemandKeys, isOnDemandQueryString} from 'sentry/utils/onDemandMetrics';
  58. import {hasOnDemandMetricAlertFeature} from 'sentry/utils/onDemandMetrics/features';
  59. import withApi from 'sentry/utils/withApi';
  60. import withProjects from 'sentry/utils/withProjects';
  61. import withTags from 'sentry/utils/withTags';
  62. import WizardField from 'sentry/views/alerts/rules/metric/wizardField';
  63. import {
  64. convertDatasetEventTypesToSource,
  65. DATA_SOURCE_LABELS,
  66. DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
  67. } from 'sentry/views/alerts/utils';
  68. import type {AlertType} from 'sentry/views/alerts/wizard/options';
  69. import {getSupportedAndOmittedTags} from 'sentry/views/alerts/wizard/options';
  70. import {
  71. SpanTagsContext,
  72. SpanTagsProvider,
  73. } from 'sentry/views/explore/contexts/spanTagsContext';
  74. import {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  75. import {getProjectOptions} from '../utils';
  76. import {isCrashFreeAlert} from './utils/isCrashFreeAlert';
  77. import {DEFAULT_AGGREGATE, DEFAULT_TRANSACTION_AGGREGATE} from './constants';
  78. import {AlertRuleComparisonType, Dataset, Datasource, TimeWindow} from './types';
  79. const TIME_WINDOW_MAP: Record<TimeWindow, string> = {
  80. [TimeWindow.ONE_MINUTE]: t('1 minute'),
  81. [TimeWindow.FIVE_MINUTES]: t('5 minutes'),
  82. [TimeWindow.TEN_MINUTES]: t('10 minutes'),
  83. [TimeWindow.FIFTEEN_MINUTES]: t('15 minutes'),
  84. [TimeWindow.THIRTY_MINUTES]: t('30 minutes'),
  85. [TimeWindow.ONE_HOUR]: t('1 hour'),
  86. [TimeWindow.TWO_HOURS]: t('2 hours'),
  87. [TimeWindow.FOUR_HOURS]: t('4 hours'),
  88. [TimeWindow.ONE_DAY]: t('24 hours'),
  89. };
  90. type Props = {
  91. aggregate: string;
  92. alertType: AlertType;
  93. api: Client;
  94. comparisonType: AlertRuleComparisonType;
  95. dataset: Dataset;
  96. disabled: boolean;
  97. isEditing: boolean;
  98. onComparisonDeltaChange: (value: number) => void;
  99. onFilterSearch: (query: string, isQueryValid: any) => void;
  100. onTimeWindowChange: (value: number) => void;
  101. organization: Organization;
  102. project: Project;
  103. projects: Project[];
  104. router: InjectedRouter;
  105. tags: TagCollection;
  106. thresholdChart: React.ReactNode;
  107. timeWindow: number;
  108. // optional props
  109. allowChangeEventTypes?: boolean;
  110. comparisonDelta?: number;
  111. disableProjectSelector?: boolean;
  112. isErrorMigration?: boolean;
  113. isExtrapolatedChartData?: boolean;
  114. isLowConfidenceChartData?: boolean;
  115. isOnDemandLimitReached?: boolean;
  116. isTransactionMigration?: boolean;
  117. loadingProjects?: boolean;
  118. };
  119. type State = {
  120. environments: Environment[] | null;
  121. filterKeys: TagCollection;
  122. measurements: MeasurementCollection;
  123. };
  124. class RuleConditionsForm extends PureComponent<Props, State> {
  125. state: State = {
  126. environments: null,
  127. measurements: {},
  128. filterKeys: {},
  129. };
  130. componentDidMount() {
  131. this.fetchData();
  132. const measurements = getMeasurements();
  133. const filterKeys = this.getFilterKeys();
  134. this.setState({measurements, filterKeys});
  135. }
  136. componentDidUpdate(prevProps: Props) {
  137. if (prevProps.project.id === this.props.project.id) {
  138. return;
  139. }
  140. this.fetchData();
  141. }
  142. getFilterKeys = () => {
  143. const {organization, dataset, tags} = this.props;
  144. const {measurements} = this.state;
  145. const measurementsWithKind = Object.keys(measurements).reduce(
  146. (measurement_tags, key) => {
  147. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  148. measurement_tags[key] = {
  149. ...measurements[key],
  150. kind: FieldKind.MEASUREMENT,
  151. };
  152. return measurement_tags;
  153. },
  154. {}
  155. );
  156. const orgHasPerformanceView = organization.features.includes('performance-view');
  157. const combinedTags: TagCollection =
  158. dataset === Dataset.ERRORS
  159. ? Object.assign({}, STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS)
  160. : dataset === Dataset.TRANSACTIONS
  161. ? Object.assign(
  162. {},
  163. measurementsWithKind,
  164. STATIC_SPAN_TAGS,
  165. STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS
  166. )
  167. : orgHasPerformanceView
  168. ? Object.assign({}, measurementsWithKind, STATIC_SPAN_TAGS, STATIC_FIELD_TAGS)
  169. : Object.assign({}, STATIC_FIELD_TAGS_WITHOUT_TRACING);
  170. const tagsWithKind = Object.keys(tags).reduce<Record<string, Tag>>((acc, key) => {
  171. acc[key] = {
  172. ...tags[key]!,
  173. kind: FieldKind.TAG,
  174. };
  175. return acc;
  176. }, {});
  177. const {omitTags} = getSupportedAndOmittedTags(dataset, organization);
  178. Object.assign(combinedTags, tagsWithKind, STATIC_SEMVER_TAGS);
  179. combinedTags.has = getHasTag(combinedTags);
  180. const list =
  181. omitTags && omitTags.length > 0 ? omit(combinedTags, omitTags) : combinedTags;
  182. return list;
  183. };
  184. formElemBaseStyle = {
  185. padding: `${space(0.5)}`,
  186. border: 'none',
  187. };
  188. async fetchData() {
  189. const {api, organization, project} = this.props;
  190. try {
  191. const environments = await api.requestPromise(
  192. `/projects/${organization.slug}/${project.slug}/environments/`,
  193. {
  194. query: {
  195. visibility: 'visible',
  196. },
  197. }
  198. );
  199. this.setState({environments});
  200. } catch (_err) {
  201. addErrorMessage(t('Unable to fetch environments'));
  202. }
  203. }
  204. getEventFieldValues = async (tag: any, query: any): Promise<string[]> => {
  205. const {api, organization, project, dataset, router} = this.props;
  206. if (isAggregateField(tag.key) || isMeasurement(tag.key)) {
  207. // We can't really auto suggest values for aggregate fields
  208. // or measurements, so we simply don't
  209. // NOTE: these in particular are for discover queries. We may not need/support these
  210. return Promise.resolve([]);
  211. }
  212. // device.class is stored as "numbers" in snuba, but we want to suggest high, medium,
  213. // and low search filter values because discover maps device.class to these values.
  214. if (isDeviceClass(tag.key)) {
  215. return Promise.resolve(DEVICE_CLASS_TAG_VALUES);
  216. }
  217. const values = await fetchTagValues({
  218. api,
  219. orgSlug: organization.slug,
  220. tagKey: tag.key,
  221. search: query,
  222. projectIds: [project.id],
  223. endpointParams: normalizeDateTimeParams(router.location.query), // allows searching for tags on transactions as well
  224. includeTransactions: true, // allows searching for tags on sessions as well
  225. includeSessions: dataset === Dataset.SESSIONS,
  226. });
  227. return values.filter(({name}) => defined(name)).map(({name}) => name);
  228. };
  229. get timeWindowOptions() {
  230. let options: Record<string, string> = TIME_WINDOW_MAP;
  231. if (isCrashFreeAlert(this.props.dataset)) {
  232. options = pick(TIME_WINDOW_MAP, [
  233. // TimeWindow.THIRTY_MINUTES, leaving this option out until we figure out the sub-hour session resolution chart limitations
  234. TimeWindow.ONE_HOUR,
  235. TimeWindow.TWO_HOURS,
  236. TimeWindow.FOUR_HOURS,
  237. TimeWindow.ONE_DAY,
  238. ]);
  239. }
  240. if (this.props.comparisonType === AlertRuleComparisonType.DYNAMIC) {
  241. options = pick(TIME_WINDOW_MAP, [
  242. TimeWindow.FIFTEEN_MINUTES,
  243. TimeWindow.THIRTY_MINUTES,
  244. TimeWindow.ONE_HOUR,
  245. ]);
  246. }
  247. if (this.props.dataset === Dataset.EVENTS_ANALYTICS_PLATFORM) {
  248. options = pick(TIME_WINDOW_MAP, [
  249. TimeWindow.FIVE_MINUTES,
  250. TimeWindow.TEN_MINUTES,
  251. TimeWindow.FIFTEEN_MINUTES,
  252. TimeWindow.THIRTY_MINUTES,
  253. TimeWindow.ONE_HOUR,
  254. TimeWindow.TWO_HOURS,
  255. TimeWindow.FOUR_HOURS,
  256. TimeWindow.ONE_DAY,
  257. ]);
  258. }
  259. return Object.entries(options).map(([value, label]) => ({
  260. value: parseInt(value, 10),
  261. label: tct('[timeWindow] interval', {
  262. timeWindow: label.slice(-1) === 's' ? label.slice(0, -1) : label,
  263. }),
  264. }));
  265. }
  266. get searchPlaceholder() {
  267. switch (this.props.dataset) {
  268. case Dataset.ERRORS:
  269. return t('Filter events by level, message, and other properties\u2026');
  270. case Dataset.METRICS:
  271. case Dataset.SESSIONS:
  272. return t('Filter sessions by release version\u2026');
  273. default:
  274. return t('Filter transactions by URL, tags, and other properties\u2026');
  275. }
  276. }
  277. get selectControlStyles() {
  278. return {
  279. control: (provided: {[x: string]: string | number | boolean}) => ({
  280. ...provided,
  281. minWidth: 200,
  282. maxWidth: 300,
  283. }),
  284. container: (provided: {[x: string]: string | number | boolean}) => ({
  285. ...provided,
  286. margin: `${space(0.5)}`,
  287. }),
  288. };
  289. }
  290. renderEventTypeFilter() {
  291. const {organization, disabled, alertType, isErrorMigration} = this.props;
  292. const dataSourceOptions = [
  293. {
  294. label: t('Errors'),
  295. options: [
  296. {
  297. value: Datasource.ERROR_DEFAULT,
  298. label: DATA_SOURCE_LABELS[Datasource.ERROR_DEFAULT],
  299. },
  300. {
  301. value: Datasource.DEFAULT,
  302. label: DATA_SOURCE_LABELS[Datasource.DEFAULT],
  303. },
  304. {
  305. value: Datasource.ERROR,
  306. label: DATA_SOURCE_LABELS[Datasource.ERROR],
  307. },
  308. ],
  309. },
  310. ];
  311. if (
  312. organization.features.includes('performance-view') &&
  313. alertType === 'custom_transactions'
  314. ) {
  315. dataSourceOptions.push({
  316. label: t('Transactions'),
  317. options: [
  318. {
  319. value: Datasource.TRANSACTION,
  320. label: DATA_SOURCE_LABELS[Datasource.TRANSACTION],
  321. },
  322. ],
  323. });
  324. }
  325. return (
  326. <FormField
  327. name="datasource"
  328. inline={false}
  329. style={{
  330. ...this.formElemBaseStyle,
  331. minWidth: 300,
  332. flex: 2,
  333. }}
  334. flexibleControlStateSize
  335. >
  336. {({onChange, onBlur, model}: any) => {
  337. const formDataset = model.getValue('dataset');
  338. const formEventTypes = model.getValue('eventTypes');
  339. const aggregate = model.getValue('aggregate');
  340. const mappedValue = convertDatasetEventTypesToSource(
  341. formDataset,
  342. formEventTypes
  343. );
  344. return (
  345. <Select
  346. value={mappedValue}
  347. inFieldLabel={t('Events: ')}
  348. onChange={({value}: any) => {
  349. onChange(value, {});
  350. onBlur(value, {});
  351. // Reset the aggregate to the default (which works across
  352. // datatypes), otherwise we may send snuba an invalid query
  353. // (transaction aggregate on events datasource = bad).
  354. const newAggregate =
  355. value === Datasource.TRANSACTION
  356. ? DEFAULT_TRANSACTION_AGGREGATE
  357. : DEFAULT_AGGREGATE;
  358. if (alertType === 'custom_transactions' && aggregate !== newAggregate) {
  359. model.setValue('aggregate', newAggregate);
  360. }
  361. // set the value of the dataset and event type from data source
  362. const {dataset: datasetFromDataSource, eventTypes} =
  363. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  364. DATA_SOURCE_TO_SET_AND_EVENT_TYPES[value] ?? {};
  365. model.setValue('dataset', datasetFromDataSource);
  366. model.setValue('eventTypes', eventTypes);
  367. }}
  368. options={dataSourceOptions}
  369. isDisabled={disabled || isErrorMigration}
  370. />
  371. );
  372. }}
  373. </FormField>
  374. );
  375. }
  376. renderProjectSelector() {
  377. const {
  378. project: _selectedProject,
  379. projects, // note: org projects
  380. disabled,
  381. organization,
  382. disableProjectSelector,
  383. } = this.props;
  384. const projectOptions = getProjectOptions({
  385. organization,
  386. projects,
  387. isFormDisabled: disabled,
  388. });
  389. return (
  390. <FormField
  391. name="projectId"
  392. inline={false}
  393. style={{
  394. ...this.formElemBaseStyle,
  395. minWidth: 300,
  396. flex: 2,
  397. }}
  398. flexibleControlStateSize
  399. >
  400. {({onChange, onBlur, model}: any) => {
  401. const selectedProject =
  402. projects.find(({id}) => id === model.getValue('projectId')) ||
  403. _selectedProject;
  404. return (
  405. <Select
  406. isDisabled={disabled || disableProjectSelector}
  407. value={selectedProject.id}
  408. options={projectOptions}
  409. onChange={({value}: {value: Project['id']}) => {
  410. // if the current owner/team isn't part of project selected, update to the first available team
  411. const nextSelectedProject =
  412. projects.find(({id}) => id === value) ?? selectedProject;
  413. const ownerId: string | undefined = model
  414. .getValue('owner')
  415. ?.split(':')[1];
  416. if (
  417. ownerId &&
  418. nextSelectedProject.teams.find(({id}) => id === ownerId) ===
  419. undefined &&
  420. nextSelectedProject.teams.length
  421. ) {
  422. model.setValue('owner', `team:${nextSelectedProject.teams[0]!.id}`);
  423. }
  424. onChange(value, {});
  425. onBlur(value, {});
  426. }}
  427. components={{
  428. SingleValue: (containerProps: any) => (
  429. <components.ValueContainer {...containerProps}>
  430. <IdBadge
  431. project={selectedProject}
  432. avatarProps={{consistentWidth: true}}
  433. avatarSize={18}
  434. disableLink
  435. />
  436. </components.ValueContainer>
  437. ),
  438. }}
  439. />
  440. );
  441. }}
  442. </FormField>
  443. );
  444. }
  445. renderInterval() {
  446. const {organization, timeWindow, disabled, alertType, project, onTimeWindowChange} =
  447. this.props;
  448. return (
  449. <Fragment>
  450. <StyledListItem>
  451. <StyledListTitle>
  452. <div>{t('Define your metric')}</div>
  453. </StyledListTitle>
  454. </StyledListItem>
  455. <FormRow>
  456. <WizardField
  457. name="aggregate"
  458. help={null}
  459. organization={organization}
  460. disabled={disabled}
  461. project={project}
  462. style={{
  463. ...this.formElemBaseStyle,
  464. flex: 1,
  465. }}
  466. inline={false}
  467. flexibleControlStateSize
  468. columnWidth={200}
  469. alertType={alertType}
  470. required
  471. />
  472. <Select
  473. name="timeWindow"
  474. styles={this.selectControlStyles}
  475. options={this.timeWindowOptions}
  476. isDisabled={disabled}
  477. value={timeWindow}
  478. onChange={({value}: any) => onTimeWindowChange(value)}
  479. inline={false}
  480. flexibleControlStateSize
  481. />
  482. </FormRow>
  483. </Fragment>
  484. );
  485. }
  486. render() {
  487. const {
  488. alertType,
  489. organization,
  490. disabled,
  491. onFilterSearch,
  492. allowChangeEventTypes,
  493. dataset,
  494. isExtrapolatedChartData,
  495. isTransactionMigration,
  496. isErrorMigration,
  497. project,
  498. comparisonType,
  499. isLowConfidenceChartData,
  500. isOnDemandLimitReached,
  501. } = this.props;
  502. const {environments, filterKeys} = this.state;
  503. const environmentOptions: Array<SelectValue<string | null>> = [
  504. {
  505. value: null,
  506. label: t('All Environments'),
  507. },
  508. ...(environments?.map(env => ({value: env.name, label: getDisplayName(env)})) ??
  509. []),
  510. ];
  511. const confidenceEnabled = hasEAPAlerts(organization);
  512. return (
  513. <Fragment>
  514. <ChartPanel>
  515. <StyledPanelBody>{this.props.thresholdChart}</StyledPanelBody>
  516. </ChartPanel>
  517. {isTransactionMigration ? (
  518. <Fragment>
  519. <Spacer />
  520. <HiddenListItem />
  521. <HiddenListItem />
  522. </Fragment>
  523. ) : (
  524. <Fragment>
  525. <SpanTagsProvider
  526. dataset={DiscoverDatasets.SPANS_EAP}
  527. enabled={
  528. organization.features.includes('alerts-eap') &&
  529. alertType === 'eap_metrics'
  530. }
  531. >
  532. {isExtrapolatedChartData && (
  533. <OnDemandMetricAlert
  534. message={t(
  535. 'The chart data above is an estimate based on the stored transactions that match the filters specified.'
  536. )}
  537. />
  538. )}
  539. {confidenceEnabled && isLowConfidenceChartData && (
  540. <Alert.Container>
  541. <Alert showIcon type="warning">
  542. {t(
  543. 'Your low sample count may impact the accuracy of this alert. Edit your query or increase your sampling rate.'
  544. )}
  545. </Alert>
  546. </Alert.Container>
  547. )}
  548. {!isErrorMigration && this.renderInterval()}
  549. <StyledListItem>{t('Filter events')}</StyledListItem>
  550. <FormRow noMargin columns={1 + (allowChangeEventTypes ? 1 : 0) + 1}>
  551. {this.renderProjectSelector()}
  552. <SelectField
  553. name="environment"
  554. placeholder={t('All Environments')}
  555. style={{
  556. ...this.formElemBaseStyle,
  557. minWidth: 230,
  558. flex: 1,
  559. }}
  560. styles={{
  561. singleValue: (base: any) => ({
  562. ...base,
  563. }),
  564. option: (base: any) => ({
  565. ...base,
  566. }),
  567. }}
  568. options={environmentOptions}
  569. isDisabled={
  570. disabled || this.state.environments === null || isErrorMigration
  571. }
  572. isClearable
  573. inline={false}
  574. flexibleControlStateSize
  575. />
  576. {allowChangeEventTypes && this.renderEventTypeFilter()}
  577. </FormRow>
  578. <FormRow noMargin>
  579. <FormField
  580. name="query"
  581. inline={false}
  582. style={{
  583. ...this.formElemBaseStyle,
  584. flex: '6 0 500px',
  585. }}
  586. flexibleControlStateSize
  587. >
  588. {({onChange, onBlur, initialData, value}: any) => {
  589. return alertType === 'eap_metrics' ? (
  590. <SpanTagsContext.Consumer>
  591. {tags => (
  592. <EAPSpanSearchQueryBuilder
  593. numberTags={tags?.number ?? {}}
  594. stringTags={tags?.string ?? {}}
  595. initialQuery={value ?? ''}
  596. searchSource="alerts"
  597. onSearch={(query, {parsedQuery}) => {
  598. onFilterSearch(query, parsedQuery);
  599. onChange(query, {});
  600. }}
  601. supportedAggregates={ALLOWED_EXPLORE_VISUALIZE_AGGREGATES}
  602. projects={[parseInt(project.id, 10)]}
  603. />
  604. )}
  605. </SpanTagsContext.Consumer>
  606. ) : (
  607. <SearchContainer>
  608. <SearchQueryBuilder
  609. initialQuery={initialData?.query ?? ''}
  610. getTagValues={this.getEventFieldValues}
  611. placeholder={this.searchPlaceholder}
  612. searchSource="alert_builder"
  613. filterKeys={filterKeys}
  614. disabled={disabled || isErrorMigration}
  615. onChange={onChange}
  616. invalidMessages={{
  617. [InvalidReason.WILDCARD_NOT_ALLOWED]: t(
  618. 'The wildcard operator is not supported here.'
  619. ),
  620. [InvalidReason.FREE_TEXT_NOT_ALLOWED]: t(
  621. 'Free text search is not allowed. If you want to partially match transaction names, use glob patterns like "transaction:*transaction-name*"'
  622. ),
  623. }}
  624. onSearch={query => {
  625. onFilterSearch(query, true);
  626. onChange(query, {});
  627. }}
  628. onBlur={(query, {parsedQuery}) => {
  629. onFilterSearch(query, parsedQuery);
  630. onBlur(query);
  631. }}
  632. // We only need strict validation for Transaction queries, everything else is fine
  633. disallowUnsupportedFilters={
  634. organization.features.includes('alert-allow-indexed') ||
  635. (hasOnDemandMetricAlertFeature(organization) &&
  636. isOnDemandQueryString(value))
  637. ? false
  638. : dataset === Dataset.GENERIC_METRICS
  639. }
  640. />
  641. {isExtrapolatedChartData &&
  642. isOnDemandQueryString(value) &&
  643. (isOnDemandLimitReached ? (
  644. <OnDemandWarningIcon
  645. color="red400"
  646. msg={tct(
  647. 'We don’t routinely collect metrics from [fields] and you’ve already reached the limit of [docLink:alerts with advanced filters] for your organization.',
  648. {
  649. fields: (
  650. <strong>
  651. {getOnDemandKeys(value)
  652. .map(key => `"${key}"`)
  653. .join(', ')}
  654. </strong>
  655. ),
  656. docLink: (
  657. <ExternalLink href="https://docs.sentry.io/product/alerts/create-alerts/metric-alert-config/#advanced-filters-for-transactions" />
  658. ),
  659. }
  660. )}
  661. isHoverable
  662. />
  663. ) : (
  664. <OnDemandWarningIcon
  665. color="gray500"
  666. msg={tct(
  667. 'We don’t routinely collect metrics from [fields]. However, we’ll do so [strong:once this alert has been saved.]',
  668. {
  669. fields: (
  670. <strong>
  671. {getOnDemandKeys(value)
  672. .map(key => `"${key}"`)
  673. .join(', ')}
  674. </strong>
  675. ),
  676. strong: <strong />,
  677. }
  678. )}
  679. />
  680. ))}
  681. </SearchContainer>
  682. );
  683. }}
  684. </FormField>
  685. </FormRow>
  686. <FormRow noMargin>
  687. <FormField
  688. name="query"
  689. inline={false}
  690. style={{
  691. ...this.formElemBaseStyle,
  692. flex: '6 0 500px',
  693. }}
  694. flexibleControlStateSize
  695. >
  696. {(args: any) => {
  697. if (
  698. args.value?.includes('is:unresolved') &&
  699. comparisonType === AlertRuleComparisonType.DYNAMIC
  700. ) {
  701. return (
  702. <OnDemandMetricAlert
  703. message={t(
  704. "'is:unresolved' queries are not supported by Anomaly Detection alerts."
  705. )}
  706. />
  707. );
  708. }
  709. return null;
  710. }}
  711. </FormField>
  712. </FormRow>
  713. </SpanTagsProvider>
  714. </Fragment>
  715. )}
  716. </Fragment>
  717. );
  718. }
  719. }
  720. const StyledListTitle = styled('div')`
  721. display: flex;
  722. span {
  723. margin-left: ${space(1)};
  724. }
  725. `;
  726. // This is a temporary hacky solution to hide list items without changing the numbering of the rest of the list
  727. // TODO(issues): Remove this once the migration is complete
  728. const HiddenListItem = styled(ListItem)`
  729. position: absolute;
  730. width: 0px;
  731. height: 0px;
  732. opacity: 0;
  733. pointer-events: none;
  734. `;
  735. const Spacer = styled('div')`
  736. margin-bottom: ${space(2)};
  737. `;
  738. const ChartPanel = styled(Panel)`
  739. margin-bottom: ${space(1)};
  740. `;
  741. const StyledPanelBody = styled(PanelBody)`
  742. ol,
  743. h4 {
  744. margin-bottom: ${space(1)};
  745. }
  746. `;
  747. const SearchContainer = styled('div')`
  748. display: flex;
  749. align-items: center;
  750. gap: ${space(1)};
  751. `;
  752. const StyledListItem = styled(ListItem)`
  753. margin-bottom: ${space(0.5)};
  754. font-size: ${p => p.theme.fontSizeExtraLarge};
  755. line-height: 1.3;
  756. `;
  757. const FormRow = styled('div')<{columns?: number; noMargin?: boolean}>`
  758. display: flex;
  759. flex-direction: row;
  760. align-items: center;
  761. flex-wrap: wrap;
  762. margin-bottom: ${p => (p.noMargin ? 0 : space(4))};
  763. ${p =>
  764. p.columns !== undefined &&
  765. css`
  766. display: grid;
  767. grid-template-columns: repeat(${p.columns}, auto);
  768. `}
  769. `;
  770. export default withApi(withProjects(withTags(RuleConditionsForm)));