Skip to content

Commit

Permalink
feat(starfish): HTTP error rates and counts for spans (#50238)
Browse files Browse the repository at this point in the history
Also renames it to http_error_rate.
  • Loading branch information
shruthilayaj committed Jun 2, 2023
1 parent dae651b commit 7df243e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/sentry/search/events/datasets/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,11 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
default_result_type="percentage",
),
fields.MetricsFunction(
"http_500_rate",
"http_error_rate",
snql_distribution=lambda args, alias: Function(
"divide",
[
self._resolve_http_500_count(args),
self._resolve_http_error_count(args),
Function(
"countIf",
[
Expand All @@ -685,6 +685,11 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
),
default_result_type="percentage",
),
fields.MetricsFunction(
"http_error_count",
snql_distribution=self._resolve_http_error_count,
default_result_type="integer",
),
]
}

Expand Down Expand Up @@ -1008,7 +1013,7 @@ def _resolve_failure_count(
alias,
)

def _resolve_http_500_count(
def _resolve_http_error_count(
self,
_: Mapping[str, Union[str, Column, SelectType, int, float]],
alias: Optional[str] = None,
Expand Down
77 changes: 77 additions & 0 deletions src/sentry/search/events/datasets/spans_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
),
default_result_type="duration",
),
fields.MetricsFunction(
"http_error_rate",
snql_distribution=lambda args, alias: Function(
"divide",
[
self._resolve_http_error_count(args),
Function(
"countIf",
[
Column("value"),
Function(
"equals",
[
Column("metric_id"),
self.resolve_metric("span.duration"),
],
),
],
),
],
alias,
),
default_result_type="percentage",
),
fields.MetricsFunction(
"http_error_count",
snql_distribution=self._resolve_http_error_count,
default_result_type="integer",
),
]
}

Expand All @@ -229,6 +258,28 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:

return function_converter

# Query Functions
def _resolve_count_if(
self,
metric_condition: Function,
condition: Function,
alias: Optional[str] = None,
) -> SelectType:
return Function(
"countIf",
[
Column("value"),
Function(
"and",
[
metric_condition,
condition,
],
),
],
alias,
)

def _resolve_total_span_duration(self, alias: str) -> SelectType:
"""This calculates the app's total time, so other filters that are
a part of the original query will not be applies. Only filter conditions
Expand Down Expand Up @@ -278,6 +329,32 @@ def _resolve_time_spent_percentage(
alias,
)

def _resolve_http_error_count(
self,
_: Mapping[str, Union[str, Column, SelectType, int, float]],
alias: Optional[str] = None,
) -> SelectType:
statuses = [
self.builder.resolve_tag_value(status) for status in constants.HTTP_SERVER_ERROR_STATUS
]
return self._resolve_count_if(
Function(
"equals",
[
Column("metric_id"),
self.resolve_metric("span.duration"),
],
),
Function(
"in",
[
self.builder.column("span.status_code"),
list(status for status in statuses if status is not None),
],
),
alias,
)

@property
def orderby_converter(self) -> Mapping[str, OrderBy]:
return {}
8 changes: 4 additions & 4 deletions tests/snuba/api/endpoints/test_organization_events_mep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ def test_os_name_falls_back(self):
meta = response.data["meta"]
assert not meta["isMetricsData"]

def test_http_500_rate(self):
def test_http_error_rate(self):
self.store_transaction_metric(
1,
tags={
Expand All @@ -2183,7 +2183,7 @@ def test_http_500_rate(self):
response = self.do_request(
{
"field": [
"http_500_rate()",
"http_error_rate()",
],
"dataset": "metrics",
}
Expand All @@ -2192,7 +2192,7 @@ def test_http_500_rate(self):
assert response.status_code == 200, response.content
data = response.data["data"]
assert len(data) == 1
assert data[0]["http_500_rate()"] == 0.5
assert data[0]["http_error_rate()"] == 0.5
meta = response.data["meta"]
assert meta["isMetricsData"]

Expand Down Expand Up @@ -2255,5 +2255,5 @@ def test_time_spent(self):
super().test_custom_measurement_size_filtering()

@pytest.mark.xfail(reason="Not supported")
def test_http_500_rate(self):
def test_http_error_rate(self):
super().test_having_condition()

0 comments on commit 7df243e

Please sign in to comment.