Skip to content
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

feat(alerts): Migrate metric alert rules #80505

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ceorourke
Copy link
Member

@ceorourke ceorourke commented Nov 8, 2024

Draft of metric alert migration to use as reference (and at some point, as an actual migration)

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 8, 2024
Copy link
Contributor

github-actions bot commented Nov 8, 2024

This PR has a migration; here is the generated SQL for src/sentry/workflow_engine/migrations/0012_migrate_metric_alerts.py ()

--
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL

Copy link

codecov bot commented Nov 9, 2024

Codecov Report

Attention: Patch coverage is 66.03774% with 36 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...flow_engine/migrations/0015_migrate_alert_rules.py 66.03% 36 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #80505      +/-   ##
==========================================
- Coverage   80.34%   80.34%   -0.01%     
==========================================
  Files        7220     7221       +1     
  Lines      319570   319699     +129     
  Branches    20782    20782              
==========================================
+ Hits       256771   256864      +93     
- Misses      62405    62441      +36     
  Partials      394      394              

WorkflowDataConditionGroup = apps.get_model("sentry", "WorkflowDataConditionGroup")

for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects.all()):
# should I skip migrating snapshotted and disabled rules?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, they're just history, and we're not migrating history from what I remember?


for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects.all()):
# should I skip migrating snapshotted and disabled rules?
# what about the date_added and date_updated fields? can I write to those to preserve history?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to change the definitions or something, I think that auto_add and so on make the cols read only. You'd need to experiment

Copy link
Member Author

@ceorourke ceorourke Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah according to the docs it's uneditable, we'll have to temporarily change the DefaultFieldsModels

# what about the date_added and date_updated fields? can I write to those to preserve history?
# description column will be added to the Detector model, but at the time of writing it isn't there yet. will need to update this
# same for threshold_period, will likely be added to the Detector model but isn't there yet
# AlertRuleActivity creation rows need to be backfilled into the Detector model, needs new created_by and created_at columns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need this if we're not keeping history?

We probably do need to migrate the audit logs over though...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to maintain the data about which user created the rule and when we'll need to put this somewhere. The audit log table has a lot of this but since it was added more recently there's a ~2 year gap of rule data that's missing. I wouldn't be opposed to trying to backfill that table with this instead though.

condition=trigger.threshold_type,
comparison=trigger.alert_threshold,
condition_result=state, # ??? is that right?
type="idk", # talk to josh about this
Copy link
Member Author

@ceorourke ceorourke Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in issue alerts this is https://github.com/getsentry/sentry/blob/master/src/sentry/constants.py#L264, I'm not sure if we need more than 1 for metric alerts

)

# state is based on incident status
project = AlertRuleProjects.objects.get(alert_rule_id=rule.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any performance concerns with us fetching all the alert rules, then fetching these individually?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be slower, but we have < 200k metric alerts, so I don't think it's a huge deal overall

DetectorWorkflow = apps.get_model("sentry", "DetectorWorkflow")
WorkflowDataConditionGroup = apps.get_model("sentry", "WorkflowDataConditionGroup")

for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects.all()):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, i wonder how we could decompose the loop body into a method that's reusable for the migration and the API 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't typically share code between migrations and real code

Copy link
Contributor

This PR has a migration; here is the generated SQL for src/sentry/workflow_engine/migrations/0015_migrate_alert_rules.py ()

--
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL

condition=trigger.threshold_type,
comparison=trigger.alert_threshold,
condition_result=condition_result,
type="metric_alert", # this will probably change by the time we actually do the migration
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs condition_group column (DataConditionGroup id)

@getsantry
Copy link
Contributor

getsantry bot commented Jan 2, 2025

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added Stale and removed Stale labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants