feat: Introduce a new GenericRules class to replace Rules class - #192
swetha1654 wants to merge 19 commits into
Conversation
|
Deprecating AlertRules is certainly a major version bump requirement. We need to be careful because this will break our libs. |
7a04ac3 to
abe94d8
Compare
What I don't like about this designThe usage of kwargs to pass backend specific inputs via the public facing
Hence I ended up using kwargs. Using kwargs sacrifies type safety but its the standard python pattern for this situation. WDYT? |
I thought we said this was not the case? Also, the passing utets indicate that this is not true. Can you update the PR description? |
from cosl.rules import generic_alert_groups, HOST_METRICS_MISSING_RULE_NAME
# or
from cosl import generic_alert_groupswill now fail, and these are used in many places. We need to be careful with backwards compatibility here. You can add this to # rules.py — backward-compat re-exports (deprecated, remove in next major)
from .prometheus import generic_alert_groups, HOST_METRICS_MISSING_RULE_NAME # noqa: F401 |
MichaelThamm
left a comment
There was a problem hiding this comment.
In the old Rules.add_path:
else:
logger.debug("Rules path does not exist: %s", path)In the new GenericRules.add_path:
else:
raise InvalidRulePathError(...)I think this difference is fine, because we said that we would allow Rules and GenericRules to exist alongside one another and deprecate in the future.
@MichaelThamm my bad i forgot to remove it, I've updated the PR description |
Yes this shouldn't be a breaking change or problem |
@MichaelThamm I dont think |
Co-authored-by: Michael Thamm <mike.thamm@canonical.com> Signed-off-by: swetha1654 <swetha.swaminathan@canonical.com>
|
To gain extra confidence that this PR is not breaking and providing the features we want, we should:
|
|
@MichaelThamm I have created a tandem PR in charmlib to update the |
I think the linting will most likely fail. You would need to add |
MichaelThamm
left a comment
There was a problem hiding this comment.
I think this PR is pretty much ready to be merged content-wise after my recent comments are resolved. However, there is one thing which makes me nervous: we are lacking a lot of rules coverage for the new rules class. You added some tests: test_backend and test_generic_rules, but these provide little coverage compared to the existing set of tests we had for the Rules class.
One could argue that since this lib will only be used by the OTLP charmlib, we there is minimal risk. I think this is a good way to faze in this feature into the rules ecosystem without too much friction for our users. We will have to migrate the tests slowly to use the new rules classes to gain the same confidence as before. I don't think this is your responsibility, and given that you will be an early adopter of this feature, I think we can proceed with merging. I will discuss this briefly with @sed-i just to be safe.
Co-authored-by: Michael Thamm <mike.thamm@canonical.com> Signed-off-by: swetha1654 <swetha.swaminathan@canonical.com>
Signed-off-by: swetha1654 <swetha.swaminathan@canonical.com>
Signed-off-by: swetha1654 <swetha.swaminathan@canonical.com>
|
Thanks for this @swetha1654! Before we go all in on As you outlined in the comparison table in #191,
Looking at -from cosl.rules import HOST_METRICS_MISSING_RULE_NAME, Rules, generic_alert_groups
+from cosl.rules import HOST_METRICS_MISSING_RULE_NAME, Rules, SigmaRules, generic_alert_groups@dataclass
class RuleStore:
topology: JujuTopology
logql: Rules = field(init=False)
promql: Rules = field(init=False)
+ sigma: SigmaRules = field(init=False)class _RulesModel(BaseModel):
logql: OfficialRuleFileFormat = Field(...)
promql: OfficialRuleFileFormat = Field(...)
+ sigma: SigmaRuleFileFormat = Field(
+ description='...',
+ default_factory=SigmaRuleFileFormat,
+ )Then, for sigma rules, instead of inheriting ready-made implementations from parent classes, we could extract logic into free function that could be reused by SigmaRules. Wdyt? |
|
@sed-i I do think your suggestion is the quickest way to get Sigma rules into otelcol. The main updates are required in the rules method for topology injection (if desired), and the RuleStore object. cos-lib abstracts a lot of the rules effort for PromQL and LogQL (e.g., injection, validation, adding, as_dict, etc.). As long as we still have Sigma implementation detail abstracted in cos-lib (like we do for promQL and logQL), then I think this is a great solution. Similar to how promql are added to the rules store: self.promql.add(...) using methods from a Sigma rules class in cos-lib, would be trivial. |
|
@sed-i @MichaelThamm If i understand correctly, what you're proposing is a seperate SigmaRules class in
I agree that Sigma rules themselves don't have a lot in common with *QL-rules, but there is still a lot in common between what we want to do with these rules. For eg:
Implementing a seperate SigmaRules class would mean repeating a lot of common logic between the user facing APIs, possibilities of divergence in the expected behaviour of Let me know if you have any further questions. I have visualized what I am trying to do with this PR in a diagram in the original issue for better understanding. |
|
Closing this PR in favor of: |
Issue
POC for #191
Tandem PRs
Visualization
The diagram visualizing the changes in this PR can be found in the issue
Solution
Some notes about the new proposal:
AlertRules,RecordingRulesandRulesclass are used by several downstream libraries and repos including:cos-proxy-operator,prometheus_scrape,prometheus_remote_write,grafana-agent-operator. Rewriting theRulesclass would break all of them hence I've proposed aAbstractRulesclass.Rulesclass implements both the public facing APIs (add,add_path,as_dict) as well the backend specific logic (topology injection, rules validation, group name definition logic). I've broken this down intoAbstractRulesclass that exposes the public APIs and a genericRuleBackendclass that every component uses to implement their own validation and injection logic.inject_and_validatefrom the oldRulesclass has been adopted and modified inAbstractRulesclass to justvalidate. I don't see value in running the injection again on a set of rules that are already injected with juju topology (note that the add method calls_from_dictwhich does the injection. Theaddmethod is called by the requirer class before dumping the data into a relation databag.)Checklist
project.versionfield in thepyproject.tomlfile.Context
This PR makes the Rules class generic enough to accept new Rule types in the future. (for eg: Sigma rules)
Testing Instructions
Upgrade Notes
Changes for downstream users
generic_alert_groups,HOST_METRICS_MISSING_RULE_NAMEhas been moved tocosl.prometheushence downstream imports of it will fail, but this should be a very easy fix.There should ideally not be any other breaking changes.