Skip to content

Commit ef2f329

Browse files
feat(discover): Allows insight metrics to be used with the avg function in the discover metrics dataset (#75870)
Allows insights metrics (eg: span.self_time) to be used in the metrics avg function
1 parent 537556d commit ef2f329

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def field_alias_converter(self) -> Mapping[str, Callable[[str], SelectType]]:
6565
}
6666

6767
def resolve_metric(self, value: str) -> int:
68-
metric_id = self.builder.resolve_metric_index(constants.METRICS_MAP.get(value, value))
68+
# SPAN_METRICS_MAP and METRICS_MAP have some overlapping keys
69+
mri_map = constants.SPAN_METRICS_MAP | constants.METRICS_MAP
70+
metric_id = self.builder.resolve_metric_index(mri_map.get(value, value))
6971
if metric_id is None:
7072
metric_id = self.builder.resolve_metric_index(
7173
constants.SPAN_METRICS_MAP.get(value, value)
@@ -116,7 +118,8 @@ def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
116118
required_args=[
117119
fields.MetricArg(
118120
"column",
119-
allowed_columns=constants.METRIC_DURATION_COLUMNS,
121+
allowed_columns=constants.SPAN_METRIC_DURATION_COLUMNS
122+
| constants.METRIC_DURATION_COLUMNS,
120123
)
121124
],
122125
calculated_args=[resolve_metric_id],

tests/sentry/snuba/test_entity_subscriptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,12 @@ def test_get_entity_subscription_for_insights_queries(self) -> None:
628628
("count()", "span.op:http.client", False),
629629
("count()", "span.description:abc", False),
630630
("performance_score(measurements.score.lcp)", "", False),
631+
("avg(span.self_time)", "", False),
631632
# TODO: The following functions are not supported in the discover metrics dataset yet.
632633
# Uncomment these as we port them over.
633634
# ("spm()", "", False),
634635
# ("cache_miss_rate()", "", False),
635636
# ("http_response_rate()", "", False),
636-
# ("avg(span.self_time)", "", False),
637637
]
638638
for aggregate, query, use_metrics_layer in cases:
639639
entity_subscription = get_entity_subscription(

tests/snuba/api/endpoints/test_organization_events_mep.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,6 +3458,48 @@ def test_filtering_by_org_id_is_not_compatible(self):
34583458
)
34593459
assert response.status_code == 400, response.content
34603460

3461+
def test_avg_span_self_time(self):
3462+
self.store_span_metric(
3463+
1,
3464+
timestamp=self.min_ago,
3465+
)
3466+
3467+
self.store_span_metric(
3468+
3,
3469+
timestamp=self.min_ago,
3470+
)
3471+
3472+
self.store_span_metric(
3473+
3,
3474+
timestamp=self.min_ago,
3475+
)
3476+
3477+
self.store_span_metric(
3478+
4,
3479+
timestamp=self.min_ago,
3480+
)
3481+
3482+
self.store_span_metric(
3483+
5,
3484+
timestamp=self.min_ago,
3485+
)
3486+
3487+
response = self.do_request(
3488+
{
3489+
"field": [
3490+
"avg(span.self_time)",
3491+
],
3492+
"query": "",
3493+
"project": self.project.id,
3494+
"dataset": "metrics",
3495+
}
3496+
)
3497+
3498+
assert response.status_code == 200, response.content
3499+
data = response.data["data"]
3500+
assert len(data) == 1
3501+
assert data[0]["avg(span.self_time)"] == 3.2
3502+
34613503

34623504
class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithOnDemandMetrics(
34633505
MetricsEnhancedPerformanceTestCase
@@ -3946,3 +3988,7 @@ def test_timestamp_groupby(self):
39463988
@pytest.mark.xfail(reason="Not implemented")
39473989
def test_on_demand_with_mep(self):
39483990
super().test_on_demand_with_mep()
3991+
3992+
@pytest.mark.xfail(reason="Not implemented")
3993+
def test_avg_span_self_time(self):
3994+
super().test_avg_span_self_time()

0 commit comments

Comments
 (0)