Browse Source

feat(trends): Add top events query param (#54526)

If `topEvents` param is passed then it's used instead of the default top
event count. This is done so that we can send a higher top event count
for experiments.
Dameli Ushbayeva 1 year ago
parent
commit
ab37b81e76

+ 7 - 2
src/sentry/api/endpoints/organization_events_trends_v2.py

@@ -35,7 +35,8 @@ REGRESSION = "regression"
 ANY = "any"
 TREND_TYPES = [IMPROVED, REGRESSION, ANY]
 
-TOP_EVENTS_LIMIT = 45
+DEFAULT_TOP_EVENTS_LIMIT = 45
+MAX_TOP_EVENTS_LIMIT = 1000
 EVENTS_PER_QUERY = 15
 DAY_GRANULARITY_IN_SECONDS = METRICS_GRANULARITIES[0]
 ONE_DAY_IN_SECONDS = 24 * 60 * 60  # 86,400 seconds
@@ -229,11 +230,15 @@ class OrganizationEventsNewTrendsStatsEndpoint(OrganizationEventsV2EndpointBase)
             return formatted_results
 
         def get_event_stats_metrics(_, user_query, params, rollup, zerofill_results, __):
+            top_event_limit = min(
+                int(request.GET.get("topEvents", DEFAULT_TOP_EVENTS_LIMIT)),
+                MAX_TOP_EVENTS_LIMIT,
+            )
             # Fetch transactions names with the highest event count
             top_events = get_top_events(
                 user_query=user_query,
                 params=params,
-                event_limit=TOP_EVENTS_LIMIT,
+                event_limit=top_event_limit,
                 referrer=Referrer.API_TRENDS_GET_EVENT_STATS_V2_TOP_EVENTS.value,
             )
 

+ 35 - 0
tests/sentry/api/endpoints/test_organization_events_trends_v2.py

@@ -267,3 +267,38 @@ class OrganizationEventsTrendsStatsV2EndpointTest(MetricsAPIBaseTestCase):
 
         assert len(result_stats) > 0
         assert len(result_stats.get(f"{self.project.slug},foo", [])) > 0
+
+    @mock.patch("sentry.api.endpoints.organization_events_trends_v2.get_trends")
+    def test_simple_with_top_events(self, mock_get_trends):
+        # store second metric but with lower count
+        self.store_performance_metric(
+            name=TransactionMRI.DURATION.value,
+            tags={"transaction": "bar"},
+            org_id=self.org.id,
+            project_id=self.project.id,
+            value=2,
+            hours_before_now=2,
+        )
+
+        with self.feature(self.features):
+            response = self.client.get(
+                self.url,
+                format="json",
+                data={
+                    "end": iso_format(self.now),
+                    "start": iso_format(self.now - timedelta(days=1)),
+                    "interval": "1h",
+                    "field": ["project", "transaction"],
+                    "query": "event.type:transaction",
+                    "project": self.project.id,
+                    "trendFunction": "p95(transaction.duration)",
+                    "topEvents": 1,
+                },
+            )
+
+        assert response.status_code == 200, response.content
+
+        trends_call_args_data = mock_get_trends.call_args[0][0]["data"]
+        assert len(trends_call_args_data.get(f"{self.project.slug},foo")) > 0
+        # checks that second transaction wasn't sent to the trends microservice
+        assert len(trends_call_args_data.get(f"{self.project.slug},bar", [])) == 0