Skip to content

Commit e0a04bb

Browse files
authored
fix(functions): Units on function stats response (#59143)
Using an alias prefix broke the units on the response. Make sure it is correctly mapped back to the expected aliases with the correct units set.
1 parent 28c52c8 commit e0a04bb

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/sentry/search/events/builder/profile_functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ def strip_alias_prefix(self, result):
5757
column: get_function_alias(function_details.field)
5858
for column, function_details in self.function_alias_map.items()
5959
}
60+
self.function_alias_map = {
61+
alias_mappings.get(column, column): function_details
62+
for column, function_details in self.function_alias_map.items()
63+
}
6064
result["data"] = [
6165
{alias_mappings.get(k, k): v for k, v in item.items()}
6266
for item in result.get("data", [])
6367
]
68+
for item in result.get("meta", []):
69+
item["name"] = alias_mappings.get(item["name"], item["name"])
6470
return result
6571

6672
@property

src/sentry/search/events/datasets/profile_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,27 @@ def function_converter(self) -> Mapping[str, SnQLFunction]:
243243
SnQLFunction(
244244
"cpm", # calls per minute
245245
snql_aggregate=lambda args, alias: self._resolve_cpm(args, alias),
246-
default_result_type="integer",
246+
default_result_type="number",
247247
),
248248
SnQLFunction(
249249
"cpm_before",
250250
required_args=[TimestampArg("timestamp")],
251251
snql_aggregate=lambda args, alias: self._resolve_cpm_cond(args, alias, "less"),
252-
default_result_type="integer",
252+
default_result_type="number",
253253
),
254254
SnQLFunction(
255255
"cpm_after",
256256
required_args=[TimestampArg("timestamp")],
257257
snql_aggregate=lambda args, alias: self._resolve_cpm_cond(
258258
args, alias, "greater"
259259
),
260-
default_result_type="integer",
260+
default_result_type="number",
261261
),
262262
SnQLFunction(
263263
"cpm_delta",
264264
required_args=[TimestampArg("timestamp")],
265265
snql_aggregate=self._resolve_cpm_delta,
266-
default_result_type="integer",
266+
default_result_type="number",
267267
),
268268
SnQLFunction(
269269
"count_unique",

tests/snuba/api/endpoints/test_organization_events_stats.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,14 +2611,30 @@ def test_functions_dataset_simple(self):
26112611
"start": iso_format(self.three_days_ago),
26122612
"end": iso_format(self.one_day_ago),
26132613
"interval": "1d",
2614-
"yAxis": "cpm()",
2614+
"yAxis": ["cpm()", "p95(function.duration)"],
26152615
}
26162616

26172617
response = self.client.get(self.url, data=data, format="json")
26182618
assert response.status_code == 200, response.content
2619-
assert sum(row[1][0]["count"] for row in response.data["data"]) == pytest.approx(
2619+
2620+
assert sum(row[1][0]["count"] for row in response.data["cpm()"]["data"]) == pytest.approx(
26202621
100 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
26212622
)
2623+
assert any(
2624+
row[1][0]["count"] > 0 for row in response.data["p95(function.duration)"]["data"]
2625+
)
2626+
2627+
for y_axis in ["cpm()", "p95(function.duration)"]:
2628+
assert response.data[y_axis]["meta"]["fields"] == {
2629+
"time": "date",
2630+
"cpm": "number",
2631+
"p95_function_duration": "duration",
2632+
}
2633+
assert response.data[y_axis]["meta"]["units"] == {
2634+
"time": None,
2635+
"cpm": None,
2636+
"p95_function_duration": "nanosecond",
2637+
}
26222638

26232639

26242640
@region_silo_test
@@ -2667,17 +2683,28 @@ def test_functions_dataset_simple(self):
26672683
"field": ["function", "count()"],
26682684
"start": iso_format(self.three_days_ago),
26692685
"end": iso_format(self.one_day_ago),
2670-
"yAxis": "cpm()",
2686+
"yAxis": ["cpm()", "p95(function.duration)"],
26712687
"interval": "1d",
26722688
"topEvents": 2,
26732689
"excludeOther": 1,
26742690
}
26752691

26762692
response = self.client.get(self.url, data=data, format="json")
26772693
assert response.status_code == 200, response.content
2678-
assert sum(row[1][0]["count"] for row in response.data["foo"]["data"]) == pytest.approx(
2694+
assert sum(
2695+
row[1][0]["count"] for row in response.data["foo"]["cpm()"]["data"]
2696+
) == pytest.approx(
26792697
100 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
26802698
)
2681-
assert sum(row[1][0]["count"] for row in response.data["bar"]["data"]) == pytest.approx(
2699+
assert sum(
2700+
row[1][0]["count"] for row in response.data["bar"]["cpm()"]["data"]
2701+
) == pytest.approx(
26822702
10 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
26832703
)
2704+
2705+
assert any(
2706+
row[1][0]["count"] > 0 for row in response.data["foo"]["p95(function.duration)"]["data"]
2707+
)
2708+
assert any(
2709+
row[1][0]["count"] > 0 for row in response.data["bar"]["p95(function.duration)"]["data"]
2710+
)

0 commit comments

Comments
 (0)