Skip to content

Commit

Permalink
Port MRI parsing to SpansMetricsQueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
narsaynorath committed Dec 19, 2024
1 parent 7fdb3d8 commit fd1123c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
7 changes: 7 additions & 0 deletions src/sentry/search/events/builder/spans_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from sentry.search.events.datasets.spans_metrics import SpansMetricsDatasetConfig
from sentry.search.events.types import SelectType
from sentry.snuba.metrics.naming_layer.mri import parse_mri

SIZE_FIELDS = {
"http.decoded_response_content_length": "byte",
Expand Down Expand Up @@ -43,6 +44,12 @@ def get_field_type(self, field: str) -> str | None:
if unit := self.size_fields.get(field):
return unit

mri = constants.SPAN_METRICS_MAP.get(field)
if mri is not None:
parsed_mri = parse_mri(mri)
if parsed_mri is not None and parsed_mri.unit in constants.RESULT_TYPES:
return parsed_mri.unit

return None

def resolve_select(
Expand Down
29 changes: 2 additions & 27 deletions src/sentry/search/events/datasets/spans_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections.abc import Callable, Mapping
from datetime import datetime
from typing import Any, TypedDict
from typing import TypedDict

import sentry_sdk
from snuba_sdk import AliasedExpression, Column, Condition, Function, Identifier, Op, OrderBy
Expand All @@ -16,7 +16,7 @@
from sentry.search.events.fields import SnQLStringArg, get_function_alias
from sentry.search.events.types import SelectType, WhereType
from sentry.search.utils import DEVICE_CLASS
from sentry.snuba.metrics.naming_layer.mri import SpanMRI, parse_mri
from sentry.snuba.metrics.naming_layer.mri import SpanMRI
from sentry.snuba.referrer import Referrer


Expand Down Expand Up @@ -77,31 +77,6 @@ def resolve_metric(self, value: str) -> int:
self.builder.metric_ids.add(metric_id)
return metric_id

def reflective_result_type(
self, index: int = 0
) -> Callable[[list[fields.FunctionArg], dict[str, Any]], str]:
"""
Return the type of the metric, defaults to super implementation
"""

def result_type_fn(
function_arguments: list[fields.FunctionArg], parameter_values: dict[str, Any]
) -> str:
argument = function_arguments[index]
value = parameter_values[argument.name]
mri = constants.SPAN_METRICS_MAP.get(value)
if mri is not None:
parsed_mri = parse_mri(mri)
if parsed_mri is not None and parsed_mri.unit in constants.RESULT_TYPES:
return parsed_mri.unit

# if the mri is not found, then we fallback to the super implementation
return super(SpansMetricsDatasetConfig, self).reflective_result_type(index)(
function_arguments, parameter_values
)

return result_type_fn

@property
def function_converter(self) -> Mapping[str, fields.MetricsFunction]:
"""While the final functions in clickhouse must have their -Merge combinators in order to function, we don't
Expand Down

0 comments on commit fd1123c

Please sign in to comment.