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

Alerting: Add database migration for recording rule fields #87012

Merged
merged 16 commits into from May 9, 2024

Conversation

alexweav
Copy link
Contributor

@alexweav alexweav commented Apr 26, 2024

What is this feature?

This defines two new rule fields to be used in recording rules.

  • metric: The name of the series to write to
  • from: indicates which expression node is considered the result of the graph - the same thing as "condition" but for recording rules.

These are stored inside a sub-struct encompassing all recording rule data, called record.

Let the naming bikeshed commence 😄

Which issue(s) does this PR fix?:

n/a

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@alexweav alexweav added area/alerting Grafana Alerting area/backend no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes labels Apr 26, 2024
@alexweav alexweav requested review from a team as code owners April 26, 2024 20:57
@alexweav alexweav requested review from rwwiv, JacobsonMT, yuri-tceretian, grobinson-grafana and zserge and removed request for a team April 26, 2024 20:57
@grafana-delivery-bot grafana-delivery-bot bot added this to the 11.1.x milestone Apr 26, 2024
@@ -240,6 +240,8 @@ type AlertRule struct {
PanelID *int64 `xorm:"panel_id"`
RuleGroup string
RuleGroupIndex int `xorm:"rule_group_idx"`
Record string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prometheus just reuses the title for this - but it also calls the field Record. That's where the name came from.

I think we should not reuse the title directly. There is a chance that the other fields may not be needed - we need some field to identify a recording rule, or it becomes indistinguishable from certain alert rule patch requests. Nothing stops users from using the same string for title and record.

@@ -240,6 +240,8 @@ type AlertRule struct {
PanelID *int64 `xorm:"panel_id"`
RuleGroup string
RuleGroupIndex int `xorm:"rule_group_idx"`
Record string
RecordFrom string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is literally the same thing as Condition. But, the term "condition" doesn't make any sense for recording rules.

I'm also open to not including this field, and instead renaming Condition to something more generic. Naming is hard though, and i don't think the duplication hurts anything as it's logically a different thing.

@@ -240,6 +240,8 @@ type AlertRule struct {
PanelID *int64 `xorm:"panel_id"`
RuleGroup string
RuleGroupIndex int `xorm:"rule_group_idx"`
Record string
RecordFrom string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There might be another field called RecordTo, indicating the datasource to write the results to. Whether we want a per-rule datasource, or just config.ini knobs for that, is still up for debate in the design doc and has not reached consensus. So, I've left that field out for now.

That's where the current name came from. "RecordFrom" and "RecordTo" make a nice pair, if both exist - but open to alternative suggestions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the new struct format we could call this To or Target. Less important now though

Comment on lines 519 to 521
if alertRule.Record != "" {
return fmt.Errorf("%w: Storing recording rules is not yet allowed.", ErrAlertRuleFailedValidation)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really reachable since we do not have API layer fields for this, just making things apparent. This was useful to catch serialization bugs in tests.

@@ -240,6 +240,8 @@ type AlertRule struct {
PanelID *int64 `xorm:"panel_id"`
RuleGroup string
RuleGroupIndex int `xorm:"rule_group_idx"`
Record string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also begs the question - we should probably either rename AlertRule to just Rule, since it might not actually be an alerting rule any more... or have separated domain model for alert rules vs recording rules that are combined in the API/rule table.

I'm considering both not in scope for this PR and leaving them for the future, as they're deep refactor type changes.

@alexweav alexweav force-pushed the alexweav/recording-rules-database branch from eb81c51 to e1248ab Compare May 3, 2024 21:42
Copy link
Contributor

@rwwiv rwwiv left a comment

Choose a reason for hiding this comment

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

Format LGTM 👍

@@ -757,3 +766,26 @@ func GroupByAlertRuleGroupKey(rules []*AlertRule) map[AlertRuleGroupKey]RulesGro
}
return result
}

// Record contains mapping information for Recording Rules.
type Record struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Bikeshedding nit: WDYT about Recording?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The field name or the struct name? It's a field called Record, of type Record right now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think field name should remain (record also has meaning in prometheus). Struct name i'm totally good with changing if we want - but this can be done without DB migration anytime in future

Copy link
Contributor

@yuri-tceretian yuri-tceretian left a comment

Choose a reason for hiding this comment

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

LGTM

@alexweav
Copy link
Contributor Author

alexweav commented May 9, 2024

Tested migration on all supported grafana databases, no issues

@alexweav alexweav merged commit 36ef611 into main May 9, 2024
13 checks passed
@alexweav alexweav deleted the alexweav/recording-rules-database branch May 9, 2024 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/alerting Grafana Alerting area/backend/db/migration area/backend no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants