Repository navigation
feat(provisioning): provision correlation rules from KEEP_CORRELATION_RULES - #6632
aditya-786 wants to merge 1 commit into
Conversation
…_RULES Correlation (rules-engine) rules were the only alerting resource the startup provisioner did not cover: providers, workflows, dashboards, deduplication rules, and mapping rules can all be declared in Helm values, but correlation rules could only be created via the UI, the SDK, or POST /rules. Add provision_correlation_rules_from_env, mirroring the deduplication provisioner. KEEP_CORRELATION_RULES holds a JSON array (or a path to a .json file) of rule specs in the POST /rules schema. On every startup the DB is reconciled to the env: upsert by ruleName, delete provisioned rules no longer present, and leave UI-created rules untouched, so rolling restarts stay idempotent. Each celQuery is compiled at provision time and invalid specs are skipped so one bad rule does not block the rest. Add an is_provisioned column to Rule (with migration) to track which rules the provisioner owns. Closes keephq#6563
|
We reviewed this PR, ran its tests, and deployed it on 0.54.3 in a self-hosted single-tenant install, where it does what it says — correlation rules that previously existed only as a database row are now declared in configuration and reconciled on every start. Thanks for writing it. Some findings from reviewing and running it, in case they help it move. The red checks are not this PR. All three failures are
What worked for us was giving _UNCHANGED = object()
def update_rule(..., assignee=None, group_description=_UNCHANGED):
...
if group_description is not _UNCHANGED:
rule.group_description = group_descriptionThe sentinel matters: with a plain A threshold = rule_to_provision.get("threshold", 1)
if isinstance(threshold, bool) or not isinstance(threshold, int) or threshold <= 0:
raise ValueError("'threshold' must be a positive integer")
Docstring vs. behaviour on paths. One adoption note for anyone deploying this, which might be worth a line in the PR description: the provisioner only manages rules with The migration applied cleanly on the current single head ( |
Why are these changes needed?
The startup provisioner (
keep/api/config.py:provision_resources) configures providers, workflows, dashboards, deduplication rules, and mapping rules from Helm values, but correlation (rules-engine) rules were left out. Today the only ways to create one are the UI, the SDK, orPOST /rules, so a GitOps deployment cannot declare the alert grouping rule next to the workflow that uses the same CEL filter, and operators have toPOST /rulesafter every cluster bootstrap.This adds
provision_correlation_rules_from_env, giving correlation rules the same env-driven provisioning the other resources already have. Closes #6563.What changed
keep/api/bl/correlation_rules_provisioning.py(new):provision_correlation_rules_from_env(tenant_id), mirroringprovision_deduplication_rules_from_env.KEEP_CORRELATION_RULESholds a JSON array (or a path to a.jsonfile, dispatched by the same regex the dedup provisioner uses) of rule specs in thePOST /rulesschema (ruleName,celQuery,sqlQuery,timeframeInSeconds,timeUnit,groupingCriteria,createOn,resolveOn,incidentNameTemplate,incidentPrefix,threshold,requireApprove).ruleName, delete provisioned rules that are no longer present, and never touch UI-created rules (is_provisioned=False). Unsetting the env var deprovisions everything the provisioner owns. Rolling restarts are idempotent.celQueryis compiled at provision time; a spec that is missing a required field or has an unparseable CEL is logged and skipped, so one bad rule does not block the others.is_provisionedcolumn onRule+ Alembic migrationb3f1c9a24d70(on the current head67ff7efffed4), tracking which rules the provisioner owns. Mirrors the flag added toMappingRule.db.create_rulegains anis_provisioned=Falseparameter (same shapecreate_deduplication_rulealready has).provision_resources()calls the new function right afterprovision_mapping_rules_from_env.Example
values.yaml:Notes on scope
keephq/helm-chartsrepo, so exposing acorrelationRuleskey in the chart templates is a companion change there; this PR is the backend that reads the env var (same split as the existing dedup/mapping provisioning).matches()footgun the issue raises: the pinnedcel-pythonin this repo does evaluatename.matches('^Foo')(returns a bool), so that specific case does not reproduce here. Provision-time validation therefore compiles the CEL to catch genuine parse errors rather than attempting a trial evaluation, which would false-negative on any rule referencing fields absent from a synthetic payload.Checks
tests/test_correlation_rules_provisioning.py(11 tests, mirroring the mapping/dedup provisioning tests): create, multiple, idempotent, update-in-place, deprovision-on-removal, deprovision-on-unset, leave-UI-rules-untouched, skip-invalid-CEL, skip-missing-field, load-from-.json-file, and clean no-op. All pass locally; the rules-engine suite (tests/test_rules_engine.py) stays green.