Skip to content

Commit 4038339

Browse files
committed
feat(tracemetrics): Bypass metric field validation for equations
1 parent 7ae3663 commit 4038339

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/sentry/explore/endpoints/serializers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from rest_framework.serializers import ListField
55

66
from sentry.constants import ALL_ACCESS_PROJECTS
7+
from sentry.discover.arithmetic import is_equation
78
from sentry.explore.models import ExploreSavedQueryDataset
89
from sentry.utils.dates import parse_stats_period, validate_interval
910

@@ -222,9 +223,16 @@ def validate(self, data):
222223
raise serializers.ValidationError(
223224
"Metric field is only allowed for metrics dataset"
224225
)
225-
if data["dataset"] == "metrics" and "metric" not in q:
226+
227+
# the metrics field is only required for non-equation queries
228+
has_equations = any(
229+
is_equation(y_axis)
230+
for aggregate_field in q.get("aggregateField", [])
231+
for y_axis in aggregate_field.get("yAxes", [])
232+
)
233+
if data["dataset"] == "metrics" and not has_equations and "metric" not in q:
226234
raise serializers.ValidationError(
227-
"Metric field is required for metrics dataset"
235+
"Metric field is required for non-equation queries on the metrics dataset"
228236
)
229237
inner_query = {}
230238
for key in inner_query_keys:

tests/sentry/explore/endpoints/test_explore_saved_queries.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,3 +1391,25 @@ def test_post_with_empty_query_is_rejected(self) -> None:
13911391
},
13921392
)
13931393
assert response.status_code == 400
1394+
1395+
def test_post_with_equation_is_accepted(self) -> None:
1396+
with self.feature(self.features):
1397+
response = self.client.post(
1398+
self.url,
1399+
{
1400+
"name": "Equation query",
1401+
"projects": self.project_ids,
1402+
"dataset": "metrics",
1403+
"query": [
1404+
{
1405+
"aggregateField": [{"yAxes": ["equation|A + B"], "chartType": 1}],
1406+
"mode": "samples",
1407+
"fields": ["A", "B"],
1408+
"orderby": "-timestamp",
1409+
},
1410+
],
1411+
},
1412+
)
1413+
assert response.status_code == 201, response.content
1414+
data = response.data
1415+
assert data["query"][0].get("metric") is None

0 commit comments

Comments
 (0)