Browse Source

fix(snuba): Allow common suffixes when validating referrers (#73734)

The query builder adds `".primary"` and `".secondary"` to the query
referrer string when it constructs compound queries (when fetching from
multiple tables). It's a pain to have to declare both a primary _and_ a
secondary _and_ a regular referrer for each of those cases, especially
because the fields in the query can change for all kinds of reasons.
Instead, add a list of common referrer suffixes that are considered
valid. Closes https://github.com/getsentry/sentry/issues/71570
George Gritsouk 8 months ago
parent
commit
e18ea4f2db
2 changed files with 27 additions and 2 deletions
  1. 12 2
      src/sentry/snuba/referrer.py
  2. 15 0
      tests/snuba/test_referrer.py

+ 12 - 2
src/sentry/snuba/referrer.py

@@ -887,15 +887,25 @@ class Referrer(Enum):
 
 VALUES = {referrer.value for referrer in Referrer}
 
+# These suffixes are automatically added by Query Builder code in certain conditions. Any valid referrer with these suffixes is still a valid referrer.
+VALID_SUFFIXES = ["primary", "secondary"]
+
 
 def validate_referrer(referrer: str | None) -> None:
     if not referrer:
         return
 
+    if referrer in VALUES:
+        return
+
+    for suffix in VALID_SUFFIXES:
+        if referrer.removesuffix(f".{suffix}") in VALUES:
+            return
+
     error_message = f"referrer {referrer} is not part of Referrer Enum"
+
     try:
-        if referrer not in VALUES:
-            raise Exception(error_message)
+        raise Exception(error_message)
     except Exception:
         metrics.incr("snql.sdk.api.new_referrers", tags={"referrer": referrer})
         logger.warning(error_message, exc_info=True)

+ 15 - 0
tests/snuba/test_referrer.py

@@ -30,6 +30,21 @@ class ReferrerTest(TestCase):
         validate_referrer("tsdb-modelid:4.user_count_snoozes")
         assert warn_log.call_count == 1
 
+    @patch("sentry.snuba.referrer.logger.warning")
+    def test_referrer_validate_common_suffixes(self, warn_log):
+        assert warn_log.call_count == 0
+        validate_referrer("api.performance.http.domain-summary-transactions-list")
+        validate_referrer("api.performance.http.domain-summary-transactions-list.primary")
+        validate_referrer("api.performance.http.domain-summary-transactions-list.secondary")
+        assert warn_log.call_count == 0
+
+    @patch("sentry.snuba.referrer.logger.warning")
+    def test_referrer_validate_uncommon_suffixes(self, warn_log):
+        assert warn_log.call_count == 0
+        validate_referrer("api.performance.http.domain-summary-transactions-list.special")
+        validate_referrer("api.performance.http.domain-summary-transactions-list.airyrpm")
+        assert warn_log.call_count == 2
+
     @patch("sentry.snuba.referrer.logger.warning")
     def test_referrer_validate_tsdb_models(self, warn_log):
         assert warn_log.call_count == 0