-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(alerts): Add support for log type alert #93043
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
shruthilayaj
merged 4 commits into
master
from
shruthi/feat/allow-support-for-log-type-alerts
Jun 9, 2025
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from httpx import HTTPError | ||
from rest_framework import status | ||
from rest_framework.exceptions import ErrorDetail | ||
from sentry_protos.snuba.v1.endpoint_create_subscription_pb2 import CreateSubscriptionRequest | ||
from sentry_protos.snuba.v1.request_common_pb2 import TraceItemType | ||
from urllib3.exceptions import MaxRetryError, TimeoutError | ||
from urllib3.response import HTTPResponse | ||
|
||
|
@@ -46,6 +48,8 @@ | |
from sentry.silo.base import SiloMode | ||
from sentry.snuba.dataset import Dataset | ||
from sentry.snuba.metrics.naming_layer.mri import SessionMRI | ||
from sentry.snuba.models import SnubaQueryEventType | ||
from sentry.snuba.tasks import create_subscription_in_snuba | ||
from sentry.testutils.abstract import Abstract | ||
from sentry.testutils.cases import APITestCase, SnubaTestCase | ||
from sentry.testutils.factories import EventType | ||
|
@@ -54,6 +58,7 @@ | |
from sentry.testutils.outbox import outbox_runner | ||
from sentry.testutils.silo import assume_test_silo_mode | ||
from sentry.testutils.skips import requires_snuba | ||
from sentry.utils.snuba import _snuba_pool | ||
from sentry.workflow_engine.models import ( | ||
Action, | ||
ActionAlertRuleTriggerAction, | ||
|
@@ -408,23 +413,89 @@ def test_anomaly_detection_alert_eap(self, mock_seer_request): | |
assert alert_rule.sensitivity == resp.data.get("sensitivity") | ||
assert mock_seer_request.call_count == 1 | ||
|
||
def test_create_alert_rule_eap(self): | ||
@patch( | ||
"sentry.snuba.subscriptions.create_subscription_in_snuba.delay", | ||
wraps=create_subscription_in_snuba, | ||
) | ||
def test_create_alert_rule_eap_spans(self, mock_create_subscription_in_snuba): | ||
for event_type in ["transaction", "trace_item_span", None]: | ||
data = deepcopy(self.alert_rule_dict) | ||
data["dataset"] = "events_analytics_platform" | ||
data["alertType"] = "eap_metrics" | ||
data["timeWindow"] = 5 | ||
if event_type: | ||
data["eventTypes"] = [event_type] | ||
|
||
with ( | ||
outbox_runner(), | ||
self.feature(["organizations:incidents", "organizations:performance-view"]), | ||
): | ||
with patch.object( | ||
_snuba_pool, "urlopen", side_effect=_snuba_pool.urlopen | ||
) as urlopen: | ||
resp = self.get_success_response( | ||
self.organization.slug, | ||
status_code=201, | ||
**data, | ||
) | ||
|
||
rpc_request_body = urlopen.call_args[1]["body"] | ||
createSubscriptionRequest = CreateSubscriptionRequest.FromString( | ||
rpc_request_body | ||
) | ||
|
||
assert ( | ||
createSubscriptionRequest.time_series_request.meta.trace_item_type | ||
== TraceItemType.TRACE_ITEM_TYPE_SPAN | ||
) | ||
|
||
assert "id" in resp.data | ||
alert_rule = AlertRule.objects.get(id=resp.data["id"]) | ||
assert resp.data == serialize(alert_rule, self.user) | ||
|
||
@patch( | ||
"sentry.snuba.subscriptions.create_subscription_in_snuba.delay", | ||
wraps=create_subscription_in_snuba, | ||
) | ||
def test_create_alert_rule_logs(self, mock_create_subscription_in_snuba): | ||
data = deepcopy(self.alert_rule_dict) | ||
data["dataset"] = "events_analytics_platform" | ||
data["alertType"] = "eap_metrics" | ||
data["aggregate"] = "count_unique(org)" | ||
data["aggregate"] = "count(message)" | ||
data["eventTypes"] = ["trace_item_log"] | ||
data["timeWindow"] = 5 | ||
with ( | ||
outbox_runner(), | ||
self.feature(["organizations:incidents", "organizations:performance-view"]), | ||
self.feature( | ||
[ | ||
"organizations:incidents", | ||
"organizations:performance-view", | ||
"organizations:ourlogs-alerts", | ||
] | ||
), | ||
): | ||
resp = self.get_success_response( | ||
self.organization.slug, | ||
status_code=201, | ||
**data, | ||
) | ||
with patch.object(_snuba_pool, "urlopen", side_effect=_snuba_pool.urlopen) as urlopen: | ||
resp = self.get_success_response( | ||
self.organization.slug, | ||
status_code=201, | ||
**data, | ||
) | ||
|
||
rpc_request_body = urlopen.call_args[1]["body"] | ||
createSubscriptionRequest = CreateSubscriptionRequest.FromString(rpc_request_body) | ||
|
||
assert ( | ||
createSubscriptionRequest.time_series_request.meta.trace_item_type | ||
== TraceItemType.TRACE_ITEM_TYPE_LOG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. making sure snuba subscription is created with trace item type log |
||
) | ||
|
||
assert "id" in resp.data | ||
alert_rule = AlertRule.objects.get(id=resp.data["id"]) | ||
assert resp.data == serialize(alert_rule, self.user) | ||
assert ( | ||
SnubaQueryEventType.objects.filter(snuba_query_id=alert_rule.snuba_query_id)[0].type | ||
== SnubaQueryEventType.EventType.TRACE_ITEM_LOG.value | ||
) | ||
|
||
@with_feature("organizations:anomaly-detection-alerts") | ||
@with_feature("organizations:anomaly-detection-rollout") | ||
|
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.
making sure snuba subscription is created with trace item type span