Skip to content

wip(metric-issues): Update script to create metric issues #83025

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bin/load-mocks
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ if __name__ == "__main__":
action="store_true",
help="sleep between each transaction to let clickhouse rest",
)
parser.add_option(
"--load-metric-issues",
default=False,
action="store_true",
help="load metric issues",
)
parser.add_option(
"--metric_alert_id",
default=None,
type=int,
help="load metric issues with a specific alert id",
)

(options, args) = parser.parse_args()

Expand All @@ -55,6 +67,7 @@ if __name__ == "__main__":
load_trends=options.load_trends,
load_performance_issues=options.load_performance_issues,
slow=options.slow,
load_metric_issues=options.load_metric_issues,
)
except Exception:
# Avoid reporting any issues recursively back into Sentry
Expand Down
62 changes: 60 additions & 2 deletions src/sentry/utils/mockdata/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sentry.exceptions import HashDiscarded
from sentry.feedback.usecases.create_feedback import FeedbackCreationSource, create_feedback_issue
from sentry.incidents.logic import create_alert_rule, create_alert_rule_trigger, create_incident
from sentry.incidents.models.alert_rule import AlertRuleThresholdType
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleThresholdType
from sentry.incidents.models.incident import IncidentType
from sentry.ingest.consumer.processors import (
process_attachment_chunk,
Expand Down Expand Up @@ -743,7 +743,7 @@ def generate_events(
return generated_events


def create_metric_alert_rule(organization: Organization, project: Project) -> None:
def create_metric_alert_rule(organization: Organization, project: Project) -> AlertRule:
# Metric alerts
alert_rule = create_alert_rule(
organization,
Expand All @@ -764,6 +764,7 @@ def create_metric_alert_rule(organization: Organization, project: Project) -> No
alert_rule=alert_rule,
projects=[project],
)
return alert_rule


def create_mock_transactions(
Expand Down Expand Up @@ -1318,13 +1319,67 @@ def create_mock_user_feedback(project, has_attachment=True):
create_mock_attachment(event["event_id"], project)


def create_or_update_metric_issue(project, metric_alert_id=None):
from django.utils import timezone

from sentry.incidents.models.alert_rule import AlertRule
from sentry.integrations.metric_alerts import get_incident_status_text
from sentry.issues.grouptype import MetricIssuePOC
from sentry.issues.occurrence_consumer import _process_message
from sentry.issues.producer import PayloadType
from sentry.types.group import PriorityLevel

if not project:
return None

if not metric_alert_id:
alert_rule = create_metric_alert_rule(project.organization, project)
metric_alert_id = alert_rule.id

# collect the data from the incident to treat as an event
event_data: dict[str, str | int] = {
"event_id": uuid4().hex,
"project_id": project.id,
"timestamp": timezone.now().isoformat(),
"platform": project.platform or "",
"received": timezone.now().isoformat(),
}

initial_issue_priority = PriorityLevel.HIGH
alert_rule = AlertRule.objects.get(id=metric_alert_id)
fingerprint = [str(alert_rule.id)]
occurrence = {
"id": uuid4().hex,
"project_id": project.id,
"event_id": str(event_data["event_id"]),
"fingerprint": fingerprint,
"issue_title": alert_rule.name,
"subtitle": get_incident_status_text(alert_rule, str(0.1)),
"resource_id": None,
"type": MetricIssuePOC.type_id,
"detection_time": timezone.now(),
"level": "error",
"culprit": "",
"initial_issue_priority": initial_issue_priority,
# TODO(snigdha): Add more data here as needed
"evidence_data": {"metric_value": 0.1, "alert_rule_id": alert_rule.id},
"evidence_display": [],
}
occurrence.update({"payload_type": PayloadType.OCCURRENCE.value, "event": event_data})
_process_message(occurrence)

return occurrence


def main(
skip_default_setup=False,
num_events=1,
extra_events=False,
load_trends=False,
load_performance_issues=False,
slow=False,
load_metric_issues=False,
metric_alert_id=None,
):
owner = get_superuser()
user = create_user()
Expand All @@ -1335,6 +1390,9 @@ def main(
member = create_member(organization, user, role=roles.get_default().id)

project_map = generate_projects(organization)
if load_metric_issues:
create_or_update_metric_issue(project=project_map["Wind"], metric_alert_id=metric_alert_id)

if not skip_default_setup:
for project in project_map.values():
environment = create_environment(project)
Expand Down
Loading