From 366845fd9cb03d13c622a972e85c29483ee89cc4 Mon Sep 17 00:00:00 2001 From: William Mak Date: Wed, 7 Oct 2026 11:19:17 -0400 Subject: [PATCH 1/4] fix(arithmetic): Improve arithmetic allowlists - Instead of hardcoded allowlists, use the dataset definitions to determine what is and isn't allowed in arithmetic --- .../organization_explore_formulas.py | 4 +- src/sentry/discover/arithmetic.py | 48 +++++++++++++++---- src/sentry/search/eap/columns.py | 2 + src/sentry/search/eap/common_aggregates.py | 1 + .../search/eap/occurrences/aggregates.py | 3 ++ src/sentry/search/eap/ourlogs/aggregates.py | 10 ++++ .../search/eap/preprod_size/aggregates.py | 2 + .../eap/processing_errors/aggregates.py | 1 + src/sentry/search/eap/resolver.py | 6 ++- src/sentry/search/eap/spans/aggregates.py | 19 ++++++++ src/sentry/search/eap/spans/formulas.py | 14 +++++- .../search/eap/trace_metrics/aggregates.py | 12 +++++ src/sentry/search/eap/trace_metrics/config.py | 3 +- .../search/eap/trace_metrics/formulas.py | 2 + .../search/eap/trace_metrics/validator.py | 4 +- src/sentry/search/eap/utils.py | 17 ++++--- tests/sentry/search/eap/test_utils.py | 20 +++++--- 17 files changed, 140 insertions(+), 28 deletions(-) diff --git a/src/sentry/api/endpoints/organization_explore_formulas.py b/src/sentry/api/endpoints/organization_explore_formulas.py index 0ff5d83ab5a2..cb81c6bbfe8e 100644 --- a/src/sentry/api/endpoints/organization_explore_formulas.py +++ b/src/sentry/api/endpoints/organization_explore_formulas.py @@ -251,12 +251,12 @@ def validate(self, data: FormulaData) -> FormulaData: parameters, calculations, [SimpleNamespace(**param) for param in data["references"]], - resolver.resolve_column, + resolver, ) except InvalidSearchQuery as e: raise ValidationError(str(e)) try: - parse_arithmetic(formula) + parse_arithmetic(formula, definitions=resolver.definitions) except (ArithmeticParseError, ArithmeticValidationError) as e: raise ValidationError(str(e)) diff --git a/src/sentry/discover/arithmetic.py b/src/sentry/discover/arithmetic.py index abda6cfc3ceb..1f3285ffb803 100644 --- a/src/sentry/discover/arithmetic.py +++ b/src/sentry/discover/arithmetic.py @@ -2,14 +2,20 @@ import re from dataclasses import dataclass -from typing import Literal, Union, overload +from typing import Iterable, Literal, Union, overload from parsimonious.exceptions import ParseError from parsimonious.grammar import Grammar from parsimonious.nodes import NodeVisitor from sentry.exceptions import InvalidSearchQuery -from sentry.search.events.constants import TOTAL_COUNT_ALIAS, TOTAL_TRANSACTION_DURATION_ALIAS +from sentry.search.eap.columns import ColumnDefinitions +from sentry.search.events.constants import ( + DURATION_UNITS, + SIZE_UNITS, + TOTAL_COUNT_ALIAS, + TOTAL_TRANSACTION_DURATION_ALIAS, +) # prefix on fields so we know they're equations EQUATION_PREFIX = "equation|" @@ -148,7 +154,7 @@ class ArithmeticVisitor(NodeVisitor): # Don't wrap in VisitationErrors unwrapped_exceptions = (ArithmeticError,) - field_allowlist = { + field_allowlist: Iterable[str] = { "transaction.duration", "spans.http", "spans.db", @@ -176,7 +182,7 @@ class ArithmeticVisitor(NodeVisitor): TOTAL_COUNT_ALIAS, TOTAL_TRANSACTION_DURATION_ALIAS, } - function_allowlist = { + function_allowlist: Iterable[str] = { "count", "count_if", "count_unique", @@ -222,11 +228,36 @@ class ArithmeticVisitor(NodeVisitor): "trace_status_rate", } - def __init__(self, max_operators: int | None): + def __init__(self, max_operators: int | None, definitions: ColumnDefinitions | None): super().__init__() self.operators: int = 0 self.terms: int = 0 self.max_operators = max_operators if max_operators else self.DEFAULT_MAX_OPERATORS + self.definitions = definitions + if definitions: + self.field_allowlist = [ + name + for name, column in definitions.columns.items() + if column.search_type + in [ + "number", + "integer", + "duration", + "rate", + "currency", + "percentage", + *SIZE_UNITS.keys(), + *DURATION_UNITS.keys(), + ] + ] + self.function_allowlist = [ + name + for name, function in definitions.aggregates.items() + if function.valid_arithmetic + ] + self.function_allowlist.extend( + [name for name, formula in definitions.formulas.items() if formula.valid_arithmetic] + ) self.fields: set[str] = set() self.functions: set[str] = set() @@ -323,20 +354,21 @@ def parse_arithmetic( equation: str, max_operators: int | None = None, *, + definitions: ColumnDefinitions | None = None, validate_single_operator: Literal[True], ) -> tuple[Operation, list[str], list[str]]: ... @overload def parse_arithmetic( - equation: str, - max_operators: int | None = None, + equation: str, max_operators: int | None = None, definitions: ColumnDefinitions | None = None ) -> tuple[Operation | float | str, list[str], list[str]]: ... def parse_arithmetic( equation: str, max_operators: int | None = None, + definitions: ColumnDefinitions | None = None, validate_single_operator: bool = False, ) -> tuple[Operation | float | str, list[str], list[str]]: """Given a string equation try to parse it into a set of Operations""" @@ -346,7 +378,7 @@ def parse_arithmetic( raise ArithmeticParseError( "Unable to parse your equation, make sure it is well formed arithmetic" ) - visitor = ArithmeticVisitor(max_operators) + visitor = ArithmeticVisitor(max_operators, definitions) result = visitor.visit(tree) # total count is the exception to the no mixing rule if ( diff --git a/src/sentry/search/eap/columns.py b/src/sentry/search/eap/columns.py index c2eeed2d54f5..bb09b2850602 100644 --- a/src/sentry/search/eap/columns.py +++ b/src/sentry/search/eap/columns.py @@ -478,6 +478,8 @@ class FunctionDefinition: processor: Callable[[Any], Any] | None = None # if a function is private, assume it can't be used unless it's provided in `SearchResolverConfig.functions_acl` private: bool = False + # Usable in arithmetic + valid_arithmetic: bool = False @property def required_arguments( diff --git a/src/sentry/search/eap/common_aggregates.py b/src/sentry/search/eap/common_aggregates.py index 82de861cbb52..3d0ff6c7ebcb 100644 --- a/src/sentry/search/eap/common_aggregates.py +++ b/src/sentry/search/eap/common_aggregates.py @@ -34,4 +34,5 @@ def count_unique_aggregate_definition( default_arg=default_arg, ) ], + valid_arithmetic=True, ) diff --git a/src/sentry/search/eap/occurrences/aggregates.py b/src/sentry/search/eap/occurrences/aggregates.py index f75ae1bced4f..6d24bb2c1134 100644 --- a/src/sentry/search/eap/occurrences/aggregates.py +++ b/src/sentry/search/eap/occurrences/aggregates.py @@ -46,6 +46,7 @@ attribute_types=COMMON_COUNTABLE_ATTRIBUTE_TYPES, ) ], + valid_arithmetic=True, ), "count": AggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -59,6 +60,7 @@ ) ], attribute_resolver=count_argument_resolver_optimized(OCCURRENCES_ALWAYS_PRESENT_ATTRIBUTES), + valid_arithmetic=True, ), "count_if": ConditionalAggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -99,6 +101,7 @@ ), # Second value is only for between, so it must be a number ], aggregate_resolver=resolve_key_eq_value_filter, + valid_arithmetic=True, ), "min": AggregateDefinition( internal_function=Function.FUNCTION_MIN, diff --git a/src/sentry/search/eap/ourlogs/aggregates.py b/src/sentry/search/eap/ourlogs/aggregates.py index d0a9228c5ae3..1e5a87a9385a 100644 --- a/src/sentry/search/eap/ourlogs/aggregates.py +++ b/src/sentry/search/eap/ourlogs/aggregates.py @@ -30,6 +30,7 @@ ) ], attribute_resolver=count_argument_resolver_optimized(LOGS_ALWAYS_PRESENT_ATTRIBUTES), + valid_arithmetic=True, ), "count_unique": count_unique_aggregate_definition(), "sum": AggregateDefinition( @@ -47,6 +48,7 @@ }, ) ], + valid_arithmetic=True, ), "avg": AggregateDefinition( internal_function=Function.FUNCTION_AVG, @@ -64,6 +66,7 @@ }, ) ], + valid_arithmetic=True, ), "p50": AggregateDefinition( internal_function=Function.FUNCTION_P50, @@ -81,6 +84,7 @@ }, ) ], + valid_arithmetic=True, ), "p75": AggregateDefinition( internal_function=Function.FUNCTION_P75, @@ -98,6 +102,7 @@ }, ) ], + valid_arithmetic=True, ), "p90": AggregateDefinition( internal_function=Function.FUNCTION_P90, @@ -115,6 +120,7 @@ }, ) ], + valid_arithmetic=True, ), "p95": AggregateDefinition( internal_function=Function.FUNCTION_P95, @@ -132,6 +138,7 @@ }, ) ], + valid_arithmetic=True, ), "p99": AggregateDefinition( internal_function=Function.FUNCTION_P99, @@ -149,6 +156,7 @@ }, ) ], + valid_arithmetic=True, ), "max": AggregateDefinition( internal_function=Function.FUNCTION_MAX, @@ -166,6 +174,7 @@ }, ) ], + valid_arithmetic=True, ), "min": AggregateDefinition( internal_function=Function.FUNCTION_MIN, @@ -183,5 +192,6 @@ }, ) ], + valid_arithmetic=True, ), } diff --git a/src/sentry/search/eap/preprod_size/aggregates.py b/src/sentry/search/eap/preprod_size/aggregates.py index 3780603ef4be..354abb8771f3 100644 --- a/src/sentry/search/eap/preprod_size/aggregates.py +++ b/src/sentry/search/eap/preprod_size/aggregates.py @@ -16,6 +16,7 @@ }, ) ], + valid_arithmetic=True, ), "min": AggregateDefinition( internal_function=Function.FUNCTION_MIN, @@ -29,5 +30,6 @@ }, ) ], + valid_arithmetic=True, ), } diff --git a/src/sentry/search/eap/processing_errors/aggregates.py b/src/sentry/search/eap/processing_errors/aggregates.py index 2d74b61dff9c..b9137ba521e0 100644 --- a/src/sentry/search/eap/processing_errors/aggregates.py +++ b/src/sentry/search/eap/processing_errors/aggregates.py @@ -34,6 +34,7 @@ attribute_resolver=count_argument_resolver_optimized( PROCESSING_ERRORS_ALWAYS_PRESENT_ATTRIBUTES ), + valid_arithmetic=True, ), "count_unique": count_unique_aggregate_definition(default_arg="event_id"), } diff --git a/src/sentry/search/eap/resolver.py b/src/sentry/search/eap/resolver.py index 07bc6e4a3887..b0ee2bb84974 100644 --- a/src/sentry/search/eap/resolver.py +++ b/src/sentry/search/eap/resolver.py @@ -1490,7 +1490,9 @@ def resolve_equation( """Resolve an equation creating a ResolvedEquation object, we don't just return a Column.BinaryFormula since it'll help callers with extra information, like the existence of aggregates and the search type """ - operation, fields, functions = arithmetic.parse_arithmetic(equation) + operation, fields, functions = arithmetic.parse_arithmetic( + equation, definitions=self.definitions + ) # Handle the case where the equation is just a single term if isinstance(operation, str): # Resolve the column, and turn it into a RPC Column so it can be used in a BinaryFormula @@ -1635,5 +1637,5 @@ def resolve_formula( arguments = fields.parse_arguments(formula.name, columns) - equation = resolve_and_parse_formula(formula, arguments, self.resolve_column) + equation = resolve_and_parse_formula(formula, arguments, self) return self.resolve_equation(equation, alias) diff --git a/src/sentry/search/eap/spans/aggregates.py b/src/sentry/search/eap/spans/aggregates.py index c454bfe4b7d5..0b3082074813 100644 --- a/src/sentry/search/eap/spans/aggregates.py +++ b/src/sentry/search/eap/spans/aggregates.py @@ -138,6 +138,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_search_type="integer", arguments=[ValueArgumentDefinition(argument_types={"string"})], aggregate_resolver=resolve_count_op, + valid_arithmetic=True, ), "count_scores": ConditionalAggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -156,6 +157,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], aggregate_resolver=resolve_count_scores, + valid_arithmetic=True, ), "count_starts": ConditionalAggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -169,6 +171,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], aggregate_resolver=resolve_count_starts, + valid_arithmetic=True, ), "http_response_count": ConditionalAggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -180,6 +183,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], aggregate_resolver=resolve_http_response_count, + valid_arithmetic=True, ), "bounded_sample": ConditionalAggregateDefinition( # Bounded sample will return True if the sample is between the lower bound (2nd parameter) and if provided, greater the upper bound (3rd parameter). @@ -214,6 +218,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "avg": AggregateDefinition( internal_function=Function.FUNCTION_AVG, @@ -232,6 +237,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "avg_sample": AggregateDefinition( internal_function=Function.FUNCTION_AVG, @@ -251,6 +257,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], extrapolation_mode_override=ExtrapolationMode.EXTRAPOLATION_MODE_NONE, + valid_arithmetic=True, ), "count": AggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -272,6 +279,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], attribute_resolver=count_argument_resolver_optimized(SPANS_ALWAYS_PRESENT_ATTRIBUTES), + valid_arithmetic=True, ), "count_sample": AggregateDefinition( internal_function=Function.FUNCTION_COUNT, @@ -292,6 +300,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], extrapolation_mode_override=ExtrapolationMode.EXTRAPOLATION_MODE_NONE, + valid_arithmetic=True, ), "p50": AggregateDefinition( internal_function=Function.FUNCTION_P50, @@ -310,6 +319,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "p50_sample": AggregateDefinition( internal_function=Function.FUNCTION_P50, @@ -328,6 +338,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace ) ], extrapolation_mode_override=ExtrapolationMode.EXTRAPOLATION_MODE_NONE, + valid_arithmetic=True, ), "p75": AggregateDefinition( internal_function=Function.FUNCTION_P75, @@ -346,6 +357,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "p90": AggregateDefinition( internal_function=Function.FUNCTION_P90, @@ -364,6 +376,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "p95": AggregateDefinition( internal_function=Function.FUNCTION_P95, @@ -382,6 +395,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "p99": AggregateDefinition( internal_function=Function.FUNCTION_P99, @@ -400,6 +414,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "p100": AggregateDefinition( internal_function=Function.FUNCTION_MAX, @@ -418,6 +433,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "max": AggregateDefinition( internal_function=Function.FUNCTION_MAX, @@ -439,6 +455,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "min": AggregateDefinition( internal_function=Function.FUNCTION_MIN, @@ -460,6 +477,7 @@ def resolve_bounded_sample(args: ResolvedArguments) -> tuple[AttributeKey, Trace default_arg="span.duration", ) ], + valid_arithmetic=True, ), "any": AggregateDefinition( internal_function=Function.FUNCTION_ANY, @@ -755,6 +773,7 @@ def if_combinator(definition: AggregateDefinition) -> AggregateDefinition: ValueArgumentDefinition(argument_types={"query"}, validator=if_query_validator), *definition.arguments, ], + valid_arithmetic=definition.valid_arithmetic, ) diff --git a/src/sentry/search/eap/spans/formulas.py b/src/sentry/search/eap/spans/formulas.py index 4d275f953b15..32a79373b212 100644 --- a/src/sentry/search/eap/spans/formulas.py +++ b/src/sentry/search/eap/spans/formulas.py @@ -1102,6 +1102,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B arguments=[], formula_resolver=failure_rate, is_aggregate=True, + valid_arithmetic=True, ), "failure_rate_if": FormulaDefinition( default_search_type="percentage", @@ -1142,6 +1143,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=opportunity_score, is_aggregate=True, + valid_arithmetic=True, ), "performance_score": FormulaDefinition( default_search_type="percentage", @@ -1157,6 +1159,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B formula_resolver=performance_score, processor=none_if_zero_processor, is_aggregate=True, + valid_arithmetic=True, ), "avg_compare": FormulaDefinition( default_search_type="percentage", @@ -1274,15 +1277,21 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B arguments=[], formula_resolver=make_epm(_SPAN_COUNT_KEY), is_aggregate=True, + valid_arithmetic=True, ), "tpm": FormulaDefinition( - default_search_type="rate", arguments=[], formula_resolver=tpm, is_aggregate=True + default_search_type="rate", + arguments=[], + formula_resolver=tpm, + is_aggregate=True, + valid_arithmetic=True, ), "failure_count": FormulaDefinition( default_search_type="integer", arguments=[], formula_resolver=failure_count, is_aggregate=True, + valid_arithmetic=True, ), "failure_count_if": FormulaDefinition( default_search_type="integer", @@ -1303,6 +1312,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B arguments=[], formula_resolver=make_eps(_SPAN_COUNT_KEY), is_aggregate=True, + valid_arithmetic=True, ), "apdex": FormulaDefinition( default_search_type="number", @@ -1318,6 +1328,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=apdex, is_aggregate=True, + valid_arithmetic=True, ), "user_misery": FormulaDefinition( default_search_type="number", @@ -1333,5 +1344,6 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=user_misery, is_aggregate=True, + valid_arithmetic=True, ), } diff --git a/src/sentry/search/eap/trace_metrics/aggregates.py b/src/sentry/search/eap/trace_metrics/aggregates.py index a0c29bb728d9..f201b38a6753 100644 --- a/src/sentry/search/eap/trace_metrics/aggregates.py +++ b/src/sentry/search/eap/trace_metrics/aggregates.py @@ -52,6 +52,7 @@ attribute_resolver=count_argument_resolver_optimized( TRACE_METRICS_ALWAYS_PRESENT_ATTRIBUTES ), + valid_arithmetic=True, ), "count_unique": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_UNIQ, @@ -86,6 +87,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "sum": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_SUM, @@ -116,6 +118,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "avg": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_AVG, @@ -147,6 +150,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "p50": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_P50, @@ -178,6 +182,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "p75": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_P75, @@ -209,6 +214,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "p90": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_P90, @@ -240,6 +246,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "p95": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_P95, @@ -271,6 +278,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "p99": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_P99, @@ -302,6 +310,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "max": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_MAX, @@ -333,6 +342,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), "min": TraceMetricAggregateDefinition( internal_function=Function.FUNCTION_MIN, @@ -364,6 +374,7 @@ ), ValueArgumentDefinition(argument_types={"string"}, default_arg=""), ], + valid_arithmetic=True, ), } @@ -383,6 +394,7 @@ def if_combinator(definition: AggregateDefinition) -> AggregateDefinition: ValueArgumentDefinition(argument_types={"query"}, validator=if_query_validator), *definition.arguments, ], + valid_arithmetic=definition.valid_arithmetic, ) diff --git a/src/sentry/search/eap/trace_metrics/config.py b/src/sentry/search/eap/trace_metrics/config.py index dcb39fb65cdd..8040a7c5fa71 100644 --- a/src/sentry/search/eap/trace_metrics/config.py +++ b/src/sentry/search/eap/trace_metrics/config.py @@ -46,6 +46,7 @@ def _extra_conditions_from_columns( aggregate_all_metrics = False selected_metrics: set[TraceMetric] = set() columns: set[str] = set() + from sentry.search.eap.trace_metrics.definitions import TRACE_METRICS_DEFINITIONS if selected_columns: stripped_columns = [column.strip() for column in selected_columns] @@ -56,7 +57,7 @@ def _extra_conditions_from_columns( if equations: for equation in equations: - _, _, terms = parse_arithmetic(equation) + _, _, terms = parse_arithmetic(equation, definitions=TRACE_METRICS_DEFINITIONS) for term in terms: columns.add(term) diff --git a/src/sentry/search/eap/trace_metrics/formulas.py b/src/sentry/search/eap/trace_metrics/formulas.py index fe844f497d73..2d1c0d604674 100644 --- a/src/sentry/search/eap/trace_metrics/formulas.py +++ b/src/sentry/search/eap/trace_metrics/formulas.py @@ -145,6 +145,7 @@ def per_minute(args: ResolvedArguments, settings: ResolverSettings) -> Column.Bi formula_resolver=per_second, is_aggregate=True, infer_search_type_from_arguments=False, + valid_arithmetic=True, ), "per_minute": TraceMetricFormulaDefinition( default_search_type="rate", @@ -174,6 +175,7 @@ def per_minute(args: ResolvedArguments, settings: ResolverSettings) -> Column.Bi formula_resolver=per_minute, is_aggregate=True, infer_search_type_from_arguments=False, + valid_arithmetic=True, ), } diff --git a/src/sentry/search/eap/trace_metrics/validator.py b/src/sentry/search/eap/trace_metrics/validator.py index 1445c00734e5..2633eacf0866 100644 --- a/src/sentry/search/eap/trace_metrics/validator.py +++ b/src/sentry/search/eap/trace_metrics/validator.py @@ -60,7 +60,9 @@ def validate_trace_metrics_aggregate(aggregate: str) -> None: serializers.ValidationError: If the aggregate is invalid """ if is_equation(aggregate): - _, _, terms = parse_arithmetic(strip_equation(aggregate)) + _, _, terms = parse_arithmetic( + strip_equation(aggregate), definitions=TRACE_METRICS_DEFINITIONS + ) else: terms = [aggregate] diff --git a/src/sentry/search/eap/utils.py b/src/sentry/search/eap/utils.py index e0a36fec5e60..f1814111b39e 100644 --- a/src/sentry/search/eap/utils.py +++ b/src/sentry/search/eap/utils.py @@ -2,7 +2,7 @@ import re from collections.abc import Collection, Iterable, Mapping from datetime import datetime -from typing import Callable, Literal, Protocol +from typing import Literal, Protocol from google.protobuf.timestamp_pb2 import Timestamp from sentry_conventions.attributes import ATTRIBUTE_METADATA as ATTRIBUTE_METADATA @@ -57,6 +57,7 @@ PROFILE_FUNCTIONS_REPLACEMENT_MAP, ) from sentry.search.eap.profile_functions.definitions import PROFILE_FUNCTIONS_DEFINITIONS +from sentry.search.eap.resolver import SearchResolver from sentry.search.eap.spans.attributes import ( SPAN_ATTRIBUTE_DEFINITIONS, SPAN_INTERNAL_TO_SECONDARY_ALIASES_MAPPING, @@ -532,7 +533,7 @@ class FormulaTerm(Protocol): def resolve_and_parse_formula( saved_formula: ExploreSavedFormula, arguments: list[str], - resolve_column: Callable[[str], object], + resolver: SearchResolver, ) -> str: """Given a formula, parse its parameters and create its rpc definition""" saved_variables = sorted( @@ -552,7 +553,7 @@ def resolve_and_parse_formula( saved_args, saved_calculations, saved_references, - resolve_column, + resolver, ) @@ -562,7 +563,7 @@ def parse_formula( saved_args: Iterable[FormulaParam], saved_calculations: Iterable[FormulaTerm], saved_references: Iterable[FormulaTerm], - resolve_column: Callable[[str], object], + resolver: SearchResolver, ) -> str: # Create a dict of param name -> the arg the user passed variables = {} @@ -586,7 +587,7 @@ def parse_formula( ) elif saved_arg.param_type == ParamItemTypes.COLUMN: try: - resolve_column(arg) + resolver.resolve_column(arg) except InvalidSearchQuery: raise InvalidSearchQuery( f"{saved_arg.name} expected a valid column but got '{arg}'" @@ -604,7 +605,7 @@ def parse_formula( f"Missing parameters for {calculation.name}; {', '.join(unmatched)}" ) try: - parsed, _, _ = parse_arithmetic(value) + parsed, _, _ = parse_arithmetic(value, definitions=resolver.definitions) except ArithmeticError as e: raise InvalidSearchQuery(e) calculations[calculation.name] = resolve_arithmetic(parsed) @@ -629,5 +630,9 @@ def parse_formula( final_equation = final_equation.replace(f"{{{param_name}}}", str(user_arg)) if unmatched := re.findall(FORMAT_RE, final_equation): raise InvalidSearchQuery(f"Missing parameters for formula; {', '.join(unmatched)}") + try: + _, _, _ = parse_arithmetic(final_equation, definitions=resolver.definitions) + except ArithmeticError as e: + raise InvalidSearchQuery(e) return f"{final_equation}" diff --git a/tests/sentry/search/eap/test_utils.py b/tests/sentry/search/eap/test_utils.py index 9b23b9bc8295..34dee9d53f91 100644 --- a/tests/sentry/search/eap/test_utils.py +++ b/tests/sentry/search/eap/test_utils.py @@ -11,12 +11,15 @@ from sentry.exceptions import InvalidSearchQuery from sentry.search.eap import utils from sentry.search.eap.constants import SearchType +from sentry.search.eap.types import SearchResolverConfig from sentry.search.eap.utils import ( attribute_name_exists, check_attribute_names_exist, resolve_and_parse_formula, serialize_search_type, ) +from sentry.search.events.types import SnubaParams +from sentry.snuba.spans_rpc import Spans from sentry.testutils.cases import TestCase @@ -100,39 +103,42 @@ def setUp(self) -> None: self.org = self.create_organization(owner=self.user) self.project = self.create_project(organization=self.org) self.formula = self.create_explore_saved_formula(organization=self.org) + self.resolver = Spans.get_resolver(SnubaParams(), SearchResolverConfig()) def test_parse_formula_wrong_args(self) -> None: with pytest.raises(InvalidSearchQuery, match="formula.apdex expected 2 arguments got 5"): resolve_and_parse_formula( - self.formula, ["300", "300", "300", "300", "300"], lambda x: x + self.formula, ["300", "300", "300", "300", "300"], self.resolver ) def test_parse_formula_wrong_arg_type(self) -> None: with pytest.raises( InvalidSearchQuery, match="threshold expected a number but got 'hello_world' instead" ): - resolve_and_parse_formula(self.formula, ["span.duration", "hello_world"], lambda x: x) + resolve_and_parse_formula(self.formula, ["span.duration", "hello_world"], self.resolver) def test_parse_formula_values_too_big(self) -> None: with pytest.raises(InvalidSearchQuery, match="which is outside the supported number range"): - resolve_and_parse_formula(self.formula, ["span.duration", "inf"], lambda x: x) + resolve_and_parse_formula(self.formula, ["span.duration", "inf"], self.resolver) with pytest.raises(InvalidSearchQuery, match="which is outside the supported number range"): resolve_and_parse_formula( - self.formula, ["span.duration", "100000000000000000000"], lambda x: x + self.formula, ["span.duration", "100000000000000000000"], self.resolver ) with pytest.raises(InvalidSearchQuery, match="which is outside the supported number range"): resolve_and_parse_formula( - self.formula, ["span.duration", "0.000000000000000000001"], lambda x: x + self.formula, ["span.duration", "0.000000000000000000001"], self.resolver ) def test_parse_formula_simple(self) -> None: - equation = resolve_and_parse_formula(self.formula, ["span.duration", "300"], lambda x: x) + equation = resolve_and_parse_formula(self.formula, ["span.duration", "300"], self.resolver) assert ( equation == "(count_if(`span.duration:<300.0`) + count_if(`span.duration:>=300.0 and span.duration:<=1200.0`) / 2) / count()" ) - equation = resolve_and_parse_formula(self.formula, ["measurements.lcp", "400"], lambda x: x) + equation = resolve_and_parse_formula( + self.formula, ["measurements.lcp", "400"], self.resolver + ) assert ( equation == "(count_if(`measurements.lcp:<400.0`) + count_if(`measurements.lcp:>=400.0 and measurements.lcp:<=1600.0`) / 2) / count()" From 2c176c049be9521dfb862097d3b3f827667061ca Mon Sep 17 00:00:00 2001 From: William Mak Date: Thu, 8 Oct 2026 15:17:52 -0400 Subject: [PATCH 2/4] fix: Missing valids on a few functions and add tests --- src/sentry/search/eap/spans/formulas.py | 6 ++++++ tests/sentry/discover/test_arithmetic.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/sentry/search/eap/spans/formulas.py b/src/sentry/search/eap/spans/formulas.py index 32a79373b212..14a2c7f2438c 100644 --- a/src/sentry/search/eap/spans/formulas.py +++ b/src/sentry/search/eap/spans/formulas.py @@ -1096,6 +1096,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ) ], formula_resolver=trace_status_rate, + valid_arithmetic=True, ), "failure_rate": FormulaDefinition( default_search_type="percentage", @@ -1117,18 +1118,21 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=failure_rate_if, is_aggregate=True, + valid_arithmetic=True, ), "ttfd_contribution_rate": FormulaDefinition( default_search_type="percentage", arguments=[], formula_resolver=ttfd_contribution_rate, is_aggregate=True, + valid_arithmetic=True, ), "ttid_contribution_rate": FormulaDefinition( default_search_type="percentage", arguments=[], formula_resolver=ttid_contribution_rate, is_aggregate=True, + valid_arithmetic=True, ), "opportunity_score": FormulaDefinition( default_search_type="percentage", @@ -1212,6 +1216,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=division_if, is_aggregate=True, + valid_arithmetic=True, ), "division": FormulaDefinition( default_search_type="percentage", @@ -1306,6 +1311,7 @@ def user_misery(args: ResolvedArguments, settings: ResolverSettings) -> Column.B ], formula_resolver=failure_count_if, is_aggregate=True, + valid_arithmetic=True, ), "eps": FormulaDefinition( default_search_type="rate", diff --git a/tests/sentry/discover/test_arithmetic.py b/tests/sentry/discover/test_arithmetic.py index 5c99aca3ebdb..4545b4cf04b7 100644 --- a/tests/sentry/discover/test_arithmetic.py +++ b/tests/sentry/discover/test_arithmetic.py @@ -8,6 +8,7 @@ parse_arithmetic, resolve_arithmetic, ) +from sentry.search.eap.spans.definitions import SPAN_DEFINITIONS def parse_arithmetic_must_be_operation(s: str) -> Operation: @@ -347,3 +348,21 @@ def test_unparseable_arithmetic(equation) -> None: def test_invalid_arithmetic(equation) -> None: with pytest.raises(ArithmeticValidationError): parse_arithmetic(equation, validate_single_operator=True) + + +@pytest.mark.parametrize( + "equation", + [ + # Previously on the hardcoded allowlist; must stay allowed via SPAN_DEFINITIONS. + "tpm() + failure_rate_if(is_transaction,equals,true)", + "failure_count_if(is_transaction,equals,true) + 0", + "ttid_contribution_rate() + ttfd_contribution_rate()", + "100 * ttid_contribution_rate()", + "division_if(span.self_time,span.duration,is_transaction,equals,true) + 0", + "trace_status_rate(ok) + 0", + ], +) +def test_span_definition_allowlist_keeps_prebuilt_dashboard_formulas(equation) -> None: + result, _, functions = parse_arithmetic(equation, definitions=SPAN_DEFINITIONS) + assert isinstance(result, Operation) + assert len(functions) > 0 From e68222f906cabd67e1cfbd87718d255b9443f8de Mon Sep 17 00:00:00 2001 From: William Mak Date: Fri, 9 Oct 2026 10:29:06 -0400 Subject: [PATCH 3/4] fix: log combinators was missing valid_arithmetic --- src/sentry/search/eap/ourlogs/aggregates.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/search/eap/ourlogs/aggregates.py b/src/sentry/search/eap/ourlogs/aggregates.py index 5256df2d3562..6676a69f7dc4 100644 --- a/src/sentry/search/eap/ourlogs/aggregates.py +++ b/src/sentry/search/eap/ourlogs/aggregates.py @@ -214,6 +214,7 @@ def if_combinator(definition: AggregateDefinition) -> AggregateDefinition: ValueArgumentDefinition(argument_types={"query"}, validator=if_query_validator), *definition.arguments, ], + valid_arithmetic=definition.valid_arithmetic, ) From e260f63c1377b6fa9fc2569ea4b0aa2f6a50a908 Mon Sep 17 00:00:00 2001 From: William Mak Date: Fri, 9 Oct 2026 10:48:36 -0400 Subject: [PATCH 4/4] fix: more missing spots --- src/sentry/search/eap/occurrences/aggregates.py | 1 + src/sentry/search/eap/replays/aggregates.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/sentry/search/eap/occurrences/aggregates.py b/src/sentry/search/eap/occurrences/aggregates.py index 6d24bb2c1134..6243d2372bdc 100644 --- a/src/sentry/search/eap/occurrences/aggregates.py +++ b/src/sentry/search/eap/occurrences/aggregates.py @@ -114,6 +114,7 @@ }, ) ], + valid_arithmetic=True, ), "count_unique": count_unique_aggregate_definition(default_arg="group_id"), "first_seen": AggregateDefinition( diff --git a/src/sentry/search/eap/replays/aggregates.py b/src/sentry/search/eap/replays/aggregates.py index e6bdc2a09ad8..6e9a7e57456e 100644 --- a/src/sentry/search/eap/replays/aggregates.py +++ b/src/sentry/search/eap/replays/aggregates.py @@ -29,6 +29,7 @@ ) ], attribute_resolver=count_argument_resolver_optimized(REPLAYS_ALWAYS_PRESENT_ATTRIBUTES), + valid_arithmetic=True, ), "sum": AggregateDefinition( internal_function=Function.FUNCTION_SUM, @@ -45,6 +46,7 @@ }, ) ], + valid_arithmetic=True, ), "min": AggregateDefinition( internal_function=Function.FUNCTION_MIN, @@ -63,6 +65,7 @@ }, ) ], + valid_arithmetic=True, ), "max": AggregateDefinition( internal_function=Function.FUNCTION_MAX, @@ -81,5 +84,6 @@ }, ) ], + valid_arithmetic=True, ), }