-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(alerts): Add support for trace metrics alert type #104901
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d222343
feat(alerts): Add support for trace metrics alert type
k-fish a0ef3e9
Merge branch 'master' into feat/tracemetrics/add-alerts-backend
k-fish d7fc2da
feat(alerts): Add support for trace metrics alert type
k-fish 2e4d4f1
test trace metrics feature flag check, fix type
k-fish c14562d
fix types
k-fish aedd0ae
Merge remote-tracking branch 'origin/master' into feat/tracemetrics/a…
k-fish dc03815
Cleanup
k-fish 264da59
PR comments
k-fish 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
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,71 @@ | ||
| import logging | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from sentry.exceptions import InvalidSearchQuery | ||
| from sentry.search.eap.resolver import SearchResolver | ||
| from sentry.search.eap.trace_metrics.config import TraceMetricsSearchResolverConfig | ||
| from sentry.search.eap.trace_metrics.definitions import TRACE_METRICS_DEFINITIONS | ||
| from sentry.search.eap.trace_metrics.types import TraceMetric | ||
| from sentry.search.events.types import SnubaParams | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def extract_trace_metric_from_aggregate(aggregate: str) -> TraceMetric | None: | ||
| """ | ||
| Extract trace metric information from an aggregate string using SearchResolver. | ||
| Args: | ||
| aggregate: The aggregate function string | ||
| Returns: | ||
| TraceMetric | None: The extracted trace metric or None if no specific metric | ||
| Raises: | ||
| InvalidSearchQuery: If the aggregate is invalid | ||
| """ | ||
|
|
||
| now = datetime.now(tz=timezone.utc) | ||
| snuba_params = SnubaParams( | ||
| projects=[], | ||
| organization=None, | ||
| start=now - timedelta(hours=1), | ||
| end=now, | ||
| ) | ||
|
|
||
| resolver = SearchResolver( | ||
| params=snuba_params, | ||
| config=TraceMetricsSearchResolverConfig( | ||
| metric=None | ||
| ), # It's fine to not pass a metric here since that way of using metrics is deprecated. | ||
| definitions=TRACE_METRICS_DEFINITIONS, | ||
| ) | ||
|
|
||
| resolved_function, _ = resolver.resolve_function(aggregate) | ||
|
|
||
| return getattr(resolved_function, "trace_metric", None) | ||
|
|
||
|
|
||
| def validate_trace_metrics_aggregate(aggregate: str) -> None: | ||
| """ | ||
| Validate a trace metrics aggregate using the SearchResolver. | ||
| Args: | ||
| aggregate: The aggregate function to validate | ||
| Raises: | ||
| serializers.ValidationError: If the aggregate is invalid | ||
| """ | ||
| try: | ||
| trace_metric = extract_trace_metric_from_aggregate(aggregate) | ||
| if trace_metric is None: | ||
| raise InvalidSearchQuery( | ||
| f"Trace metrics aggregate {aggregate} must specify metric name, type, and unit" | ||
| ) | ||
| except InvalidSearchQuery as e: | ||
| logger.exception("Invalid trace metrics aggregate: %s %s", aggregate, e) | ||
| raise serializers.ValidationError( | ||
| {"aggregate": f"Invalid trace metrics aggregate: {aggregate}"} | ||
| ) | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation error loses specific error message details
Low Severity
When
validate_trace_metrics_aggregatecatches anInvalidSearchQueryexception, it discards the specific error message fromeand replaces it with a generic message. This includes cases wheretrace_metric is None(which raises anInvalidSearchQuerywith "must specify metric name, type, and unit") and errors fromresolve_function. The original error is only logged but not returned to the user, making it difficult to understand why a trace metrics aggregate is invalid.