Skip to content

Commit

Permalink
feat(insights): Add division_if for span metrics (#72995)
Browse files Browse the repository at this point in the history
Add division_if for span metrics so we can calculate rates such as slow
and frozen rates.

---------

Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
  • Loading branch information
philipphofmann and markushi authored Jul 17, 2024
1 parent b7284de commit a88b476
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/sentry/search/events/datasets/spans_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
),
default_result_type="integer",
),
fields.MetricsFunction(
"division_if",
required_args=[
fields.MetricArg(
# the dividend, needs to be named column, otherwise the query builder won't be able to determine the correct target table
"column",
allow_custom_measurements=False,
),
fields.MetricArg(
"divisorColumn",
allow_custom_measurements=False,
),
fields.MetricArg(
"if_col",
allowed_columns=["release"],
),
fields.SnQLStringArg(
"if_val", unquote=True, unescape_quotes=True, optional_unquote=True
),
],
snql_gauge=self._resolve_division_if,
snql_distribution=self._resolve_division_if,
default_result_type="percentage",
),
fields.MetricsFunction(
"sum",
optional_args=[
Expand Down Expand Up @@ -1228,6 +1252,48 @@ def _resolve_avg_if(self, args, alias):
alias,
)

def _resolve_sum_if(
self,
metric_name: str,
if_col_name: str,
if_val: SelectType,
alias: str | None = None,
) -> SelectType:
return Function(
"sumIf",
[
Column("value"),
Function(
"and",
[
Function(
"equals",
[
Column("metric_id"),
self.resolve_metric(metric_name),
],
),
Function(
"equals",
[self.builder.column(if_col_name), if_val],
),
],
),
],
alias,
)

def _resolve_division_if(
self,
args: Mapping[str, str | Column | SelectType],
alias: str,
) -> SelectType:
return function_aliases.resolve_division(
self._resolve_sum_if(args["column"], args["if_col"], args["if_val"]),
self._resolve_sum_if(args["divisorColumn"], args["if_col"], args["if_val"]),
alias,
)

def _resolve_avg_compare(self, args, alias):
return function_aliases.resolve_avg_compare(self.builder.column, args, alias)

Expand Down
75 changes: 75 additions & 0 deletions tests/snuba/api/endpoints/test_organization_events_span_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,81 @@ def test_count_if(self):
assert data[0][fieldRelease2] == 1
assert meta["dataset"] == "spansMetrics"

def test_division_if(self):
self.store_span_metric(
{
"min": 1,
"max": 1,
"sum": 1,
"count": 1,
"last": 1,
},
entity="metrics_gauges",
metric="mobile.slow_frames",
timestamp=self.three_days_ago,
tags={"release": "1.0.0"},
)
self.store_span_metric(
{
"min": 1,
"max": 1,
"sum": 15,
"count": 15,
"last": 1,
},
entity="metrics_gauges",
metric="mobile.total_frames",
timestamp=self.three_days_ago,
tags={"release": "1.0.0"},
)
self.store_span_metric(
{
"min": 1,
"max": 1,
"sum": 2,
"count": 2,
"last": 1,
},
entity="metrics_gauges",
metric="mobile.frozen_frames",
timestamp=self.three_days_ago,
tags={"release": "2.0.0"},
)
self.store_span_metric(
{
"min": 1,
"max": 1,
"sum": 10,
"count": 10,
"last": 1,
},
entity="metrics_gauges",
metric="mobile.total_frames",
timestamp=self.three_days_ago,
tags={"release": "2.0.0"},
)

fieldRelease1 = "division_if(mobile.slow_frames,mobile.total_frames,release,1.0.0)"
fieldRelease2 = "division_if(mobile.frozen_frames,mobile.total_frames,release,2.0.0)"

response = self.do_request(
{
"field": [fieldRelease1, fieldRelease2],
"query": "",
"project": self.project.id,
"dataset": "spansMetrics",
"statsPeriod": "7d",
}
)
assert response.status_code == 200, response.content
data = response.data["data"]
meta = response.data["meta"]
assert len(data) == 1
assert data[0][fieldRelease1] == 1 / 15
assert data[0][fieldRelease2] == 2 / 10

assert meta["dataset"] == "spansMetrics"

def test_count_unique(self):
self.store_span_metric(
1,
Expand Down

0 comments on commit a88b476

Please sign in to comment.