Просмотр исходного кода

fix(release-health): Restrict project release count to metric ID (#34951)

The snuba query for get_project_releases_count was not restricted to any
metric ID, so it also counted (project, release) combinations of other
metrics (see added test case).
Joris Bayer 2 лет назад
Родитель
Сommit
6387f752a1
2 измененных файлов с 38 добавлено и 0 удалено
  1. 5 0
      src/sentry/release_health/metrics.py
  2. 33 0
      tests/snuba/sessions/test_sessions.py

+ 5 - 0
src/sentry/release_health/metrics.py

@@ -1458,8 +1458,13 @@ class MetricsReleaseHealthBackend(ReleaseHealthBackend):
         if scope in ["users", "crash_free_users"]:
             having.append(Condition(Function("uniq", [Column("value")], "value"), Op.GT, 0))
             match = Entity(EntityKey.MetricsSets.value)
+            mri = SessionMRI.USER
         else:
             match = Entity(EntityKey.MetricsCounters.value)
+            mri = SessionMRI.SESSION
+
+        metric_id = resolve(organization_id, mri.value)
+        where.append(Condition(Column("metric_id"), Op.EQ, metric_id))
 
         query_columns = [
             Function(

+ 33 - 0
tests/snuba/sessions/test_sessions.py

@@ -9,6 +9,7 @@ from sentry.release_health.base import OverviewStat
 from sentry.release_health.duplex import DuplexReleaseHealthBackend
 from sentry.release_health.metrics import MetricsReleaseHealthBackend
 from sentry.release_health.sessions import SessionsReleaseHealthBackend
+from sentry.snuba.dataset import EntityKey
 from sentry.snuba.sessions import _make_stats
 from sentry.testutils import SnubaTestCase, TestCase
 from sentry.testutils.cases import SessionMetricsTestCase
@@ -1028,6 +1029,38 @@ class GetProjectReleasesCountTest(TestCase, SnubaTestCase):
             == 0
         )
 
+    def test_with_other_metrics(self):
+        if not self.backend.is_metrics_based():
+            return
+
+        # Test no errors when no session data
+        org = self.create_organization()
+        proj = self.create_project(organization=org)
+
+        # Insert a different set metric:
+        self._send_buckets(
+            [
+                {
+                    "org_id": org.id,
+                    "project_id": proj.id,
+                    "metric_id": 666,  # any other metric ID
+                    "timestamp": time.time(),
+                    "tags": {},
+                    "type": "s",
+                    "value": [1, 2, 3],
+                    "retention_days": 90,
+                }
+            ],
+            entity=EntityKey.MetricsSets.value,
+        )
+
+        assert (
+            self.backend.get_project_releases_count(
+                org.id, [proj.id], "crash_free_users", stats_period="14d"
+            )
+            == 0
+        )
+
     def test(self):
         project_release_1 = self.create_release(self.project)
         other_project = self.create_project()