Browse Source

feat(alerts): enable custom tags for on-demand alerts (#54836)

Ogi 1 year ago
parent
commit
1ad4805110

+ 2 - 7
static/app/utils/onDemandMetrics/index.spec.tsx

@@ -7,6 +7,7 @@ describe('isOnDemandQueryString', () => {
     expect(isOnDemandQueryString('transaction.duration:>1')).toBeTruthy();
     expect(isOnDemandQueryString('device.name:foo')).toBeTruthy();
     expect(isOnDemandQueryString('geo.region:>US')).toBeTruthy();
+    expect(isOnDemandQueryString('foo:bar')).toBeTruthy();
   });
 
   it('should return false for an alert that has only standard fields', () => {
@@ -23,6 +24,7 @@ describe('createOnDemandFilterWarning', () => {
     expect(getOnDemandFilterWarning('transaction.duration')).toBe(message);
     expect(getOnDemandFilterWarning('user.email')).toBe(message);
     expect(getOnDemandFilterWarning('device.family')).toBe(message);
+    expect(getOnDemandFilterWarning('foo.bar')).toBe(message);
   });
 
   it('should return null if the query key is a standard search key', () => {
@@ -32,11 +34,4 @@ describe('createOnDemandFilterWarning', () => {
       expect(getOnDemandFilterWarning(key)).toBe(null);
     });
   });
-
-  it('should return null if the query key is not a supported on-demand metrics key', () => {
-    const message = "This filter isn't supported";
-    const getOnDemandFilterWarning = createOnDemandFilterWarning(message);
-
-    expect(getOnDemandFilterWarning('not_a_valid_key')).toBe(null);
-  });
 });

+ 17 - 7
static/app/utils/onDemandMetrics/index.tsx

@@ -1,6 +1,6 @@
 import {ParseResult, parseSearch, Token} from 'sentry/components/searchSyntax/parser';
 import {Organization} from 'sentry/types';
-import {FieldKey} from 'sentry/utils/fields';
+import {FieldKey, getFieldDefinition} from 'sentry/utils/fields';
 import {
   ON_DEMAND_METRICS_SUPPORTED_TAGS,
   STANDARD_SEARCH_FIELD_KEYS,
@@ -14,12 +14,21 @@ function isOnDemandSupportedFilterKey(key: FieldKey): boolean {
   return ON_DEMAND_METRICS_SUPPORTED_TAGS.has(key);
 }
 
+function isCustomTag(key: FieldKey): boolean {
+  return !getFieldDefinition(key);
+}
+
 export function createOnDemandFilterWarning(warning: React.ReactNode) {
-  return (key: string) =>
-    !isStandardSearchFilterKey(key as FieldKey) &&
-    isOnDemandSupportedFilterKey(key as FieldKey)
-      ? warning
-      : null;
+  return (key: string) => {
+    const fieldKey = key as FieldKey;
+    if (isCustomTag(fieldKey)) {
+      return warning;
+    }
+    if (!isStandardSearchFilterKey(fieldKey) && isOnDemandSupportedFilterKey(fieldKey)) {
+      return warning;
+    }
+    return null;
+  };
 }
 
 export function isOnDemandQueryString(query: string): boolean {
@@ -33,7 +42,8 @@ export function isOnDemandQueryString(query: string): boolean {
   const isOnDemandSupportedSearch = searchFilterKeys.some(key =>
     ON_DEMAND_METRICS_SUPPORTED_TAGS.has(key)
   );
-  return !isStandardSearch && isOnDemandSupportedSearch;
+  const hasCustomTags = searchFilterKeys.some(isCustomTag);
+  return !isStandardSearch && (isOnDemandSupportedSearch || hasCustomTags);
 }
 
 type SearchFilterKey = FieldKey | null;

+ 3 - 4
static/app/views/alerts/rules/metric/ruleConditionsForm.tsx

@@ -480,9 +480,7 @@ class RuleConditionsForm extends PureComponent<Props, State> {
                     searchSource="alert_builder"
                     defaultQuery={initialData?.query ?? ''}
                     omitTags={datasetOmittedTags(dataset, organization)}
-                    {...(datasetSupportedTags(dataset, organization)
-                      ? {supportedTags: datasetSupportedTags(dataset, organization)}
-                      : {})}
+                    supportedTags={datasetSupportedTags(dataset, organization)}
                     includeSessionTagsValues={dataset === Dataset.SESSIONS}
                     disabled={disabled}
                     useFormWrapper={false}
@@ -492,7 +490,8 @@ class RuleConditionsForm extends PureComponent<Props, State> {
                     query={initialData.query}
                     // We only need strict validation for Transaction queries, everything else is fine
                     highlightUnsupportedTags={
-                      organization.features.includes('alert-allow-indexed')
+                      organization.features.includes('alert-allow-indexed') ||
+                      hasOnDemandMetricAlertFeature(organization)
                         ? false
                         : [Dataset.GENERIC_METRICS, Dataset.TRANSACTIONS].includes(
                             dataset