Repository navigation
feat(aci): Allow equations in span metric monitors behind a flag #130742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sentry-junior
wants to merge
9
commits into
master
Choose a base branch
from
feat/aci/span-monitor-equations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ef801bc
feat(aci): Allow equations in span metric monitors behind a flag
sentry-junior[bot] 6bd4542
docs(aci): Drop equation mention from spans data source schema
mrduncan c4513ab
docs(aci): Keep equations out of the spans aggregate schema pattern
mrduncan 10eb32f
ref(aci): Use EAP_FUNCTIONS for the span equation allowlist
mrduncan 405f5e0
test(aci): Cover more span equation validation errors
mrduncan 650ed5e
ref(aci): Move span equation validation into the spans package
mrduncan 57ba0b5
fix(aci): Return validation errors for bad span equation arguments
sentry-junior[bot] 3700fbf
fix(aci): Scope span equation validation queries to the monitor's pro…
sentry-junior[bot] 4b4b5f9
test(aci): Table-drive the span equation validator tests
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from django.utils import timezone | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from rest_framework import serializers | ||
|
|
||
| from sentry.discover.arithmetic import ArithmeticError, parse_arithmetic, strip_equation | ||
| from sentry.exceptions import InvalidSearchQuery | ||
| from sentry.incidents.logic import EAP_FUNCTIONS | ||
| from sentry.models.organization import Organization | ||
| from sentry.models.project import Project | ||
| from sentry.search.eap.resolver import SearchResolver | ||
| from sentry.search.eap.spans.definitions import SPAN_DEFINITIONS | ||
| from sentry.search.eap.types import SearchResolverConfig | ||
| from sentry.search.events.fields import is_function | ||
| from sentry.search.events.types import SnubaParams | ||
|
|
||
|
|
||
| def validate_spans_equation_aggregate( | ||
| aggregate: str, organization: Organization, projects: list[Project] | ||
| ) -> None: | ||
| equation = strip_equation(aggregate) | ||
| try: | ||
| _operation, fields, functions = parse_arithmetic(equation, definitions=SPAN_DEFINITIONS) | ||
| except ArithmeticError as e: | ||
| raise serializers.ValidationError({"aggregate": _(f"Invalid Metric: {e}")}) | ||
|
|
||
| # The subscription only sends aggregate expressions to Snuba, so an equation without | ||
| # functions would produce an empty request. | ||
| if fields or not functions: | ||
| raise serializers.ValidationError( | ||
| { | ||
| "aggregate": _( | ||
| "Invalid Metric: An equation must combine aggregate functions, such as " | ||
| "sum(a) / sum(b)." | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| for function in functions: | ||
| match = is_function(function) | ||
| if match is None or match.group("function") not in EAP_FUNCTIONS: | ||
| raise serializers.ValidationError( | ||
| {"aggregate": _(f"Invalid Metric: {function} is not supported in monitors.")} | ||
| ) | ||
|
|
||
| now = timezone.now() | ||
| resolver = SearchResolver( | ||
| # Formulas such as opportunity_score run a query while resolving, so scope it to the | ||
| # monitor's organization and projects. | ||
| params=SnubaParams( | ||
| projects=projects, organization=organization, start=now - timedelta(hours=1), end=now | ||
| ), | ||
| config=SearchResolverConfig(), | ||
| definitions=SPAN_DEFINITIONS, | ||
| ) | ||
| try: | ||
| resolver.resolve_equation(equation) | ||
| # Number arguments are converted with float() and int(), so a bad value raises ValueError. | ||
| except (InvalidSearchQuery, ArithmeticError, ValueError) as e: | ||
| raise serializers.ValidationError({"aggregate": _(f"Invalid Metric: {e}")}) | ||
|
sentry-junior[bot] marked this conversation as resolved.
Resolved
|
||
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.