Repository navigation
feat(aci): Allow equations in span metric monitors behind a flag - #130742
Open
sentry-junior[bot] wants to merge 9 commits into
Open
sentry-junior[bot] wants to merge 9 commits into
sentry-junior[bot] wants to merge 9 commits into
Conversation
Span metric monitors rejected equation aggregates with `equation|... is not a valid function` because the validator ran the whole equation through translate_aggregate_field. With the organizations:spans-monitor-equations flag, span equations are now parsed against the span definitions, every term must be a function that span monitors already accept, and the equation must resolve. The subscription path already sends an equation to Snuba as one formula expression. Without the flag the request still fails, now with a clear message. Co-Authored-By: Matt Duncan <14761+mrduncan@users.noreply.github.com>
Span equations are still behind a feature flag, so the public API docs should not advertise them yet.
Span equations are still behind a feature flag, so the public API schema should not accept them yet.
The span equation check imported its allowlist from the API schema module, which only documents monitor aggregates. Read EAP_FUNCTIONS from incidents.logic instead, so validation no longer depends on a docs module.
Assert exact messages for equations without functions, mixed functions and fields, and unparseable equations, and check that logs equations are still rejected.
Match the trace metrics layout by moving the span equation checks to search/eap/spans/validator.py. The feature flag check stays in SnubaQueryValidator because it needs the organization from the validator context.
Bad number arguments such as apdex(span.duration,foo) raised ValueError, and opportunity_score raised because resolving it runs a query that needs an organization. Both surfaced as 500s. Catch ValueError and pass the organization into the resolver.
mrduncan
marked this pull request as ready for review
November 12, 2026 00:37
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 57ba0b5. Configure here.
…ject Resolving formulas such as opportunity_score runs a Snuba query. Pass the monitor's project instead of an empty project list so that query is scoped correctly.
saponifi3d
approved these changes
Nov 12, 2026
saponifi3d
left a comment
Contributor
There was a problem hiding this comment.
lgtm! might be some cleanup for the tests if you wanted (paramterizing them might make the diff a little smaller).
Fold the accepted and rejected span equation cases into subTest tables, as suggested in review. Same cases, less repetition. Co-Authored-By: Matt Duncan <14761+mrduncan@users.noreply.github.com>
Contributor
Author
|
@saponifi3d Thanks for the suggestion. I folded the span equation validator cases into subTest tables in 4b4b5f9 ( |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Creating a span metric monitor with an equation aggregate fails with a 400:
Spans Explore has supported equations since mid-2025, and Application Metrics monitors already accept them (#113105). For span monitors,
_validate_aggregatesends the wholeequation|…string totranslate_aggregate_field, which can only handle a single function. So ratio monitors such assum(gen_ai.usage.cache_read.input_tokens) / sum(gen_ai.usage.input_tokens)can't be created.With the new
organizations:spans-monitor-equationsflag (FlagPole,api_expose=True), span equations get their own validation path:SPAN_DEFINITIONS. It must contain aggregate functions and no bare fields, because the subscription only sends aggregate expressions to Snuba.EAP_MONITOR_FUNCTIONS["spans"]). For example,count_sample()andcount_if(...)are still rejected.SearchResolver, which catches bad argument types such assum(span.op).SnubaQuery.aggregate, the sameequation|<expr>format Application Metrics uses.Without the flag, span equations still fail, but with
Span monitors do not support equations.instead of the "not a valid function" message.Nothing downstream needed changing.
PerformanceSpansEAPRpcEntitySubscription.build_rpc_requestalready turns a span equation into one formula expression, with an extrapolation mode on each aggregation. That is the shape Snuba's subscription validation requires. A new entity subscription test checks this, and the detector create test confirms the Snuba subscription gets created.Out of scope:
get_aggregation_value_helperstill turns an empty result into0, so a "below X" ratio monitor fires on a window with no traffic. Ratio equations probably want to skip those updates; that's left as a follow-up.via @mrduncan.
--
View Session [Sentry]